I saw this https://bookdown.org/yihui/rmarkdown-cookbook/fig-chunk.html and I'm wondering if there is something similar if a use a .rnw file that I compile in a pdf with knitr.
Thanks in advance for the help.
Yes, you can use the same trick. All you need to do is use the right syntax in your document, i.e., LaTeX in your case. Below is a translation of the example you mentioned from (R) Markdown to Rnw/LaTeX:
We generate a plot in this code chunk but do not show it:
<<cars-plot, dev='pdf', fig.show='hide'>>=
plot(cars)
#
After another paragraph, we introduce the plot:
\includegraphics{\Sexpr{knitr::fig_chunk('cars-plot', 'pdf')}}
Related
I am searching for a way to produce a standalone HTML document from a chunk in Rmarkdown.
My example is this:
I have an Rmarkdown document for my analysis.
In one chunk of code, I produce interactive plotly plots that are quit long to load when I open the HTML.
I am looking for an option that would create another HTML document for this particular chunk and put a link to it in its place in the master HTML document.
I am sure I could manage to do something like that with a bit of tweaking with another script, but I would like to know if there is not a simpler option first.
Thanks
Yes, you can nest documents using knitr child documents. Put the chunk you want to isolate in its own Rmd (say, child.Rmd), then use this syntax to insert it in the master document:
```{r child='child.Rmd'}
```
I am working on a document about R software, which is currently stored in a Rnw file to eventually process it with Sweave. The document contains several R code chunks, of the usual form:
<<>>=
R Code
#
Next to some code lines, I would like to add specific comments, which I would like to be displayed like usual LaTeX text (so that they are easily recognizable as comments). Is there a way to display R code chunks and corresponding comments next to each other when using sweave or knitR?
Thank you for your help!
If you want the to be formatted like code (I assume that is what you mean) you can use the following code:
\begin{filecontents*}{my_r_code_test.r}
R code
#
|\includegraphics[width=0.5\textwidth]{example-image}|
\end{filecontents*}
\documentclass{article}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{listings,mdframed}
\lstset{
language=R,
backgroundcolor=\color{black!5}, % set backgroundcolor
basicstyle=\footnotesize\ttfamily,% basic font setting
columns=fullflexible,
}
\begin{document}
\begin{mdframed}[backgroundcolor=black!5,linewidth=0pt,%
innerleftmargin=0pt,innertopmargin=0pt,innerbottommargin=0pt]
\lstinputlisting[escapeinside=||]{my_r_code_test.r}
\end{mdframed}
\end{document}
Which produces
at the top of the page.
Hope this helps!
I have a .Rpres file in RStudio. I would like to include code, but not have it run (I am only showing the code to explain how it works). Is it possible to accomplish this (and ensure that it will not produce errors, because it is not running)?
Have you tried eval=FALSE in the knitr code chunk options? e.g.:
```{r eval=FALSE}
print("Don't run me")
```
{r, eval=F, echo=T} will include the R source code in the output file while it is not evaluated
Posting for anyone who may come across this like I have. I've found that for small examples (if you don't want to use chunks), you can also just use back ticks like you would with regular markdown inline, but just don't add the "r" at the beginning:
`plot(cars)`
Will print the code itself, but will not print the plot.
I am writing a document with knitr (Rnw to be processed to PDF) that will be printed in black&white. I still would like the code chunks to have syntax highlighting.
Is it possible, and how, to modify default syntax highlighting colors to use some shades of gray etc.?
PS. I do not want to switch to LaTeX 'listings' package.
As suggested by #Roland, knitr themes (?knit_theme) do the job. In particular, the print theme seems to suite black-and-white printing best. A gallery of all built-in knitr themes can be found here http://animation.r-forge.r-project.org/knitr/
In particular, to set print theme in my Rnw document put this in the initial R code chunk:
opts_knit$set( out.format="latex" )
knit_theme$set("print")
I am using knit()and markdownToHTML() to automatically generate reports.
The issue is that I am not outputting plots when using these commands. However, when I use RStudio's Knit HTML button, the plots get generated. When I then use my own knit/markdown function, it suddenly outputs the plot. When I switch to another document and knit that one, the old plot appears.
Example:
```{r figA, result='asis', echo=TRUE, dpi=300, out.width="600px",
fig=TRUE, fig.align='center', fig.path="figure/"}
plot(1:10)
```
Using commands:
knit(rmd, md, quiet=TRUE)
markdownToHTML(md, html, stylesheet=style)
So I guess there are 2 questions, depending on how you want to approach it:
What magic is going on in Rstudio's Knit HTML?
How can I produce/include without depending on RStudio's Knit HTML button?
The only issue I see here is that this doesn't work when you have the chunk options {...} spanning two lines. If it's all on one line, it works fine. Am I missing something?
See how this is not allowed under knitr in the documentation:
Chunk options must be written in one line; no line breaks are allowed inside chunk options;
RStudio must handle linebreaks in a non-standard way.
This is really embarrassing, I really thought I read the documentation carefully:
include: (TRUE; logical) whether to include the chunk output in the
final output document; if include=FALSE, nothing will be written into
the output document, but the code is still evaluated and plot files
are generated if there are any plots in the chunk, so you can manually
insert figures; note this is the only chunk option that is not cached,
i.e., changing it will not invalidate the cache
Simply adding {..., include=TRUE} did the trick. I would say it would be a pretty sensible default though.