Include a chunk in an R Markdown footnote - r

this is the closest question I have found to mine, but it looks a bit dated.
I want to add a code chunk with eval = FALSE into the footnote of an R Markdown html document. I have tried the standard notation as in 2.5.1 here, but knitting ignores it.
Thanks for your help.

It should work if it your footnote looks like this:
Random Numbers[^a-random-footnote-label]
Other text for your document.
Other text for your document.
Other text for your document.
[^a-random-footnote-label]:
```{r, echo = F}
runif(1:10)
```
So you don't define the footnote directly via Random Numbers^[content footnote]. Instead you name it and define the contents later.

Related

custom block which includes R in bookdown

In bookdown how can I include R code within a custom block, whereby the R code will be parsed. Like in the example below the r code (plot function) will not be parsed. Is there a way to make this work?
```{block2 type='test'}
some text here
plot(1:10)
```
You can use text references as a hack. This seems to work as long as valid_markdown contains a single paragraph/line of markdown.
```{r}
valid_markdown <- your_function()
```
(ref:my_hack) `r valid_markdown`
```{block2, type="block"}
(ref:my_hack)
```
This hack works fine with text. Getting it work with plots will require a bit more work.

How to display ``` in knitr chunks?

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.

Is there a way to prevent line break in the HTML when results='hide' and echo=FALSE?

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 -->")
}
```

Using in line r code as part of a R markdown header

I wish to use in line R code as part of a header in a r markdown file. However when i knit the file the fonts used on the header are different. How can I ensure the fonts are the same. A simple example is below.
`r 1+1` Header
-------------------------
You can wrap content in backticks to denote r code inline, as follows:
## Title `r 1+1` Header
Without a reproducible example it is hard to be precise, but one thing you might want to consider is to use the results="asis" chunk option in your R code so that the results are not wrapped in a code markup block. I am not sure how this works with inline commands, but you could use a regular R block and have it create the entire header from the R code, something like:
```r results="asis"
cat('# ', 1+1, " Header")
```

global comment option for R markdown in knitr

To change the leading characters for the output a knitr chunk in .Rmd has a comment option, like
```{r comment = ""}
1:100
```
Is there a way to set this globally, not separately for every chunk?
opts_knit$set(comment = "")
does not work and I cannot find it anywhere in the documentation.
Use opts_chunk$set() because that is a chunk option; opts_knit is for package options (sorry about the similar names which apparently confused you). See http://yihui.name/knitr/options

Resources