Rendering rMarkdown File in a Markdown file - render

If I were to use rmarkdown::render(myFile) within an rMarkdown file. How would I get the first rMarkdown file to display the rendered second rMarkdown file?
At present I've got:
{r other, results="asis"}
myFile <- "SecondFile.Rmd"
rmarkdown::render(myFile)
But this is just outputting the console text generated while the markdown is being knit, whereas obviously I want the HTML result of the knit.

Okay, after some fiddling the best answer I can come up with is this, but I'm more than happy for someone to correct me if there is a more 'proper' way.
```{r setup}
library(htmltools)
```
##Title
```{r generate, include=FALSE}
rmarkdown::render(myFile)
```
```{r print}
includeHTML(myFile)
```

Related

In R markdown in RStudio, how can I prevent the source code from running off a pdf page when bash language is used?

I'm writing a technical book using Bookdown and RStudio. My code chunks are mainly using bash. Everything works fine except when I export the book to pdf, then the source code is partially out of the "box" and even the page if this is long enough. I have read a lot of solutions when r language is used, but none of these solutions works when bash language is used.
Here is my code at the beginning of the .Rmd file:
```{r, global_options, include=FALSE}
knitr::opts_chunk$set(message=FALSE, eval=FALSE,
tidy.opts=list(width.cutoff=60), tidy=TRUE)
```
And then when I write the code chunk:
```{bash}
mongodump --uri="mongodb+srv://cluster0.rh6qzzz.mongodb.net/" --db sample_mflix --username my_username
```
The outputs were produced as shown below (see the end of line):
I would like to avoid this, but I have not found the solution.

Python chunk echos in r-markdown, but not r-notebook

I like to take notes inside of r-notebooks and have recently been trying to incorporate python code chunks into some of my documents. I have no problems executing python chunks and displaying the output, but I get different behavior depending on whether I'm using an R-notebook or an R-markdown document. While R-markdown will echo the python code and display the output, R-notebook will only display the output. I have tried explicitly stating echo=T in the chunk, bit it does not change the outcome. Any thoughts on how to get the appropriate behavior in R-notebooks?
EDIT: Will also add that this behavior in notebooks only appears to happen when the code chunk has a printed output. A chunk that does not print will echo code correctly.
Below is an example:
R-notebook example
---
title: "R Notebook"
output: html_notebook
---
Example R-notebook
```{r setup, include=FALSE}
library(knitr)
library(reticulate)
knitr::knit_engines$set(python = reticulate::eng_python)
```
```{python}
print("Hello world")
```
R-markdown example
---
title: "R Notebook"
output: html_document
---
Example R-notebook
```{r setup, include=FALSE}
library(knitr)
library(reticulate)
knitr::knit_engines$set(python = reticulate::eng_python)
```
```{python}
print("Hello world")
```

How do I get amsmath to work in RMarkdown when knitting to PDF?

