<<newChunk,echo=FALSE,comment=NA,background=NA>>=
x <- "\\includegraphics[width=\\maxwidth]{figure/Kompetenz1.pdf}"
cat(x)
#
If I run this r code in my .Rnw file I will get the following output in my .tex file:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{1, 1, 1}\color{fgcolor}\begin{kframe}
\begin{verbatim}
\includegraphics[width=\maxwidth]{figure/Kompetenz1.pdf}
\end{verbatim}
\end{kframe}
\end{knitrout}
How can I get just the \includegraphics{...} without any environment in my .tex file?
I am not sure why you are using Knitr in the first place. It looks like your just trying to include a PDF in your latex document. To do that I would use the command:
\includepdf[pages=-]{figure/Kompetenz1.pdf}
or
\includegraphics[width=\maxwidth]{figure/Kompetenz1.pdf}
In your Rnw file. No need for a Knitr chunk here.
The answer from #baptiste was right.
Why I was using a Knitr chunk was to loop over some values. Here is my example code:
<<Ueberblick, echo = FALSE, results = 'asis'>>=
for(i in 1:nrow(Kompetenzen)){
cat(paste("\\includegraphics[width=\\maxwidth]figure/Kompetenz.pdf}",i," \\newline ",sep=""))
}
#
Thanks for the help!
Related
I just found this awesome technique to put the code used in the .Rmd file in the appendix (of that same file).
However, I am using R Sweave and not R Markdown and I would like to know if there exists a similar way to put all the code at the end in a unique chunk. The code to do that in Markdown does not work in Sweave. I precise that, unlike this post, I do not have a separate .R file where the calculations are made. Everything is done in the .Rnw file.
Does anybody know how to do it?
Edit : a reproducible example
\documentclass[11pt, twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<reg2, echo=FALSE, print=FALSE>>=
head(mtcars)
#
<<reg3, echo=FALSE, print=FALSE>>=
head(iris)
#
\section*{Appendix}
% the place where I could like to put the whole code
\end{document}
This chunk works to include the code:
<<echo=FALSE, eval=TRUE>>=
filename <- tempfile(fileext=".R")
Stangle("test.Rnw", output = filename, quiet = TRUE)
cat(readLines(filename), sep = "\n")
#
When I include that in your example file, I see this:
I think it's possible to modify the format a bit; see ?Rtangle for some details. Similar things are possible with knitr, but it's more flexible. I suspect the best method would be similar to the one you found for RMarkdown.
I have a Rnw file a.Rnw with the following contents:
\documentclass{beamer}
\begin{document}
\begin{frame}[fragile]
<<>>=
1+1
#
\end{frame}
\end{document}
I produce a pdf by
Rscript -e 'knitr::knit("a.Rnw")'
pdflatex a.tex
The output in a.pdf looks like this:
How do I get the output to look like this:
R> 1+1
## [1] 2
That is, how do I put R> in front of the R code and remove the blank line between code and output?
To show R> in front of all R commands, I set the R prompt using options and tell knitr to show the prompt using opts_chunk (code at bottom of answer).
Getting rid of the new line is a bit trickier because the R code and R output in the generated tex file looks like this:
\begin{alltt}
\hlstd{R> }\hlnum{1}\hlopt{+}\hlnum{1}
\end{alltt}
\begin{verbatim}
## [1] 2
\end{verbatim}
So the newline between R code and its output is not generated explicitly by knitr, but is due to a new paragraph being started between \end{alltt} and \begin{verbatim}. The verbatim environment adds above and below it the current value of \topsep (see here). So I patch the knitrout environment such that this variable is locally set to 0pt. Here is the new version of a.Rnw:
\documentclass{beamer}
% reduce whitespace between R code and R output
\let\oldknitrout\knitrout
\renewenvironment{knitrout}{
\begin{oldknitrout}
\topsep=0pt
}{
\end{oldknitrout}
}
% show R> prompt before R commands
<<r setup, echo=FALSE>>=
options(prompt='R> ')
knitr::opts_chunk$set(prompt=TRUE)
#
\begin{document}
\begin{frame}[fragile]
<<>>=
1+1
#
\end{frame}
\end{document}
and the output looks like this:
I want to include help of R functions into .Rnw document using help_console function from noamtools package which can be installed from github using the following comand:
devtools::install_github('noamross/noamtools', build_vignettes = TRUE)
Minimum working example is given below:
\documentclass{article}
\begin{document}
<< label=Test, echo=FALSE >>=
library(noamtools)
# help_console(topic="mean", format = "latex")
help_console(topic="mean", format = "Rd")
#
\end{document}
I'm not getting the correct output.
The solution is to include the line
\usepackage{Rd}
in the beginning of the .Rnw file and then use
help_console(topic="mean", format="latex")
For example, put the following in a file, say test.Rnw and compile to produce documentation about the function mean. Note that results=tex ensures that the Latex code produced by R is actually created like Latex code.
% devtools::install_github('noamross/noamtools', build_vignettes = TRUE)
\documentclass{book}
\usepackage{Rd} % Rstudio will be able to find this .sty file
\begin{document}
<<label=Test, results=tex>>=
library(noamtools)
help_console(topic="mean", format = "latex")
#
\end{document}
Which produces a pdf which looks like this,
Looking here helped me realize that the Rd.sty file needed to be included. Running the following code
pack <- "ggplot2"
path <- find.package(pack)
system(paste(shQuote(file.path(R.home("bin"), "R")),
"CMD", "Rd2pdf --no-clean", shQuote(path)))
# .Rd2xxxx directory created
# cd .Rd2xxxx
You can look inside the created directory named something like .Rd2xxxx to find Rd2.tex which shows what is put in the Latex documentation files which R creates.
I have a R Markdown file that has my notes and chunks of code. I now want to write a R Sweave(Knitr) document to publish a paper using those chunks. I do not want to cut and paste the chunks, I rather call them directly. That way if I update the chunks, I don't have to do it in two places. It seems like it would be simple enough, but I can not figure it out. My code is as follows, test.rmd is my mark down document, foo is the chunk in the rmd file.
Test.rnw
<<Setup>>===
read_chunk('test.rmd')
#
<<foo>>==
#
Test.rmd
```{r foo, echo=TRUE}
print(summary(cars))
```
I would expect a summary of cars to be displayed in the output of the compilation of test.rnw into a PDF. But I don't. Any help is greatly appreciated.
read_chunk reads chunks from r script so call purl before read_chunk:
<<Setup>>=
knit_patterns$set(all_patterns[["md"]])
purl("test.Rmd")
knit_patterns$set(all_patterns[["rnw"]])
read_chunk("test.R")
#
<<foo>>=
#
How to have R code (R chunk) inline with my text?
Example:
Please install the package by using install.packages("ISwR")
whereby, install.packages("ISwR") is automatically highlighted as R chunk using knitr?
In other words, I would like to have R code at the same line with my text.
Following a suggestion from Yihui,
\documentclass{article}
<<setup, include=FALSE>>=
knit_hooks$set(inline = function(x) {
if (is.numeric(x)) return(knitr:::format_sci(x, 'latex'))
knitr:::hi_latex(x)
})
#
\begin{document}
Please install the package by using \Sexpr{'install.packages("ISwR")'}.
\end{document}
You can use the 3 backticks ``` if you want the encapsulated code NOT to be executed inline . E.g.
There were ```nrow(cars)``` cars studied