How to Avoid Using Round() in Every \Sexpr{}

How to avoid using round() in every \Sexpr{}?

If you have read the motivation of the knitr package, you probably know I'm the person who hates round() inside \Sexpr{} most. I suggested this to R core long time ago for Sweave but it was ignored, so I started my own package.

Although the answer by Josh O'Brien is absolutely correct, I should say you really only need options(digits = 2) with knitr, and there is no need to reset the default inline hook because the default hook respects this option.

print vector as with and in \Sexpr{}

You can use knitr::combine_words(x).

Using cat() is only for its side-effect: printing in the console. cat() won't return a character string, so you won't see anything in the output. By comparison, knitr::combine_words() returns a character string.

Standardized output of test statistics with \Sexpr

Why not just write a function to output what you want. For example, at the start of your document, have:

<<echo=FALSE, tidy=FALSE>>=
trans = function(mycor) {

out1 = paste0("$r(", mycor$parameter, ") = ", signif(mycor$estimate, 2), "$,")

p = mycor$p.value
cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1)
round_p = cut(p, cutpoints, labels=cutpoints[-1])
p_value = paste0("$p < ", round_p, "$" )

paste(out1, p_value)
}
@

Which allows \Sexpr{trans(mycor)} to work as you want.

R / Sweave formatting numbers with \Sexpr{} in scientific notation

I think this function should work:

sn <- function(x,digits)
{
if (x==0) return("0")
ord <- floor(log(abs(x),10))
x <- x / 10^ord
if (!missing(digits)) x <- format(x,digits=digits)
if (ord==0) return(as.character(x))
return(paste(x,"\\\\times 10^{",ord,"}",sep=""))
}

Some tests:

> sn(2000000)
[1] "2\\\\times 10^{6}"
> sn(0.001)
[1] "1\\\\times 10^{-3}"
> sn(0.00005)
[1] "5\\\\times 10^{-5}"
> sn(10.1203)
[1] "1.01203\\\\times 10^{1}"
> sn(-0.00013)
[1] "-1.3\\\\times 10^{-4}"
> sn(0)
[1] "0"

If you want the result in math mode you could enter $ signs in the paste() call.

Edit:

Here is a Sweave example:

\documentclass{article}

\begin{document}
<<echo=FALSE>>=
sn <- function(x,digits)
{
if (x==0) return("0")
ord <- floor(log(abs(x),10))
x <- x / 10^ord
if (!missing(digits)) x <- format(x,digits=digits)
if (ord==0) return(as.character(x))
return(paste(x,"\\\\times 10^{",ord,"}",sep=""))
}
@

Blablabla this is a pretty formatted number $\Sexpr{sn(0.00134,2)}$.

\end{document}

Programming R/Sweave for proper \Sexpr output

@KennyTM got it. @David, you should avoid options(digits = 1) since it will affect all of your calculations (it will suppress decimals in each output afterwards). So use round() function after applying the weighted.mean(). Something like this:

\Sexpr{round(p.mean(x))}

And do not use print(), but return(). Here's why:

> set.seed(1234)
> x <- rnorm(100)
> foo <- function(x) {
res <- mean(x) + 5
print(res)
}
> foo(x)
[1] 5
> foo(x) + 10
[1] 5
[1] 15

Use return() or just type the resulting variable in the last line:

> bar <- function(x) {
res <- mean(x) + 5
return(res)
}
> bar(x) + 10
[1] 15

So, rewrite your function, and be sure to use as.character()... you have all the bits, now just put it all together.

P.S.

I'm not sure how you function works... I've never used weighed mean in my analysis. The bit that's puzzling me the most is weight=weight. Wouldn't it be nicer to put one more argument in function? Frankly, I'm still amazed by the fact that your function is giving you right result... probably because you have weight variable defined prior to function definition. [EDIT] You will not get the weighted mean with your function if you don't have weight function predefined, but "regular" mean!

I hope this one helped you!

Kind regards,
Aleksandar



Related Topics



Leave a reply



Submit