How can I get R chunk code to produce the same number of digits as r inline code in RStudio RMarkdown file?
For example:
```{r}
x=1:30
sigma.sq=sum((x-mean(x))^2)/30
sigma.sq
```
Thus, $\sigma^2=`r sigma.sq`$.
The chunk produces the number 74.92, but the inline code produces the number 74.9167. I'd like both to be the same.
Suggestions?
D.
P.S. Here's a screen shot of what the above code produces.
round() function gives you an answer. For example, the below inline code in R Markdown would display the second decimal place of the number of the object.
`r round(sigma.sq, 2)`
Related
I am trying to write a markdown document where I have a chunk of R code within a theorem or example extension.
I assumed I could simply write the theorem extension, starting and ending with ``` and within define an R code chunk starting with ```{r} and ending with ```. As follows:
```{theorem, name="Test"}
Some initial text.
```{r}
a <- 1 + 1
print(a)
And some more text.
However, it seems markdown gets confused, as does stackoverflow, by closing the theorem chunk using the end of the R code block instead of nesting the blocks. Any ideas on how to solve this?
You can't nest chunks like that in r-markdown. You'll need to close your first chunk then open a new one for the R chunk.
I'm working on my first R notebook which works pretty well, except for one issue.
I'd like to be the numbers that I output inline with
`r realbignumber`
to have commas as separator and max 2 decimal points: 123,456,789.12
In order to achieve this, I added a chunk at the beginning of my document, which contains...
```{r setup}
knitr::opts_chunk$set(echo = FALSE, warning=FALSE, cache = TRUE, message = FALSE)
knitr::opts_chunk$set(inline = function(x){if(!is.numeric(x)){x}else{prettyNum(round(x,1), big.mark = ",")}})
options(scipen=999)
```
The suppression of scientific numbers works like a charm, so the chunk is definitely executed. However, formatting of the inline output of numbers does not work.
Any ideas why that could be?
Do these kinds of settings generally not work with R notebooks?
Edit:
The solution suggested here also has no effect on the output format of numbers.
Here is an example illustrating two ways to print a large number in an R Markdown document. First, code to use the prettyNum() function in an inline R chunk.
Sample document where we test printing a large number. First set the number in an R chunk.
```{r initializeData}
theNum <- 1234567891011.03
options(scipen=999,digits=16)
```
The R code we'll use to format the number is: `prettyNum(theNum,width=23,big.mark=",")`.
Next, print the large number. `r prettyNum(theNum,width=23,big.mark=",")`.
The alternative of using chunk options works as follows.
Now, try an alternative using knitr chunks.
```{r prettyNumHook }
knitr::knit_hooks$set(inline = function(x) { if(!is.numeric(x)){ x }else{ prettyNum(x, big.mark=",",width=23) } })
```
Next, print the large number by simply referencing the number in an inline chunk as `theNum`: `r theNum`.
When both chunks of code are embedded in an Rmd file and knit, the output is as follows, showing that both techniques produce the same result.
regards,
Len
I'm generating a knitr (focusing on .Rmd files) manual for a project and wanted to embed a code chunk that actually displays the " ``` " marks as well as the internal chunk syntax. E.g.
``` {r sample, eval= FALSE}
Code example
```
So basically the exact visual you see above. Any thoughts on how to do this?
You can do this by using four spaces before your code as follows:
So this:
Becomes this:
There also is this SO-meta discussion on how to escape this backtick in Markdown
If you mean how to display the three backticks, the answer is "indent by 4 spaces" (see Pandoc's four-space rule)". If you mean how to display a literal knitr code chunk, the answer has been given by knitr FAQ #8.
In my knitr repport I have several paragraphs that are only relevant if some criteria is met.
Wrapping everything in an inline r ifelse(... gets convuluted real fast.
So I tried with a code chunk like this
```{r conditional_block, eval=nrow(data)>0, results="asis"}
print("For theese `r nrow(data)` people, the mean salary is `r paste(round(mean(data$sallary),2))` dollars per year")
```
I tried with print, paste and cat. And I tired with results asis and markup. But the output is always - 'raw' the inline R code shows verbatim.
The problem with the code chunk shown in the question is rather conceptual than technical: The content of the chunk is interpreted as R code. Using knitr's syntax for inline output in the R context is neither possible nor necessary. Instead, the normal string functions should be used to compose the output string:
```{r conditional_block, eval=nrow(data)>0, results="asis"}
cat(sprintf(
"For these %d people, the mean salary is %.2f dollars per year.",
nrow(data), mean(data$salary))
)
```
In R, using knitr, is there a way to prevent line breaks in the HTML when results='hide' and echo=FALSE?
In this case:
First I do this,
```{r results='hide', echo=FALSE}
x=4;x
```
then I do that.
I get:
First I do this,
then I do that.
with both a break and an extra line between.
I'd like to get:
First I do this, then I do that.
instead.
Generally speaking, I'd like for code chunks to not insert new lines so that markdown is free to eat the one after the first line of text.
Thanks,
I assume you're creating an HTML document from an R Markdown document. In that case, you can use the inline R code capability offered by knitr by using the ` characters starting with the letter r.
Example:
In your R Markdown, write:
First I do this,`r x=4` then I do that. I can call x by doing `r x`.
And as output, you get:
First I do this, then I do that. I can call x by doing 4.
Note that in my example, I evaluated the variable x, but if you do not want to evaluate it, you do not have to. The variable x should still be assigned a value of 4 from the
`r x=4`
part of the R Markdown.
This is Inline R Code, and is documented here under the section "Inline R Code".
EDIT:
Note that Inline R Code has properties that are analogous to "echo=FALSE". And if you want to hide the results from inline R code, you can use base R functions to hide the output. See this question.
Try something like:
``` {r , results="asis", echo=F, eval=T}
if(showMe){
cat("printed")
} else {
cat("<!-- no break line -->")
}
```