I'm writing a document in R Markdown and use a Bibtex library for my citations. They work fine when I use them in the text, but give me trouble when I try to implement them in a figure caption.
The Bibtex reference is:
#book{TEST,
title = {R for Data Science},
author = {Test Person},
year = {2018},
}
How it works:
```{r carplot, echo=F, warning=F, fig.cap="This is a Test [#TEST]"}
plot(cars)
```
Ouput:
How it gets broken:
```{r carplot, echo=F, warning=F, fig.cap="This is a Test [#TEST]", fig.align="right"}
plot(cars)
```
Output:
I've tried other code chunk options like out.width=".7\\textwidth" and out.extra = 'trim = {0 1.1cm 0 0}, clip' which both cause the citation to break as well. Chunk options like echo=F and warning=F don't seem to be a problem though.
Any Ideas how i can put figure options in the code chunk options whithout it breaking my citation?
I have found a working solution for my problem, even though I still don't unterstand how it was caused in the first place. But for anybody looking for a workaround in the future, here is how I managed to do it:
(ref:CAP1) This is a Test [#TEST]
```{r carplot, echo=F, warning=F, fig.cap="(ref:CAP1)", fig.align="right"}
plot(cars)
```
Like this, the fig.align="right" does not seem to be a problem anymore.
Related
I just discovered the awesome Xaringan package, and I'd like my presentation to be as incremental as possible.
For instance, my introduction slide looks like this:
```{r intro1, echo=TRUE}
version$version.string #should give 3.6.1
```
--
```{r intro2, echo=TRUE}
class(iris)
```
--
```{r intro3, echo=TRUE}
dim(iris) #row, cols
```
--
```{r intro4, echo=TRUE}
colnames(iris)
```
Though, I find it tedious and not much readable to write it that way.
I tried this but it doesn't work:
```{r , echo=TRUE}
version$version.string
class(iris)
--
dim(iris)
colnames(iris)
```
Of course, this consider -- as code.
Is there a way to increment my slide from inside the code?
Yihui pretty much gave the answers in the comments but I elaborate here with one additional tweak which will ensure that the separator code (i.e. knitr::asis_output('\n--\n')) is not visible in the output (this is done by specifying the line number in which the separator appears in the chunk argument as below).
You can the separator code where you want to separate and just make sure the corresponding line number is not echo-ed.
---
output:
xaringan::moon_reader:
seal: false
---
```{r, echo = -4}
version$version.string
class(iris)
knitr::asis_output('\n--\n')
dim(iris)
colnames(iris)
```
I want my html file to show the code, but not the output of this chunk:
```{r echo=True, include=FALSE}
fun <- function(b)
{
for(a in b)
{print(a)
return(a * a)}
}
y <- fun(b)
```
When I run the code, i need the print to see the progress (it is quite a long function in reality).
But in the knitr file, I use the output in a further chunk, so I do not want to see it in this one (and there's no notion of progress, since the code has already been run).
This echo=True, include=FALSE here does not work: the whole thing is hidden (which is the normal behavior of include=FALSE).
What are the parameters I could use to hide the prints, but show my code?
As # J_F answered in the comments, using {r echo = T, results = 'hide'}.
I wanted to expand on their answer - there are great resources you can access to determine all possible options for your chunk and output display - I keep a printed copy at my desk!
You can find them either on the RStudio Website under Cheatsheets (look for the R Markdown cheatsheet and R Markdown Reference Guide) or, in RStudio, navigate to the "Help" tab, choose "Cheatsheets", and look for the same documents there.
Finally to set default chunk options, you can run (in your first chunk) something like the following code if you want most chunks to have the same behavior:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T,
results = "hide")
```
Later, you can modify the behavior of individual chunks like this, which will replace the default value for just the results option.
```{r analysis, results="markup"}
# code here
```
The results = 'hide' option doesn't prevent other messages to be printed.
To hide them, the following options are useful:
{r, error=FALSE}
{r, warning=FALSE}
{r, message=FALSE}
In every case, the corresponding warning, error or message will be printed to the console instead.
```{r eval=FALSE}
The document will display the code by default but will prevent the code block from being executed, and thus will also not display any results.
For muting library("name_of_library") codes, meanly just showing the codes, {r loadlib, echo=T, results='hide', message=F, warning=F} is great. And imho a better way than library(package, warn.conflicts=F, quietly=T)
For completely silencing the output, here what works for me
```{r error=FALSE, warning=FALSE, message=FALSE}
invisible({capture.output({
# Your code here
2 * 2
# etc etc
})})
```
The 5 measures used above are
error = FALSE
warning = FALSE
message = FALSE
invisible()
capture.output()
To hide warnings, you can also do
{r, warning=FALSE}
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
I want to have my graph output display on the next slide while the code chunk stays on the first. I am using the default ioslides in Rstudio. I would think that it would be some attribute of the code chunk but I can't figure out what it is.
---
output: ioslides_presentation
---
## Slide with Plot
```{r, echo=TRUE}
plot(cars)
```
Any one have any idea on how to do this in Rstudio?
I want to use this for educational purposes. First showing the code and than revealing the graph. Now I am stuck with doing double code chunks with echo=FALSE and TRUE and eval=FALSE and TRUE.
It's easier to run the code twice, once not evaluating the code, the second time not showing the code.
---
title: "Plot Separation"
output: ioslides_presentation
---
## Plot 1
```{r, eval = FALSE}
plot(1:10)
```
## Plot 2
```{r, echo = FALSE}
plot(1:10)
```
It also avoids the error when smaller is true.
To move a plot to the next slide, you need to add a horizontal rule ---- before it (see documentation). You can modify the default plot hook to do it:
```{r setup, include=FALSE}
library(knitr)
local({
hook_plot = knit_hooks$get('plot')
knit_hooks$set(plot = function(x, options) {
paste0('\n\n----\n\n', hook_plot(x, options))
})
})
```
If you want all your plots to appear on the next slide Yihui's answer is the way to go. But if you want some plots to appear on the same slide you may be better off doing it manually, similar to what you are already doing (and what Dario suggested). Except that I would strongly recommend the use of chunk references. That way you avoid the need to duplicate the code.
---
title: "Plot Separation"
output: ioslides_presentation
---
## Plot code
```{r cars_plot, echo = TRUE, eval = FALSE}
plot(cars)
```
## Plot display
```{r cars_plot, echo = FALSE, eval = TRUE}
```
```
I agree this would be desirable, but I'm finding ioslides to be buggy. Ioslides project started eith google, but was let go a while ago.
You may have better luck with beamer/home slides. But does require a LaTeX distribution to be installed.
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)
```