Specify files with bookdown's `rmd_files` for word document output? I get "Error in basename(files)" - docx

Following #yihui's bookdown usage 1.3, I'm attempting to select specific files to render in each output format by adding a rmd_files argument to _bookdown.yml in #yihui's Minimal Book Example.
rmd_files:
html: ["index.Rmd", "01-intro.Rmd"]
latex: ["index.Rmd", "01-intro.Rmd"]
epub: ["index.Rmd", "01-intro.Rmd"]
word: ["index.Rmd", "01-intro.Rmd"]
To _output.yml, I add
bookdown::word_document2:
toc: true
Everything else in the Minimal Book Example is unchanged.
rmarkdown::render_site(encoding = 'UTF-8') renders html, latex, and epub documents, but word output fails with Error in basename(files) : a character vector argument expected Calls: <Anonymous> ... FUN -> render_book -> source_files -> grep -> basename
The word document renders if I remove the rmd_files argument.
I tried omitting index.Rmd from the files list, deleting all content from both Rmd files, and using word_document instead of word_document2.
Same results with CRAN bookdown_0.20 and development version 0.20.3 on two different machines (both macs).
This error may be similar to this post, but it did not provide a minimal example and appears to have been fixed in the latest bookdown release.
Bookdown is amazing, and I could be missing something simple here. Thanks so much for any help!

The name of the output format is docx instead of word.
rmd_files:
docx: ["index.Rmd", "01-intro.Rmd"]
The name is for Pandoc, and all names are listed here: https://pandoc.org/MANUAL.html#option--to

Related

Bookdown preview_chapter

I don't seem to be able to get preview_chapter working on my r bookdown project. This is my _bookdown.yml file:
book_filename: "thesis"
delete_merged_file: true
output_dir: "docs"
new_session: yes
rmd_files: # defines the .Rmd files to be included for each output format
html: ["index.Rmd", "02-methodology.Rmd", "03-results.Rmd", "99_references.Rmd"]
When I use:
bookdown::render_book("index.Rmd", bookdown::'gitbook')
The entire book compiles correctly. But if I try to build a single chapter as follows:
bookdown::preview_chapter("03-results.Rmd", bookdown::'gitbook')
I get:
Error in files2[[format]] :
attempt to select less than one element in get1index
Am I using an incorrect syntax?
bookdown::'gitbook' should be 'bookdown::gitbook'.

What's the difference between `_bookdown.yml`, `_output.yml` and yaml header of the first document?

When authoring a document with bookdown, I am aware of four options to put configuration options:
_bookdown.yml
_output.yml
the yaml header of the first .Rmd document
arguments passed to the bookdown::render_book() function.
Now my question:
Which options can go where, i.e. can I use all files interchangeably?
Which options should go where, i.e. what is good practice to put the configuration options?
so far I know...hope others can complete this
First have a look at this high level explanation R markdown
next use the hyperlinks provided below which all point to the documentation details
YAML header of the first *.Rmd file
can contain additional pandoc parameters as described here.
It must contain the following line: site: bookdown::bookdown_site
may contain multiple output formats specified by
output:
bookdown::gitbook:
bookdown::pdf_book:
bookdown::epub_book:
... but these can also be in the _output.yml as described here all formats should be at the top level, instead of under an output field.
Example
_bookdown.yml
All _bookdown.yml parameters used for generating the book and save the results.
Example
book_filename: "_book_example"
repo: https://github.com/<user>/<repo>/
before_chapter_script: ["script1.R"]
after_chapter_script: ["script2.R"]
output_dir: "_book"
clean: ["deleteme.Rmd"]
rmd_files: ["index.Rmd", "02-literature.Rmd", "01-intro.Rmd"]
delete_merged_file: true
language:
label:
fig: "FIGURE "
tab: "TABLE "
ui:
edit: "Edit"
chapter_name: "Chapter "
rmd_subdir: ["content/"]

`stringr::str_view` errors with Bookdown

Given with this
```{r}
stringr::str_view(c("alice", "bob"), "o")
```
I get an error:
Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:
always_allow_html: yes
Note however that the HTML output will not be visible in non-HTML formats.
Why is bookdown complaining about this? It seems that stringr::str_view is being executed unnecessarily.
EDIT: The desired result is a PDF with the stringr::str_view(c("alice", "bob"), "o") text and syntax highlighting.
See Section 2.10 on HTMl Widgets in the bookdown book at https://bookdown.org/yihui/bookdown/html-widgets.html. Basically you need to install the webshot package and PhantomJS.
If you have enabled caching (via the chunk option cache=TRUE, e.g., https://github.com/rstudio/bookdown/issues/578), you have to remove cache files and rebuild the book.

knitr html to Word docx using pandoc

I have been saving some example R markdown html output to Word using pandoc. I actually only do this so I can add some page breaks for easier printing:
system("pandoc -s Exercise1.html -o Exercise1.docx")
Although the output is acceptable I was wondering if there is a way to keep the original syntax highlighting of the R chunks (just as they are in the original knit HTML document)?
Also, I seem to be loosing all images in the conversion process and have to stick them into Word by hand. Is that normal?
Using the rmarkdown package (baked into RStudio Version 0.98.682, the current preview release) it's very simple to convert Rmd to docx, and code highlighting is included in the docx file.
You just need to include this at the top of your markdown text:
---
title: "Untitled" # obviously you can change this
output: word_document # specifies docx output
---
However, it seems that page breaks are still not supported in this conversion.
Why not convert the markdown directly to Word format?
Anyway, Pandoc does not support syntax highlighting in Word: "Currently, the only output formats that uses this information are HTML and LaTeX."
About the images: the Word file would definitely include those if you'd convert the markdown to Word directly. I am not sure about the HTML source, but I suppose you might have a path issue.

Pandoc insert appendix after bibliography

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

Resources