Do you know if in R markdown you can use go to the new page without distorting print(df) ie. data.frame's printing? For pdf generation the simple example is:
---
output: pdf_document
---
```{r, results='asis', echo=F, fig.width=8, fig.height=5}
for (year in 1:10) {
plot(1:year)
df<-data.frame(rep("costam", year),1:year)
cat("\n\n")
print(df)
cat("\\newpage")
}
```
Here, due to results='asis' the cat() command goes to new page; however, the data frame's are printed as strings.
It is helpful if you want to print chart together with its summary for a long... long... list of charts then can do it in a neat standardize way.
Related
I have a very large phylogenetic tree that I'd quite like to insert into a supplementary material I'm writing using Rmarkdown and knitr. I dislike splitting trees across pages and I doubt anybody would print this out anyway so I thought I'd just have a large page in the middle of the pdf I'm generating.
The question is how do I change page size for one page in an otherwise A4 document? I'm pretty new to knitr and I've found global paper size options but I'm struggling to find ways of setting up what would be the equivalent of sections in Word.
(Update) Hi does anybody else have a suggestion? I tried the pdfpages package but this seems to result in an equally small figure on a page the size of the pdf that is being pasted in i.e. if I make a 20in by 20in pdf figure then paste the page in using \includepdf then I get a 20in by 20in page with a much smaller figure on it (the same as the \eject example above). It seems like knitr or Latex is forcing the graphics to have a specific size regardless of page size. Any ideas? Here's a reproducible example:
---
title: "Test"
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \usepackage{pdfpages}
---
```{r setup, include=FALSE}
require(knitr)
knitr::opts_chunk$set(echo = FALSE,
warning=FALSE,
message=FALSE,
dev = 'pdf',
fig.align='center')
```
```{r mtcars, echo=FALSE}
library(ggplot2)
pdf("myplot.pdf", width=20, height=20)
ggplot(mtcars, aes(mpg, wt)) + geom_point()
dev.off()
```
#Here's some text on a normal page, the following page is bigger but has a tiny figure.
\newpage
\includepdf[fitpaper=true]{myplot.pdf}
You should be able to use \ejectpage like this:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
\eject \pdfpagewidth=20in \pdfpageheight=20in
```{r mtcars}
library(ggplot2)
ggplot(mtcars, aes(mpg, wt)) + geom_point()
```
\eject \pdfpagewidth=210mm \pdfpageheight=297mm
Back
(I can only remember the A4 height in mm for some reason)
I have just faced the same struggle when dealing with a large phylogenetic tree.
Based on hrbrmstr's answer, I came up with the snippet below.
The trick is to make knitr generate the figure but not include it in the intermediate .tex right away (hence fig.show="hide"). Then, because figure paths are predictable, you can insert it with some latex after the code chunk.
However, the new page height is not taken into account when positioning page numbers, so they tend to be printed over your image. I have tried to overcome this behavior multiple times, but in the end I simply turned them off with \thispagestyle{empty}.
---
output: pdf_document
---
```{r fig_name, fig.show="hide", fig.height=30, fig.width=7}
plot(1)
```
\clearpage
\thispagestyle{empty}
\pdfpageheight=33in
\begin{figure}[p]
\caption{This is your caption.}\label{fig:fig_name}
{\centering \includegraphics[height=30in, keepaspectratio]{main_files/figure-latex/fig_name-1} }
\end{figure}
\clearpage
\pdfpageheight=11in
When I'm inserting long captions and the like in my R chunk header, it'd be nice to be able to split the header across multiple lines.
Is there any easy way to do this?
E.g.:
```{r, echo=FALSE, warning=FALSE,
fig.cap="Here is my really long caption. It'd be nice to split this and other portions across lines"}
print(plot(x=runif(100),y=runif(100)))
```
No, you cannot insert line breaks in chunk options. From the manual:
Chunk options must be written in one line; no line breaks are allowed inside chunk options
However, if you desperately want neat formatting in the editor you could take a detour via an additional variable, but this inflates the code quite a lot:
---
output:
pdf_document:
fig_caption: yes
---
```{r}
mycaption <- "This is my
very long caption
that spans over
several lines.
(in the editor)"
```
```{r, fig.cap = mycaption}
plot(1)
```
With the option eval.after it is even possible to define mycaption within the chunk that uses it as option value:
---
output:
pdf_document:
fig_caption: yes
---
```{r}
library(knitr)
opts_knit$set(eval.after = "fig.cap")
```
```{r, fig.cap = mycaption}
mycaption <- "This is my
very long caption
that spans over
several lines.
(in the editor)"
plot(1)
```
(I assume that the question is about how the code looks (in the editor) not about a line break in the output.)
As of knitr v1.35 (https://github.com/yihui/knitr/releases/tag/v1.35) you are able to write chunk headers across multiple lines.
The syntax is slightly different from typical rmarkdown chunk syntax.
To achieve your goal, you extend the rmarkdown chunk header into the comments of the chunk.
You'd rewrite your example as follows:
```{r}
#| echo=FALSE, warning=FALSE,
#| fig.cap="Here is my really long caption. It'd be nice to split this and other portions across lines"
print(plot(x=runif(100),y=runif(100)))
```
Admittedly, that doesn't help handle having a very long figure caption, and the recommendation by CL to put the figure caption in a variable is still a good idea.
But, the new syntax also allows you to use yaml syntax to specify the chunk options, and yaml allows for multiline strings. So you could do the following:
```{r}
#| echo: false
#| warning: false
#| fig.cap: >
#| Here is my really long caption. It'd be nice to
#| split this and other portions across lines
print(plot(x=runif(100),y=runif(100)))
```
I am trying to break to a new page from a R markdown document that is calling a function to produce a set of graphics for each set of data (400+ pages in some cases). I am using LaTeX.
Any ideas? Here is a simple example that demonstrates some my challenge though it doesn't use a function to produce the output.
---
title: "Test Page Break"
output: pdf_document
---
```{r}
summary(cars)
cat("\\pagebreak")
print(cars)
```
Update #1:
My example didn't demostrate the use of a function within the code chunk. Here is a better example.
---
title: "Test Page Break"
output: pdf_document
---
```{r}
pr_w_pagebreak <- function() {
print(summary(cars))
cat("\\pagebreak")
print(cars)
}
pr_w_pagebreak()
```
The trouble you'll have with your chunks is that you want some results to appear as 'markup' and some to appear 'asis'. If you want to get the page break with the single block as you have it, you will need to do:
```{r, echo=-2}
summary(cars)
knitr::asis_output("\\pagebreak")
print(cars)
```
Otherwise, you'll have to do
```{r}
summary(cars)
```
\pagebreak
```{r}
print(cars)
```
And as Floo0 points out, you can get LaTeX code from the R chunks if you use results = 'asis', but you have to be careful because the following will not give you what you want.
```{r, results='asis'}
summary(cars)
cat("\\pagebreak")
print(cars)
````
I would like to edit a single rmarkdown (Rmd) document with a list of "problems", each followed by its solution. Each solution may contain the results of the R console, but also some explaining (markdown and LaTeX formatted) text. Besides, I would like use knitr in 2 versions: with and without the solutions, changing the source as less as possible, and compiling.
I know that I can use a logical variable in order to conditionally evaluate R code and show plots and R output, but I don't know how to show/hide blocks of (markdown and LaTeX) formatted text, unless I put all that text into R character vectors, which seems hard for keeping things clean and readable.
I found this old question,
Conditionally display a block of text in R Markdown
where the solution was given for simple short text, which was included as an argument of the R print() function.
This other old question,
insert portions of a markdown document inside another markdown document using knitr
was for having a father document and child documents which were conditionally compiled, but I don't want to slice my document into so many pieces.
You could use the asis engine to conditionally include/exclude arbitrary text in knitr, e.g.
```{asis, echo=FALSE}
Some arbitrary text.
1. item
2. item
Change echo=TRUE or FALSE to display/hide this chunk.
```
But I just discovered a bug in this engine and fixed it. Unless you use knitr >= 1.11.6, you can create a simple asis engine by yourself, e.g.
```{r setup, include=FALSE}
library(knitr)
knit_engines$set(asis = function(options) {
if (options$echo && options$eval) paste(options$code, collapse = '\n')
})
```
If you want to include inline R expressions in the text, you will have to knit the text, e.g.
```{r setup, include=FALSE}
library(knitr)
knit_engines$set(asis = function(options) {
if (options$echo && options$eval) knit_child(text = options$code)
})
```
There is a way to hide parts of the document (including text and chunks): to comment them out with html comment marks.
And comment marks can be generated by R in a block according to a variable that can be set at the beginning of the document.
```{r results='asis', echo=FALSE}
if (hide) {cat("<!---")}
```
```{r results='asis', echo=FALSE}
if (hide) {cat("-->")}
```
And just to show a complete working example, in the example below the middle section of the document can be shown or hidden by setting the hide variable to FALSE or TRUE. That might be useful in case there are several sections to hide or show at once - for example, solutions of course problems.
---
title: "Untitled"
date: "15/10/2020"
output:
word_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
hide <- TRUE #TRUE to comment out part of the document, FALSE to show.
```
## Start
Always shown.
```{r}
hide
```
```{r results='asis', echo=FALSE}
if (hide) {cat("<!---")}
```
## To hide or not to hide
To be hidden or shown according to *hide* variable.
```{r}
"Also to be hidden according to 'hide' variable"
hist(rnorm(10))
```
```{r results='asis', echo=FALSE}
if (hide) {cat("-->")}
```
<!--
Never shown.
-->
## End
Always shown.
Just a caveat: in html output the hidden parts are kept as comments and can be seen just by viewing the source. On the other hand, PDF (LaTex) and Word outputs ignore html comments and the hidden parts aren't included in the knitted documents.
Therefore, when the hidden parts are supposed to be somehow confidential (e.g. exam solutions) PDF or Word output should be used instead of html.
For those looking for a solution when knitting to pdf through LaTex, the answer from #Pere won't work for you (because LaTex doesn't understand the <!--- --> pair as indicating a comment).
Below is one possible workaround:
---
output:
pdf_document
---
\newcommand{\ignore}[1]{}
```{r echo=FALSE}
include <- TRUE
```
```{r results='asis', echo=FALSE}
if(!include){cat("\\ignore{")}
```
Included bla bla
```{r results='asis', echo=FALSE}
if(!include){cat("}")}
```
```{r echo=FALSE}
include <- FALSE
```
```{r results='asis', echo=FALSE}
if(!include){cat("\\ignore{")}
```
NOT Included bla bla
```{r results='asis', echo=FALSE}
if(!include){cat("}")}
```
I am using knitr and pandoc to write reports into word (we need to be able to circulate for comments using track changes etc).
It is working very well so far, but I have found that the plots are all coming out with captions at the bottom, and I don't want captions. While I could just delete them in the word doc, if I can stop them showing in the code it would be better.
So for the following code in markdown:
Test test test
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r fig.width=7, fig.height=6}
plot(cars)
```
I then run the following code in R:
library("knitr")
# Stackoverflow table test 1.html
knit2html("captiontest.rmd")
FILE <- "captiontest"
system(paste0("pandoc -o ", FILE, ".docx ", FILE, ".md"))
And the graph, in the word document, has the caption "plot of chunk unnamed-chunk-2"
I know I can change this caption, e.g. {r fig.width=7, fig.height=6, fig.cap='hello'}, but I thought that fig.cap=NULL would make it hidden. Instead it seems to make the whole plot disappear.
Are plots required to have a caption - do I just have to go through each word doc and delete them manually? Or is there a way to hide them?
Kind of a dirty trick, but:
You can set fig.cap="" on the chunk in question:
Test test test
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r fig.width=7, fig.height=6, fig.cap=""}
plot(cars)
```
Or, you can set fig.cap="" for all chunks at once in an initializing chunk at the beginning of your Rmd document:
Test test test
```{r options-chunk}
opts_chunk$set(fig.cap="")
```
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r fig.width=7, fig.height=6}
plot(cars)
```