My converting the R script (.R) into an RMarkdown file (.rmd) in RStudio and then pressing “knit html” result in two output files (that is, .html and .md files). I am confronting 2 issues:
The html file shows that the title of the ggplot graph gets chopped. I have changed the original width of 11 to a new width of 15:
ggsave(file=outFile, width=15, height=7)
How would I resolve the issue ? And how would I convert the .md file into a PDF file ?
Your question is not exactly clear. I'm not sure, for example, why you are using ggsave() to begin with. You can directly create a "ggplot" image in your file to knit and set your figure width and height in your input file.
In a ".Rmd" file, your code might look something like:
```{r fig.width=7, fig.height=4, echo=FALSE}
library(ggplot2)
qplot(mpg, wt, data=mtcars)
```
The echo=FALSE will make it so that the code doesn't display, but the resulting plot will. The figure width and height have been set with the relevant arguments.
If you wanted to convert your resulting markdown file to PDF, I would recommend looking at Pandoc which will allow you to do something like the following to convert your file to a PDF:
pandoc infile.md -o outfile.pdf
Alternatively, you can use R Sweave instead of R Markdown in R/RStudio. For instance, if you create a new "Rnw" file in RStudio and paste the following in, you'll have the option to directly compile a PDF instead of compile an HTML.
\documentclass{article}
\begin{document}
<<fig.width=5, fig.height=3, echo=FALSE>>=
library(ggplot2)
qplot(mpg, wt, data=mtcars)
#
\end{document}
Related
I know R well but I start in Sweave.
I produce a report under Rstudio from several scripts:
graphic.R which defined a graphic created with ggplot2 called graph_1. I want to display it in the report.
script.Rnw containing Latex and Sweave code. I call graph_1 to display my graphic.
main.R script that executes the .Rnw code and therefore produces the report.
In graphic.R
library(ggplot2)
graph_1 <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(aes(color=Species, shape=Species))
In script.Rnw
\documentclass[french,12pt]{article}
\begin{document}
... no important...
\begin{figure}[h]
\begin{center}
<< fig=TRUE, echo=FALSE, height = 2.5>>=
graph_1
#
\caption{caption figure 1}
\label{graph_1}
\end{center}
\end{figure}
\end{document}
In main.R
Sweave("script.Rnw", encoding="UTF-8")
tools:::texi2dvi(file="script.tex"), pdf=TRUE)
Everything is OK : my report (.pdf) is generated in the folder. But I also have a file : 'script-001.pdf' containing my graphic which is added in the same folder.
I would like only the final report and not the intermediate file : so not the file 'script-001.pdf'.
Somebody know if it is possible ?
Thank you,
The figure file is required so that LaTeX can include it. But you can delete it safely after running Sweave and LaTeX.
I don't like my working directory to be littered with intermediate PDFs, too. So I have them end up in a folder called figures. With Sweave you can accomplish this by setting a suitable prefix:
\SweaveOpts{prefix.string=figures/fig}
You also need to create the figure folder yourself for it to work.
And a personal recommendation: You may want to have a look at knitr – it's a more powerful modern version of Sweave – in my opinion.
Creating a parameterized report with an rnw file. I am trying to reference specific figures from a code chunk that has multiple figures in it (generated by a loop through a list of figures). I know if there's a single figure I can reference it from the chunk label with \ref{fig:foo} as Yihui mentions in https://bookdown.org/yihui/bookdown/figures.html . But I cannot seem to reference specific figures in the chunk. I tried referencing the unique figure caption or the chunk as a whole but both give me ??. Is there a way to do this?
I searched this Dynamic LaTeX references in R comment with knitr and its linked questions but wasn't able to make it work.
Also in Figures captions and labels in knitr , the plots are combined into one big plot which bypasses the problem.
MVWE:
\documentclass{article}
\usepackage{float}
\usepackage{hyperref}
\usepackage{caption} % Needs to be after hyperref. jumps you to top of figure not to label.
\begin{document}
<<figures, fig.cap=c('fig1','fig2')>>=
library(knitr)
library(markdown)
library(rmarkdown)
library(ggplot2)
figure1 <- ggplot(mtcars) + geom_point(aes(x=mpg,y=cyl))
figure2 <- ggplot(mtcars) + geom_point(aes(x=drat,y=wt))
plots <- list(figure1,figure2)
plots
#
as we can see in \ref{fig:figures}
\end{document}
Just append a number to it:
as we can see in \ref{fig:figures1} and \ref{fig:figures2}
To figure this out, you should look at the .tex file, which contains
\begin{figure}
\includegraphics[width=\maxwidth]{figure/figures-1} \caption[fig1]{fig1}\label{fig:figures1}
\end{figure}
for the first one, and similar stuff for the other. The \label{fig:figures1} part is what your \ref needs to refer to.
There are a few questions about this already, but they are either unclear or provide solutions that don't work, perhaps because they are outdated:
Proper R Markdown Code Organization
How to source R Markdown file like `source('myfile.r')`?
http://yihui.name/knitr/demo/externalization/
Modularized code structure for large projects
R Markdown/Notebook is nice, but the way it's presented, there is typically a single file that has all the text and all the code chunks. I often have projects where such a single file structure is not a good setup. Instead, I use a single .R master file that loads the other .R files in order. I'd like to replicate this structure using R Notebook i.e. such that I have a single .Rmd file that I call the code from multiple .R files from.
The nice thing about working with a project this way is that it allows for the nice normal workflow with RStudio using the .R files but also the neat output from R Notebook/Markdown without duplicating the code.
Minimal example
This is simplified to make the example as small as possible. Two .R files and one master .Rmd file.
start.R
# libs --------------------------------------------------------------------
library(pacman)
p_load(dplyr, ggplot2)
#normally load a lot of packages here
# data --------------------------------------------------------------------
d = iris
#use iris for example, but normally would load data from file
# data manipulation tasks -------------------------------------------------
#some code here to extract useful info from the data
setosa = dplyr::filter(d, Species == "setosa")
plot.R
#setosa only
ggplot(setosa, aes(Sepal.Length)) +
geom_density()
#all together
ggplot(d, aes(Sepal.Length, color = Species)) +
geom_density()
And then the notebook file:
notebook.Rmd:
---
title: "R Notebook"
output:
html_document: default
html_notebook: default
---
First we load some packages and data and do slight transformation:
```{r start}
#a command here to load the code from start.R and display it
```
```{r plot}
#a command here to load the code from plot.R and display it
```
Desired output
The desired output is that which one gets from manually copying over the code from start.R and plot.R into the code chunks in notebook.Rmd. This looks like this (some missing due to lack of screen space):
Things I've tried
source
This loads the code, but does not display it. It just displays the source command:
knitr::read_chunk
This command was mentioned here, but actually it does the same as source as far as I can tell: it loads the code but displays nothing.
How do I get the desired output?
The solution is to use knitr's chunk option code. According to knitr docs:
code: (NULL; character) if provided, it will override the code in the
current chunk; this allows us to programmatically insert code into the
current chunk; e.g. a chunk option code =
capture.output(dump('fivenum', '')) will use the source code of the
function fivenum to replace the current chunk
No example is provided, however. It sounds like one has to feed it a character vector, so let's try readLines:
```{r start, code=readLines("start.R")}
```
```{r plot, code=readLines("start.R")}
```
This produces the desired output and thus allows for a modularized project structure.
Feeding it a file directly does not work (i.e. code="start.R"), but would be a nice enhancement.
For interoperability with R Notebooks, you can use knitr's read_chunk method as described above. In a notebook, you must call read_chunk in the setup chunk; since you can run notebook chunks in any order, this ensures that the external code will always be available.
Here's a minimal example of using read_chunk to bring code from an external R script into a notebook:
example.Rmd
```{r setup}
knitr::read_chunk("example.R")
```
```{r chunk}
```
example.R
## ---- chunk
1 + 1
When you execute the empty chunk in the notebook, code from the external file will be inserted, and the results displayed inline, as though the chunk contained that code.
As per my comment above, I use the here library to work with projects in folders:
```{ r setup, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
library(here)
insert <- function(filename){
readLines(here::here("massive_report_folder", filename))
}
```
and then each chunk looks like
```{ r setup, echo=FALSE, message=FALSE, warning=FALSE,
results='asis', code=insert("extra_file.R")}
```
I'm using the rmarkdown library to convert an Rmd file with code and plots to a PDF file, which I do with these commands:
library(rmarkdown)
render("test.Rmd", "pdf_document")
Here is my test.Rmb file:
Title
========================================================
This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the **Help** toolbar button for more details on using R Markdown).
When you click the **Knit HTML** button a web page 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:
# cars
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r}
plot(cars)
```
# mtcars
```{r}
summary(mtcars)
```
You can also embed plots, for example:
```{r}
plot(mtcars)
```
This is the HTML result from that Rmd file. When I make the PDF, rmarkdown adds page breaks by pushing plots that can't fit on the current page to the next one. This is the resultant PDF.
As you can see, the first plot can't fit on the first page, so it gets bumped to the second. That's fine, but then code that comes after the first plot is bumped back to the first page, which makes no sense to me. The only way I've figured out how to avoid this problem is to add manual $\pagebreak$ LaTeX commands all over the place to prevent this behavior.
Is there a better way to stop rmarkdown from moving code that comes after plots before those plots?
I am using knitr to create a set of lecture slides for a class using R. I would like to create a separate "companion file" that contains just has the R code (corresponding to the slides), so that students can execute the R code by cutting and pasting from the companion file.
For example, in the .Rmd file:
``` {r ....}
plot(x,y)
```
Then there would be a text file with:
plot(x,y)
But, have such a file be automatically produced from the .Rmd file?
Even better if the .Rmd file has such tags:
``` {r basic.plot ....}
plot(x,y)
```
Then, text file has:
# basic.plot
plot(x,y)
Can this be accomplished using knitr?
Yes, this is possible. What you're trying to do is called tangling, and it comes from the world of literate programming.
The knit function supports a tangle option that should be set to TRUE if you want to extract source code.