I have 6 files (for which I locally have the .Rmd code) posted to http://rpubs.com/sshleifer using knitr.
I want to create one webpage that concatenates all these webpages into one webpage, without copying and pasting the .Rmd code into one document (it will crash Rstudio).
I have tried making a new RMD file containing the chunk:
```{r learning, echo=FALSE, warning=FALSE, results='asis', message=FALSE,cache=TRUE}
knit('learning.Rmd')
```
but this only displays the progress of the knitting of Learning.Rmd at the new location, rather than the actual html created.
Thanks!
Related
After updating my R installation on my Mac with a couple of packages (most recently, RODBC), when I knit .Rmd files to pdf, I get error such as the one below.
convert: no decode delegate for this image format `images.trial/pressure-1.png'.
convert: missing an image filename
It occurs on .Rmd files that I've written on a Windows machine, and on a standard .Rmd file that I created from the base template (File>New File>R markdown...), with an initial r chunk that I've used to set knitr chunk options:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,fig.path="images/",dev="png")
```
If I exclude that knitr::opts_chunk code when I knit to pdf, I get no errors; if I knit to html with that knitr:opts_chunk intact, I get no errors.
I've run this on .Rmd files that have worked without errors in the past and on .Rmd files that were created directly from the template.
I opened a new R Markdown file in R studio and got the default small working example.
---
title: "test"
author: "Katharina Zweig"
date: "30. Januar 2016"
output: html_document
---
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}
summary(cars)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
```
Note that the `echo = FALSE` parameter was added to the code chunk to
prevent printing of the R code that generated the plot.
It says, you only need to press the knit button to create an HTML
containg the text, the code and the results of the code. I got some long
error logs that were hardly helpful. Neither did changing the output to
PDF and Word - same result: text was there, code was there, no results of
running the code. By producing the output, the original file vanished.
What is wrong?
When the knit button is used on a file not yet saved, it asks you under which name to save it. The file needs to be saved as an Rmd file - just give no extension and R-Studio will do it right. Then, the file does not vanish and the resulting document contains the results of the r commands. I thought it asked where to save the output and gave it the extension of the output file, i.e., either myfile.html / myfile.pdf / myfile.doc.
In the chunk option try this:
{r, results='asis'}
summary(cars)
You can also embed plots, for example:
{r, echo=FALSE, results='asis'}
plot(cars)
The results = 'asis' command should output the tables and graphs if not please let me know.
Is it possible to include or display an .r script in the output of .rmd file?
Important - just want to display the .r file!
Tried source(filename.r); source does not display it.
Any ideas?
**knitr Global Options**
```{r echo=TRUE}
knitr::opts_chunk$set(tidy=FALSE, fig.path='figures/')
```
**Load Libraries**
```{r echo=TRUE}
library(dplyr)
```
```{r echo=TRUE, include=TRUE}
source("external.R")
# the complete source code of the .r file should be displayed here
# possible?
```
What would be the use-case for such a requirement?
Creating .Rmd helps with documentation. In fact all my documentation is created using .Rmd.
There are .R scripts which take a long time to run (processing large data). In such a case working with .Rmd is not practical. Prefer to work with .R scripts.
If the source code of the .R can be "included & displayed" in the .Rmd would be wonderful for documentation purpose.
For this particular case, there is a simple solution. That is, you can assign source code to the chunk option code, then knitr will just take your source code as if it were written in the code chunk, e.g.
```{r, code = readLines('external.R')}
```
Alternatively and equivalently, you can use the file option:
```{r, file = 'external.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?
Let's say I have a project directory called testknit (and I do, see github for MRE), and inside this I have several subdirectories, including scripts where I keep .R and .rmd files.
In RStudio, I create a project and select this testknit directory so that when I open the project, the working directory is mypath/testknit.
Inside testknit/scripts I have a master.R file. If I want to source a file called testsource1.R, which is also in testknit/scripts, I can run source("scripts/testsource1.R") from within master.R.
library(knitr)
getwd()
# [1] "mypath/testknit"
source("scripts/testsource1.R")
So far so good.
But let's say I also want to knit a .rmd file called test.rmd that is located in testknit/scripts. I can run knit("scripts/test.rmd") from master.R.
My test.rmd file does the following:
```{r setup}
library(knitr)
opts_knit$set(root.dir='../')
```
```{r option1}
source("scripts/testsource2.R")
```
```{r option2}
source("testsource2.R")
```
Since test.rmd exists within testknit/scripts, I specify opts_knit$set(root.dir='../') in the first chunk so knitr knows that my root directory is really one level up.
When I open test.rmd in RStudio and click knit HTML, predictably, the option1 chunk works and the option2 chunk does not.
But when I try to knit test.rmd by running knit("scripts/test.rmd") from master.R instead of knitting from within the .rmd file, neither chunk option works. Both return an error that there is no file by that name.
What am I doing wrong? Why can't R find testsource2.R when knitting the .rmd file from the master .R?
See github link above for reproducible example.
Update:
As I noted below in the comments, I tried adding wd <- getwd() just before opts_knit$set and changed (root.dir='../') to (root.dir=wd). So when I run knit("scripts/test.rmd") from master.R, the option2 chunk runs because the wd I added gets set to mypath/testknit/scripts. But if i open the .rmd file and run all chunks, wd is set to the root directory, mypath/testknit, and the option1 chunk runs.
I need the working directory to remain the project root. This does not seem like an elegant solution to me, but changing:
```{r setup}
library(knitr)
opts_knit$set(root.dir='../')
```
to
```{r setup}
library(knitr)
wd <- ifelse(basename(getwd())=="scripts",
gsub("/scripts", "", getwd()),
getwd())
opts_knit$set(root.dir=wd)
```
lets me run all chunks when in the .rmd file or knit("scripts/test.rmd") from master.R. It works, but it feels like I am taking the wrong approach.
#Yihui: Perhaps you can make ../ an absolute path using normalizePath('../'). A relative working directory can be confusing to reason about (at least my head hurts after I read too many levels of relative paths :). BTW, when you Knit HTML in RStudio, RStudio first changes the working directory to the input Rmd file.
Me: yes! using just opts_knit$set(root.dir=normalizePath('../')) works for knitting the .rmd file from master.R and knitting to html or running all chunks from within the .rmd. I updated the github example. test-b.rmd now shows this. thanks!