I'm using knitr to create a markdown file from Rmd and I have the following option set at the top of my .Rmd script to hide all results and plots:
```{r, echo=FALSE}
opts_chunk$set(results="hide", fig.show="hide")
```
When I hit the Knit HTML button in RStudio, this works - I get output without the results and figures. But if I run from the command line:
Rscript -e 'knitr::knit("myfile.Rmd")'
It appears the opts_chunk$set() line isn't read, and I get results and plots in my .md output. I've worked around the problem by specifying these options in the Rscript command:
Rscript -e 'library(knitr); opts_chunk$set(results="hide", fig.show="hide"); knit("myfile.Rmd")'
But I'd rather keep all the options read from the file I'm using rather than specified at the command line. How do I get the options read in the .Rmd file when kniting with Rscript at the command line?
Thanks.
I think you need to add
library("knitr")
to the chunk (you might want to set message=FALSE in the chunk options for that chunk).
The problem is that when you do
Rscript -e 'knitr::knit("myfile.Rmd")'
you're not actually attaching the knitr package, which means it isn't in the search path for functions, which means that R can't find the opts_chunk object.
Using knitr::opts_chunk might work too ...
as you suggested, so does Rscript -e 'library("knitr"); knit("myfile.Rmd")'
When you click the button in RStudio, RStudio automatically loads knitr in the environment in which it runs knit().
Related
Rmarkdown chunks by default display the input code block and the output block. The code block is highlighted by default and the output verbatim text is usually not. If I am running some code that generates more code, is it possible to show code highlighting in the output block?
For example, this chunk executes R code
```{r,echo=FALSE}
paste0("cp -R ",getwd(),"/* ./dir")
```
to generate valid shell code as the output.
"cp -R /home/user/work/* ./dir"
Can this output be code highlighted for shell script? The output code does not have to be evaluated.
Use the chunk option class.output and specify the language name, e.g.
```{r, class.output='sh', comment=''}
cat('ls -l "$HOME"') # sh for shell scripts
```
Is there any way possible to include code for different languages in Rmarkdown documents while authoring books using bookdown package? I have looked at the knitr option like engine with possible values like python, awk/gawk and the executable path can be set using engine.path.
```{r, engine='python'}
print "Will this code chunk be hidden?"
```
However, I would like to just insert code (eg: git) in the Rmarkdown document without executing it.
For example, like including in the markdown documents
```git
git init
```
If you don't want the code to be executed, you can embed it like:
```
code not executed
```
You can also have code highlighting in specific languages:
```css
my_css{}
```
For your git command, this is execution from terminal, so you can highlight it with:
```sh
git init
```
Once again I am in the situation that I want to replicate what is happening when I press the Compile PDF button on an .Rnw file in RStudio with my own R script.
For example I create a new .Rnw file in RStudio with File > New File > R Sweave. It looks like this:
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<echo=F, results=tex>>=
library(xtable)
xtable(mtcars)
#
\end{document}
I inserted a chunk with alt+cmd+i and used the autocompletion to set the chunk options. Hence I assume I did everything with the default settings as RStudio assumes it to be. When I press Compile PDF everything works without problems. But when I execute:
knitr::knit2pdf("Sweave-test.Rnw")
I get an Error "It seems you are using the Sweave-specific syntax". Hence there are additional steps needed.
What I came up with so far is the following:
library(knitr)
tempfile1 <- tempfile(fileext=".Rnw")
Sweave2knitr(file = "input.Rnw", output = tempfile1)
tempfile2 <- tempfile(fileext=".tex")
knit(tempfile1, output=tempfile2)
tools::texi2pdf(tempfile2, clean=T)
system(paste("open", sub(".tex", ".pdf", basename(tempfile2))))
(The last line is OSX specific I think).
But I am curious to know what RStudio is doing exactly. I looked into the RStudio github page but am not sure where to find the command. Other Stackoverlow questions show that there are slight differences between what the button does and knit2pdf. It seems the question has been asked over and over again, also in relation to the Knit Html button. It might use functions from the knitr package, the markdown, the rmarkdown package, texi2pdf from the tools package, Sweave from the utils package or pandoc. I have no Idea...
Related question (that all got some rather vague answers):
Difference: "Compile PDF" button in RStudio vs. knit() and knit2pdf()
Difference between "Compile PDF" and knit2pdf
How to convert R Markdown to HTML? I.e., What does "Knit HTML" do in Rstudio 0.96?
What does “Knit HTML” do in Rstudio 0.98?
Compile .Rnw file with command
How to build Knitr document from the command line
Currently I am using RStudio 1.0.136.
I suspect it is just Sweave to turn a Rnw file into tex file and then calling pdflatex.exe to turn it from a tex file into a pdf file.
Sweave("Sweave-test.Rnw")
system("pdflatex.exe Sweave-test.tex")
And if you encountered a File Sweave.sty not found, you will need to add a path this file in your MiKTeX Options, please see here.
I'm using knitr to create a markdown file from Rmd and I have the following option set at the top of my .Rmd script to hide all results and plots:
```{r, echo=FALSE}
opts_chunk$set(results="hide", fig.show="hide")
```
When I hit the Knit HTML button in RStudio, this works - I get output without the results and figures. But if I run from the command line:
Rscript -e 'knitr::knit("myfile.Rmd")'
It appears the opts_chunk$set() line isn't read, and I get results and plots in my .md output. I've worked around the problem by specifying these options in the Rscript command:
Rscript -e 'library(knitr); opts_chunk$set(results="hide", fig.show="hide"); knit("myfile.Rmd")'
But I'd rather keep all the options read from the file I'm using rather than specified at the command line. How do I get the options read in the .Rmd file when kniting with Rscript at the command line?
Thanks.
I think you need to add
library("knitr")
to the chunk (you might want to set message=FALSE in the chunk options for that chunk).
The problem is that when you do
Rscript -e 'knitr::knit("myfile.Rmd")'
you're not actually attaching the knitr package, which means it isn't in the search path for functions, which means that R can't find the opts_chunk object.
Using knitr::opts_chunk might work too ...
as you suggested, so does Rscript -e 'library("knitr"); knit("myfile.Rmd")'
When you click the button in RStudio, RStudio automatically loads knitr in the environment in which it runs knit().
I'm trying to use knitr to inject R code and its output in pandoc/markdown documents. But I do not get knitr to inject the R output. I have tried decorating the R chunks with r and with{r}. Both doesn't work. Here my sample setup (with ```r):
First I show the command I issue, then I list the two files subsequently used by this command.
Here the command for the shell:
$ r CMD BATCH knitme.R
Content of knitme.R:
library("knitr")
pandoc("foo.md")
Content of foo.md:
# My knitr test
```r
1+1
```
Did this print *the result* as well?
Here a graph:
```r
plot(1:10)
```
And where is the graph?
After I ran the command I do get, as expected a new file foo.html. Here its content:
<h1 id="my-knitr-test">My knitr test</h1>
<pre class="sourceCode r"><code class="sourceCode r"><span class="dv">1+1</span></code></pre>
<p>Did this print <em>the result</em> as well?</p>
<p>Here a graph:</p>
<pre class="sourceCode r"><code class="sourceCode r">
<span class="kw">plot</span>(<span class="dv">1</span>:<span class="dv">10</span>)</code></pre>
<p>And where is the graph?</p>
This result shows that pandoc converted the input file foo.md, *but knitr did not inject the Output of the execeutes R code.
What do I miss? any help appreciated!
You should first call knit() on an R Markdown (*.Rmd) document, which produces a Markdown (*.md) document, and that is when you can run pandoc().
library(knitr)
knit('foo.Rmd')
pandoc('foo.md')
The R scripts in examples 084 and 088 as mentioned on the flaky website have illustrated how. Please also take a look at the Rmd documents to learn the syntax for R code chunks in R Markdown. If you still have 5 minutes, watch the video on the homepage, and I think all the confusion should be gone.