I am working with markdonw v2, the rmarkdown package. Throughout the .Rmd file, I create links to websites or images
[Link1][pathLink1]
![Image1][pathImage1]
then, at the end of the document I give the references
[pathLink1]:http://website.com/linkes/Link1.md
![pathImage1]:./images_rmd/
There are other reports that talk about the same citation and use same images in different contexts. I would like to create a separate file containing all the links and path difinitions, so that I could simply source it at the end of each .Rmd file, like I would call in an R environment
source(/Rcode1.R)
Question: How do I "source" another file in .Rmd, so that the sourced code prints needed text strings into the .Rmd file?
This would offer some help with citations and scientific paper composition in HTML and PDF.
http://yihui.name/knitr/demo/child/
```{r child, child = '~/path/to/child.Rmd'}
```
and similarly for .Rnw files:
<<child, child = '~/path/to/child.Rnw'>>=
#
And a full example: https://github.com/yihui/knitr-examples/blob/master/087-child-example.Rnw
Related
I have several Rmarkdown (Rmd) files in the same directory, and want to render them into individual pdf files using bookdown::render_book. I normally use knitr::knit for that, but would like to take advantage of the cross-referencing in bookdown. But I would still like to have one Rmd file for each pdf file, and have them live independently in the same directory.
According to section 12.4 of the bookdown book I can use output: bookdown::pdf_document2 in the yaml header to generate a single pdf from an Rmd file. However, bookdown::render_book always combines all Rmd files that it finds in the directory, which is not what I want.
Is there an option in the yaml header or in the bookdown::render_book function where I can tell it to ignore all other Rmd files in the directory?
I know there is an option rmd_files I can specify in the file _bookdown.yaml but I don't know how this would work with multiple Rmd files, and I would also like to avoid having to maintain a separate yml file.
I have begun using Rmd to render Powerpoint presentations consistently, using the YAML tags and more importantly, a reference Powerpoint to ensure standardized / consistent formatting:
output: powerpoint_presentation: slide: reference_doc: "reference.pptx". When I want to share a PPT document as a reference for my peers / students however, I want to be able to have my slides available as a .pdf file.
I have had success using shell commands in R using LibreOffice's soffice command, however I am not always at a workstation with that available. Is there a portable solution / executable I can call, or additional Rmd tags for rendering a document with a .pptx reference document, but rendered as a .pdf file?
I believe you have to specifiy output: beamer_presentation in the header.
Take a look at this: https://rmarkdown.rstudio.com/formats.html
I got 2 files from my university for writing thesis using LaTeX. One is a .sty file and other one is .TeX file. In order to work in R studio I've decided to have separate .Rnw files for each chapter and one file for combining all the chapters. I think .TeX file is the one where I can combine all the chapters because it gives sample chapters in output. On R studio's website there is a page titled as 'Working with Multiple Rnw Files' which describes this process (I guess) but is not clear to me. It talks about 'child' files which I think are the chapters in my case. So my simple question is that if I create different .Rnw files, one for each chapter, how can I ask R to combine them in one TeX file which university provided me? Please bear my ignorance as I am new to reproducible research stuff.
Assuming you're using knitr (and I highly recommend knitr over sweave) the simple way to do this is with the child chunk option.
As an example, say you had 2 chapters, kept in files chap1.Rnw and chap2.Rnw and a master document thesis.Rnw (with university style file called thesisStyle). You can put these all together, inside thesis.Rnw -- assuming these are all in the same directory -- via:
\documentclass{article}
\usepackage{thesisStyle}
\begin{document}
% "include" chapter 1
<<chap1, child='chapt1.Rnw'>>=
#
% again with chapter 2
<<chap2, child='chap2.Rnw'>>=
#
\end{document}
Then just have RStudio compile thesis.Rnw and it'll spit out thesis.tex which will have everything bundled together properly.
That's not all, though! You can develop chap1.Rnw without having to give it its own preamble. That is, if the content of chap1.Rnw is
<<echo=FALSE, cache=FALSE>>=
set_parent('thesis.Rnw')
#
\chapter{In a world where...}
\section{Great voice actors in movie trailer history}
ANYTHING YOU'D NORMALLY PUT IN AN .Rnw FILE
then you can compile chap1.Rnw like any regular .Rnw file and it'll take the preamble from thesis.Rnw before running whatever TeX backend you're using (normally pdflatex or xelatex). In particular, knitr will slap the \documentclass{article} and \usepackage{thesisStyle} lines at the top of chapt1.tex.
One word of caution, I've found the child-parent model in knitr to be white-space sensitive. So, be sure to have no space above the block
<<echo=FALSE, cache=FALSE>>=
set_parent('thesis.Rnw')
#
You have a couple of options.
One option is to just process each of your chapters by hand. You will have a .Rnw file for each chapter, then in Rstudio (or R) you run the knit function from the knitr package (there may be an Rstudio button or menu to do this directly) to convert your .Rnw file to a .tex file. Then in the parent LaTeX document you just use \include to include the .tex files for each chapter. This does mean processing each chapter yourself and having to go back and redo it anytime you change anything.
The other option is to create a parent and child documents that knitr will understand and process automatically for you (Rstudio is using knitr to do the processing to .tex and eventually .pdf files). This page has demonstrations on creating the parent and child documents this way, just modify the .tex file given to you to include the important things in the demos (and probably change the name to .Rnw). Make sure that the document class matches the .sty file given to you and the important options from the .tex file remain, but include the child documents as shown in the knitr demo. This way you can process the document as a whole rather than each individual chapter.
I'm using the knitr package and pandoc in R to convert a .Rmd file to a PDF. Pandoc is linked to a .bib file and automatically inserts the bibliography at the end of the PDF
The entries in my .bib file look like these, taken from http://johnmacfarlane.net/pandoc/demo/biblio.bib:
#Book{item1,
author="John Doe",
title="First Book",
year="2005",
address="Cambridge",
publisher="Cambridge University Press"
}
#Article{item2,
author="John Doe",
title="Article",
year="2006",
journal="Journal of Generic Studies",
volume="6",
pages="33-34"
}
To build my bibliography, I'm using the following function, taken from: http://quantifyingmemory.blogspot.co.il/2013/02/reproducible-research-with-r-knitr.html
knitsPDF <- function(name) {
library(knitr)
knit(paste0(name, ".Rmd"), encoding = "utf-8")
system(paste0("pandoc -o ", name, ".pdf ", name, ".md --bibliography /Users/.../Desktop/test.bib --csl /Users/.../Desktop/taylor-and-francis-harvard-x.csl"))
}
The contents of my .Rmd file is:
This is some text [#item1]
This is more text [#item2]
# References
And outputted PDF looks like this:
If I try to insert an appendix, the references still print at the end of the document, like this:
How do insert an appendix after the references?
With newer pandoc versions, you can specify the bibliography's position with <div id="refs"></div> source
This is some text [#item1]
This is more text [#item2]
# References
<div id="refs"></div>
# appendix
Eventually reference handling will change to make it possible to put the references wherever you like (https://github.com/jgm/pandoc/issues/771), but right now there's no easy way to do it.
As suggested here, you could put your appendix in a separate file, use pandoc to convert it to a LaTeX fragment, then include that fragment using the --include-after-body flag. It would then come after the bibliography.
When working in an Rmarkdown document, enter the following text where the citations are to be located. It can be placed in any part of the document allowing other materials, like an appendix, to follow as necessary. The method relies on pandoc's fenced divs which will work in Rmarkdown.
::: {#refs}
:::
The aforementioned code should not be in an R code chunk, rather it should be placed on blank lines by themselves. Once processed by pandoc via knitter, this code will produce the same result as <div id="refs"></div> mentioned in the answer by #soca. The two lines of code do consistently allow for exact placement of the references in any section of the document.
In the example below, references are placed first under a heading of the same name while all of the code chunks in the document are placed afterwards in a code appendix. Here is the pandoc fenced div placed in Rmarkdown that can be used to generate the image that follows.
# References
::: {#refs}
:::
# Appendix A: R Code
```{r ref.label=knitr::all_labels(), echo=TRUE, eval=FALSE}
```
Provided there is a .bib file identified in the yaml frontmatter, the preceding Rmarkdown produces output similar to the following:
Helpful links:
Pandoc User’s Guide - Placement of the Bibliography
Pandoc User’s Guide - Divs and Spans
How can the position of the bibliograpy section be set Latex format
9.6 Custom blocks (*) | R Markdown Cookbook
I really like using knitr in Rstudio and have been using it to write markdown presentations and data analysis. I want to use the same code and results in a paper and want to convert the code chunks in Rmd file ```{r} to the chunks of Rnw file << >>= #.
This allows using the same document and code written for presentation for the main paper as well.
Is there a way of converting between code chunks of markdown and Rnw files ?
or the entire file itself as apart from the difference in syntax of code chunks, they are quite similar in the markup (converting the text to latex is easy with say pandoc)
Instead of converting the whole document, you can just externalize your R code so it can be shared across different documents; see http://yihui.name/knitr/demo/externalization/
Once you have read_chunk('Rcode.r'), you can use ```{r label} in your Rmd and <<label>>= in your Rnw document, where label comes from the line ## #knitr label in the R script.