In RMarkdown, I have a document I want to knit to pdf. The document has equations for which I need automatic numbering. I had been using the $$ 1+1=2 \tag{1} $$ convention to write equations, but now want to switch to the \begin{equation} 1+1=2 \eq:this_eq \end{equation} convention so that I can have automatic numbering and easy cross referencing of the equations. The few online resources I've found make it seem like this should be fairly straightforward to do. For example here or here. However, I have run into no end of heartbreak in attempting to do it.
I am using version 3.4.3 with RStudio, the tinytex distribution, and have installed bookdown (which I am still not sure is really necessary to achieve my goal here). Here is a repex:
---
title: This title
author: "This guy"
date: "This date"
header-includes:
- \usepackage{amsmath}
output:
pdf_document:
toc: yes
toc_depth: '4'
df_print: kable
fig_caption: yes
latex_engine: xelatex
mainfont: Calibri Light
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Introduction
blah blah...
\begin{equation}
S = X \bar{P}
(\#eq:signals)
\end{equation}
## Later on
blah blah \#ref(eq:signals)
When I try to "knit to PDF" I am running into the error
! Package mathspec Error: `amsmath' must be loaded earlier than `mathspec'.
which has been reported as a bug and "fixed" here, but I am unable to understand the fix or to follow its instructions. What I'm asking for is a set of really clear steps that will get me to where I can run the repex above without incident.
Some things I've tried:
The same error occurs when I replace pdf_document with bookdown::pdf_document2. Or when I remove
header-includes:
- \usepackage{amsmath}
and instead put
includes:
in_header: preamble.tex
after the line latex_engine: xelatex, where "preamble.tex" is a notepad file containing the line \usepackage{amsmath}
The comments in this other SO post seem to suggest that it is not even necessary to say anything about amsmath in the YAML options, which confuses me even more. When I remove any mention of amsmath from the YAML options, I get errors saying that the mathjax script is not recognized, for example:
! Package amsmath Error: \bar allowed only in math mode.
When I try your example, the equation is not labelled successfully.
Then I replace the output setting as bookdown::pdf_book. It works.
---
title: This title
author: "This guy"
date: "This date"
output:
bookdown::pdf_book
mainfont: Calibri Light
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Introduction
blah blah...
\begin{equation}
S = X \bar{P}
(\#eq:signals)
\end{equation}
## Later on
blah blah \#ref(eq:signals)
For my problem, the solution came down to me just changing my latex engine from xelatex to lualatex. My document knit correctly and numbered my equations. For some reason, everywhere I looked they say one should use xelatex. Also I had header-includes: - \usepackage{amsmath} in the YAML header.
I was facing this issue. Thanks for the suggestions above. Here is what worked for me in the output and header-includes parts of the preamble:
output:
bookdown::pdf_book:
latex_engine: lualatex
header-includes:
- \usepackage{amsmath}

Use custom citation style in markdown using YAML header

I am trying to use a custom citation style in a markdown file, but the citation uses the default (Chicago) style each time I knit. I have tried changing the output format from a JS reveal presentation to an HTML document to a PDF document, but it still does not work. I am using the knitcitations package to cite using the document's DOI, and the bibliography() function to write the bibliography. I have also tried using the apa.csl style found on Zotero, yet the citation is still done in the default styple. The apa.csl file is stored in the same folder as the file that I am trying to use citations in, as is the newbiblio.bib file, in which I have stored the bibliographical information for the item I want to cite.
Below is my markdown code:
---
title: "htmlcitetest"
citation_package: natbib
csl: "apa.csl"
output:
pdf_document:
pandoc_args: ["--natbib"]
biblio-style: unsrt
bibliography: newbiblio.bib
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(bibtex)
library(knitcitations)
options("citation_format" = "pandoc")
library(RefManageR)
cleanbib()
```
## R Markdown
- This is a citation [^1]
[^1]: `r citet("10.1098/rspb.2013.1372")`
```{r, message=FALSE}
bibliography()
```
This link (http://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html)
says that I should be able to format my YAML header like this:
---
title: "Sample Document"
output: html_document
bibliography: newbiblio.bib
csl: apa.csl
---
However, when I do that, the file knits to a markdown (.md) file, but it is not processed into the output. I recieve this error:
pandoc-citeproc: 23:3-23:10: Expected end element for: Name {nameLocalName = "category", nameNamespace = Just "http://purl.org/net/xbiblio/csl", namePrefix = Nothing}, but received: EventEndElement (Name {nameLocalName = "info", nameNamespace = Just "http://purl.org/net/xbiblio/csl", namePrefix = Nothing})
pandoc: Error running filter /Applications/RStudio.app/Contents/MacOS/pandoc/pandoc-citeproc
Filter returned error status 1
Error: pandoc document conversion failed with error 83
Execution halted
The contents of my .bib file are:
#Article{Boettiger_2013,
doi = {10.1098/rspb.2013.1372},
url = {http://dx.doi.org/10.1098/rspb.2013.1372},
year = {2013},
month = {jul},
publisher = {The Royal Society},
volume = {280},
number = {1766},
pages = {20131372--20131372},
author = {C. Boettiger and A. Hastings},
title = {No early warning signals for stochastic transitions: insights from large deviation theory},
journal = {Proceedings of the Royal Society B: Biological Sciences},
}
I also do not understand why the biblio-style option in the YAML header does not to do anything. Essentially, all I need is a way to use a custom citation style I have already made with a markdown document. Any help would be greatly appreciated!
Without a reproducible example, it is hard to know exactly what is happening, but it looks like you are mixing two different configurations.
Method 1: Specifying a custom CSL file
The method of using a CSL file only works if you are using pandoc-citeproc. For example, I have downloaded the IEEE style, and saved it in the same directory as my RMarkdown file as ieee.csl. This MWE builds a separate bibliography file:
---
output: pdf_document
bibliography: test.bib
csl: ieee.csl
---
```{r}
knitr::write_bib(x = c("knitr", "rmarkdown") , file = "test.bib")
```
Some ref [#R-knitr]
Some again [#R-knitr]
Another ref [#R-rmarkdown]
# References
Method 2: Specifying styles in Natbib
If you want to use natbib to build the citations and bibliography, you have to use the biblio-style option. This following example should work without downloading anything:
---
output:
pdf_document:
citation_package: natbib
bibliography: test.bib
biblio-style: humannat
---
```{r}
knitr::write_bib(x = c("knitr", "rmarkdown") , file = "test.bib")
```
Some ref [#R-knitr]
Another ref [#R-rmarkdown]
# References
Unless you have a particular reason, I would probably go down the route of using pandoc-citeproc and a csl file. It integrates well with the RMarkdown world. Using Natbib just gets a bit more confusing, and from my experience is more prone to throwing errors.
I was having the same error message you are having (pandoc-citeproc: ParseError {errorContexts = [], errorMessage = "Failed reading: takeWhile1"...). And I discovered the solution conveying information from two other internet forums. Basically, the problem was that I had downloaded my custom citation "csl" file from GitLab in a way that it was an HTML version of the original raw xml file. I had to download the raw xml file instead. When I googled the error, I saw that many people was having the same issue.
Within Git-page, where you downloaded your "csl" file from, instead of clicking in the download button, you should right click in the "open raw" button, and then, "save link as". Then it should work.
In the below image, instead of clicking in button "1", right click in button "2" and save the link as:

pandoc document conversion failed with error 43 : pdflatex: The memory dump file could not be found

RStudio : 0.98.994
OS: Microsoft Windows 7 Ultimate Edition, 64-bit Service Pack 1
MiKTeX: 2.9.4503
Hi,
I get the following error when I try to knit a PDF document.
pandoc.exe: Error producing PDF from TeX source.
This is pdfTeX, Version 3.1415926-1.40.11 (MiKTeX 2.9)
pdflatex: The memory dump file could not be found.
pdflatex: Data: pdflatex.fmt
I also tried devtools::install_github('rstudio/rmarkdown') but was still getting an error when I added 'fig.align='center' to a ggplot2 plot in my document. It would work as HTML, but not as PDF.
After seeing isomorphismes's post I clicked on the gear symbol next to the knit PDF button, then under the advanced tab I changed the LaTeX Engine to xelatex. After that I no longer received the error message and my PDF document was created without problems.
Thank you.
I found the answer here: http://rmarkdown.rstudio.com/tufte_handout_format.html#comment-1582377678
The problem is that you need to add \usepackage[utf8]{inputnc} to the preamble of the tufte-handout.tex file in the rmarkdown package.
This was fixed here: https://github.com/rstudio/rmarkdown/commit/484d5b8e903e0e0c75c82f707efa35f9fd9a52b0
To update your rmarkdown package, you can use directly in the RStudio command line
devtools::install_github("rstudio/rmarkdown")
None of the above worked for me when knitting to PDF (and I wanted to keep the scientific notation). The problem was that latex code was generated that included "\times" without the necessary bracketing by $. In the markdown I simply bracketed the inline R code with $'s, like so:
$p = `r signif(cor.HF$p.value, 2)`$
Voila!
happy to share with you my solution.
---
title: "Untitled"
author: "-----"
date: "21/6/2017"
output:
pdf_document:
latex_engine: xelatex
---
I was able to fix it in my case. I experienced that error when generate PDF from Rmd if I added float values into a text that R tried to display as a scientific notation. For example, instead of "520274.72" it tried to add text "5.2027472 e10-5" which leads to latex code \textbf{5.2027472\times 10\^{}{5}} that was not compiling. I fixed it by wrapping it with format(....,scientific=FALSE).
replace
r round(txn_pd,2)
with
r format(round(txn_pd,2),scientific=FALSE)
I had the same problem and devtools::install_github('rstudio/rmarkdown') didn't work for me. I needed to
rmarkdown::render('in.md',
output_format=pdf_document(latex_engine='xelatex')
)
with the novel command (use xelatex) on its own line.
I encountered this problem while I was trying to add an in-line r code r test1$p.value, which is a very small p-value from t test. The error information is as following:
> ! Missing $ inserted.
> <inserted text>
> $
>l.147 9.0044314\times
>
>pandoc: Error producing PDF
>Error: pandoc document conversion failed with error 43
>Execution halted
I think the problem is the pdflatex engine has a trouble in displaying the small p-value in exponential notation.
I solved the problem by clicking on the gear symbol next to the knit button, then under output options, advanced tab I changed the LaTeX Engine to lualatex, or you can just report the p-value as p < 0.001.
If you are using inline values from your R code which are in the scientific format (too small or too big), format them like:
replace r x
with r format(x, digits=n) where n is whatever.
for me it was because on my headers I was putting + signs. For example gene + treatment. This errors but when I removed it, it works.
In my case it was solved simply by editing the author field in:
---
title: "Document Title"
author: '-----'
date: "21-03-2017"
output: pdf_document
---
the default '-----' would yield the error, but replacing it with anything (for example 'Juan') solved the issue.
I just ran into this problem and already solved it. I didn't use any code as other people did in their posts.
I will assume that you have installed all these basic stuff: R, RStudio, the rmarkdown package, the knitr package, and the MikTex basic installation (I know this is very basic, but I want those first timers know that you need these stuff to make this happen).
If you run into this problem, go to R GUI, upgrade the rmarkdown package and it should work then. Note that if you change the LaTeX Engine to xelatex as the poster of the highest vote did, it may not work for you, at least it did not for me. I leave my latex engine as it is (pdflatex).
I had a similar issue. My solution was to remove the "leading" period in the YAML title argument:
Does not work:
---
title: “1. Title”
output: pdf_document
---
output file: example.knit.md
! Argument of \reserved#a has an extra }.
\par l.79 \end{enumerate}}
pandoc: Error producing PDF Error: pandoc document conversion failed
with error 43 Execution halted
Works:
---
title: “1 Title”
output: pdf_document
---
I did try to use the xelatex engine but still, I got the error that xetex.def is not found. This is another to work around.
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
Then open .tex file in your TEX editor and build pdf as usual.
I faced a similar issue. In my case, the error occurred because of putting a percentage inside the $ sign.
Like this,
$95%$, I removed the % sign, and everything worked fine.

Resources