Hi knitr experts: I cannot for the life of me figure out how to compile the knitr Miminal Demo .Rnw document correctly. When I download and run Yihui's Minimal Demo of knitr .Rnw file (link), the document compiles but incorrectly handles the R chunks:
I changed nothing, just opened and compiled. Help welcome. As an aside, would very much appreciate tips on whether this is how I should be going about adding R code into a latex template that UT-Austin requires for dissertation publication. Thanks.
I can reproduce your PDF file when using Sweave instead of knitr for translating the .Rnw file. When switching to knitr via the RStudio options (c.f. https://support.rstudio.com/hc/en-us/articles/200532247), I get an error message and no PDF, which probably reproduces #r2evans' results in the comments. I also get a warning message that line 19 is Sweave specific (\SweaveOpts{concordance=TRUE}). Removing that line, the file processes with knitr correctly producing
Related
Everytime I click on knitr option in Rmarkdown, Rstudio starts installing knitr and Markdown and doesn't process my code. Please help me with a solution.
I am sure the codes written are correct. I have installed knitr, markdown and rmarkdown as well. But, I am not able to call knitr using "library(knitr)" function.
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.
The new version of Rstudio (0.98.932) has many new options including knit to PDF. An article describing the new version has a comment from Dave that says:
...after installing rstudio 0.98.932 I don’t get the little dropdown menu
for knit-pdf or word when editing a .Rmd file.
I'm having the same issue. A helpful response was posted:
It might be that either:
a) You are not running R 3.0 (which is required for RMarkdown v2); or
b) You have a custom markdown renderer defined (markdownToHTML
option). You can check for this by executing:
getOption(“rstudio.markdownToHTML”)
That solved Dave's problem (b), but when I run that command I get NULL
> getOption("rstudio.markdownToHTML")
NULL
Which I assume means I don't have a custom markdown renderer defined. (Previously I did in a cusomized .Rprofile, but I removed that.) My R version is 3.1.0.
Am I misunderstanding the getOption command? Could something else be tripping up my Rstudio?
I have just installed the new version of RStudio (0.98.932), which prompted me to upgrade a couple of packages (I can't remember which, although I see I have knitr 1.6, markdown 0.7 and rmarkdown 0.2.46). At first I had the same problem; there was only a single 'knit' option on the tool bar. I managed to get the ability to knit to .pdf by adding the following to the head of my .Rmd file.
---
title: "Sample Document"
output: pdf_document
---
Having done that, I now find I do have a drop down menu with options to knit to HTML, PDF and word. There's also a little gear icon that provides access to the R Markdown document options that wasn't there before. I have no idea what the problem was but it seems OK now!
Adding on #nicholas response
This is also why I create new .Rmd documents through the RStudio gui
File > New File > R markdown.
This way the YAML header is populated correctly.
I have identified an issue with knitr where the use of a central file to hold setup stuff like
\documentclass{article}
\usepackage[T1]{fontenc}
and defined styles, margins etc will cause errors when a knitr chunk has echo=TRUE set.
I've produced a MWE on github as it requires at least two files. https://github.com/stephlocke/knitrhighlighting
I've raised a request on the knitr github to accomodate \input commands but I'm hoping that someone can suggest the correct combination of code to be added to the setup file that would allow the code highlighting to go ahead in the interim.
I have tried adding
\usepackage{framed}
\usepackage{alltt}
but still get the following error
! LaTeX Error: Environment kframe undefined.
Thanks to Yihui (https://github.com/yihui/knitr/issues/765), moving the \documentclass{} into the .Rnw file instead of the boilerplate document is sufficient to ensure this works.
I am not sure if this type of question complies with the SO rules for well-defined questions ;) ... anyway:
I want to convert several R Sweave files (.Rnw) to R markdown files (.Rmd). Jeromy Anglin has posted on this matter here but there is no code supplied. I tried to use pandoc, but of course pandoc cannot handle the chunk tags and inline code tags correctly.
Consclusion: I guess I will have do write some code to parse my .Rnw files to prepare them for pandoc conversion. Thus my questions:
Is there a better way to go?
Does someone by chance have code
available that will do the job?
TIA
As #Karl commented, LaTeX --> markdown is not a trivial conversion as there are far more options and environments available in LaTeX compared to markdown. You are probably best off working with something like pandoc (see Demo #5). Basically, instead of doing
.Rnw --> .Rmd --> .md
you would do
.Rnw --> .tex --> .md
with pandoc. If you really want to go from .Rnw --> .Rmd, you may want to check out the pander package to write a function to extract code chunks, convert the remaining LaTeX content to markdown, and then re-insert the code chunks into the markdown document.