Set R bookdown input directory - r

I'd like to use bookdown to parse a series of .Rmd file into a book. If I have my .Rmd in the same directory as the main index.Rmd file then everything works fine and dandy. However, the .Rmd files are autogenerated from another source and I'd like to keep the input files in a subdirectory. Is it possible to do that?
I can include the file names in _bookdown.yml but I'd prefer not having to update that file every time a new .Rmd file is added.
The current version of _bookdown.yml looks like this
new_session: yes
rmd_files:
html: ["input/index.Rmd", "input/01-file.Rmd"]
latex: ["input/index.Rmd", "input/01-file.Rmd"]
I can move the files in a script but was hoping it was possible through bookdown itself.

In the development version of bookdown you can now provide a list of subdirectories to be search recursively.
For example to have it search dir1/ and dir2/ (and their subdirectories) you can specify rmd_subdir: ["dir1/", "dir2/"] in the _bookdown.yml.

Related

Rendering multiple Rmd files in the same directory to individual pdfs (R, bookdown)

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.

How to ignore _bookdown.yml to render a chapter using its own YAML header?

I have multiple chapters in their own Rmd files and _bookdown.yml and _output.yml files which builds a book as expected. I am writing a thesis and some of these chapters include YAML headers which should render them independently as articles for particular journals. However I can't find a way to make bookdown honour these header blocks while the book's _bookdown.yml and _output.yml files are around. gitbook::render_chapter() does not do this.
So, how can we tell bookdown to ignore the _bookdown.yml and _output.yml files and instead render a chapter using its own YAML header?
A solution is to create a _bookdown_mychapter.yml file particularly for each chapter that needs to be built as an article. In here include rmd_files: ["mychapter.Rmd"] to tell bookdown to include only the single chapter. Then use
bookdown::render_book("mychapter.Rmd", config_file = "_bookdown_mychapter.yml")
to specify that this YAML file should be used instead of the default _bookdown.yml.

.rmarkdown file, blogdown and in window preview for RStudio

I have been using .Rmarkdown files to create blog posts in R blogdown. The output of chunk of codes in .Rmarkdown documents are printed in the console and not in the document.
If instead I create a .rmd file, then previews are in window. RStudio Global options are set to show preview in window for RMarkdown documents.
Is this an expected behavior of .rmarkdown files?
Thanks
The differences between .Rmarkdown and .Rmd is explained here. The most relevant quote being:
In this book, we usually mean .Rmd files when we say “R Markdown documents,” which are compiled to .html by default. However, there is another type of R Markdown document with the filename extension .Rmarkdown. Such R Markdown documents are compiled to Markdown documents with the extension .markdown, which will be processed by Hugo instead of Pandoc.
More specific differences are explained in the book linked above.

Automating adding .tex file content to .Rnw files

I use a program to produce a series of .tex files. I then manually copy the contents of the .tex files and add it to a .Rnw file (Sweave, compiled in RStudio IDE). Is there a way to automatically include the contents of .tex files in the .Rnw file such that the .tex files automatically compile and produce pretty text/figures/tables rather than require copying? Something like the below does not appear to produce what I desire when compiled.
<<>>=
# file.tex contains the code for a pretty LaTeX table
source("file.tex")
#
source is wrong, that is for running R code. Instead, if you are using knitr, you can use the child tag in a knitr chunk, something like:
<<my-child-document, child="file.tex">>=
#

Working with Rstudio for writing thesis chapters in .Rnw

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.

Resources