Unable to find file 'path/NA' when knitting to word_document2 using tbl_summary but works for PDF, HTML etc - r-markdown

before you click "next", this is not a path file error.
I am building on a blogpost: https://labrtorian.com/2019/08/26/rmarkdown-template-that-manages-academic-affiliations/
It works well for scientific papers.
My problem is:
In Rmarkdown when calling a data frame I am returned with the error about not being able to find file 'path/NA'
---
output:
bookdown::word_document2:
pandoc_args:
- --reference-doc=Extras/Reference_Document.docx
keep_tex: TRUE
---
```{r MRE}
library(gtsummary)
a <- c(10,20,30,40)
b <- c('book', 'pen', 'textbook', 'pencil_case')
c <- c(TRUE,FALSE,TRUE,FALSE)
d <- c(2.5, 8, 10, 7)
df <- data.frame(a,b,c,d)
tbl_summary(df)
tbl_summary
which returns:
Error: could not find file '/Users/brianbrummer/Documents/Rstudio/Affilitations_template/NA'
Execution halted
So what gives? I would usually know how to fix a "path not found" error, but not a file...and the file is NA?
files referenced in YAML can be found:
https://github.com/bridaybrummer/author_affiliations_extras.git
Thanks in advance

I did some digging and found the issue and a solution. First, if you update your yaml to call the reference document like this, you won't get the error any longer.
---
output:
word_document:
reference_doc: Reference_Document.docx
---
The issue was with the flextable package. If you're looking for community service points, you can post this reprex to their github page to alert them to the issue.
---
output:
word_document:
pandoc_args:
- --reference-doc=Reference_Document.docx
---
```{r MRE}
mtcars |>
flextable::flextable()
```

Related

When I use RStudio to Generate PDF File,I Met this Error :! Missing number, treated as zero

When I use RStudio to Generate PDF File,I Met this Error :
Missing number, treated as zero.
<to be read again>
(
l.349 \begin
{minipage}[b]{\linewidth}\raggedright
错误: LaTeX failed to compile bookdown.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See bookdown.log for more info.
停止执行
When I open the tex file:
\begin{minipage}[b]{\linewidth}\raggedright
独立性和可持续性
\end{minipage}
the Rmd file is
---
CJKmainfont: PingFangSC-Regular
fontsize: "12pt"
indent: true
header-includes:
- \usepackage{xeCJK}
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
toc: yes
toc_depth: 1
template: latex/template.tex
documentclass: ctexbook
link-citations: yes
geometry: [b5paper, tmargin=2.5cm, bmargin=2.5cm, lmargin=3.5cm, rmargin=2.5cm]
colorlinks: yes
---
latex/template.tex:
https://github.com/yihui/bookdown-chinese/blob/master/latex/template.tex
How can I fix it? if I comment the code (#template:latex/template.tex) the Errors would be gone
You will successfully render the following Rmd to a pdf without any error.
Although #Stéphane Laurent commented here that you may need to add a diameter to \linewidth, I do not think that is cause of the problem you faced, since the Rmd below using \linewidth, not 1\linewidth, works fine.
The real problem may lay in other parts of your Rmd file, I suspect. However, you must post a reproducible example like the one I show in this answer, as I commented here, and present the contents of the .log file that is created when you knit your Rmd file.
---
header-includes:
- \usepackage{ctex}
output:
pdf_document: default
---
\begin{minipage}[b]{\linewidth}\raggedright
独立性和可持续性
\end{minipage}

R Error: "attempt to use zero-length variable name" when using "Preview on save"

It seems there is a problem on "Preview on Save" option in RStudio 2022.07.1 Build 554.
The code below is saved as "tmp1.Rmd". I can run chunks, and I CAN use Preview button (it works). But I can NOT save with "Preview on Save" option set.
When I change YAML Header output from output: html_notebook to output: html_document the error stops (I saw in this question).
Is this an RStudio bug?
The code:
---
title: "R Notebook"
output: html_notebook
---
# VE
```{r}
x=3
```
end

Bookdown/rmarkdown: "Could not find data file templates/--number-sections.latex Error: pandoc document conversion failed with error 97"

Following: render_book("index.Rmd")
I was getting this other error associated with three part table:
LaTeX Error: Environment ThreePartTable undefined
I applied the solution suggested there(remotes::install_github('rstudio/rmarkdown')). Now I have this error:
"Could not find data file templates/--number-sections.latex
Error: pandoc document conversion failed with error 97"
My index.Rmd looks like this:
title: "Figures and Tables"
author: "name"
date: "`r Sys.Date()`"
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
keep_tex: yes
citation_package: natbib
fontsize: 12pt
classoption: oneside
linestretch: 2
documentclass: book
bibliography: "bibs.bib"
biblio-style: "apalike"
geometry: "left=1.5in, right=1in, top=1in, bottom=1in"
---
preamble.tex has:
\usepackage[none]{hyphenat}
\usepackage{booktabs}
\pagestyle{plain}
\raggedbottom
While render_book("index.Rmd") does not generate a pdf and gives me this pandoc error, it successfully generates a merged markdown file "FiguresTables.Rmd." When I try to knit the merged file it works, it does generate the pdf. I just cannot figure-out why I am unable to generate the pdf from render_book("index.Rmd").
Never mind! Writing the question made me think of an answer. I just re-installed r-markdown from the cran repository instead of github and tried again. And it works fine now.

Render to "pdf_document" output format in rmarkdown getting stuck on knitr asis_output function

New to Rmarkdown (and markdown in general). I've inherited some code that works great for the html_document output format but not for pdf_document. It seems to get stuck on the knitr asis_output function in the .Rmd script. When I comment out chunks containing that function, it writes to pdf no problem. Here's some troubleshooting I've tried:
xfun::session_info('rmarkdown')
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.1, RStudio 1.2.1335
Random number generation:
RNG: Mersenne-Twister
Normal: Inversion
Sample: Rounding
Locale: en_CA.UTF-8 / en_CA.UTF-8 / en_CA.UTF-8 / C / en_CA.UTF-8 / en_CA.UTF-8
:Package version:
base64enc_0.1.3 digest_0.6.20 evaluate_0.14 glue_1.3.1 graphics_3.6.1 grDevices_3.6.1 highr_0.8
htmltools_0.4.0 jsonlite_1.6 knitr_1.25 magrittr_1.5 markdown_1.1 methods_3.6.1 mime_0.7
Rcpp_1.0.2 rlang_0.4.0 rmarkdown_1.16 stats_3.6.1 stringi_1.4.3 stringr_1.4.0 tinytex_0.17.1
tools_3.6.1 utils_3.6.1 xfun_0.10 yaml_2.2.0
Pandoc version: 2.7.3
Sys.getenv('PATH')
[1] "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin:/opt/X11/bin"
tinytex::tinytex_root()
[1] "/usr/local/texlive/2019"
(tinytex::tlmgr_path())
tlmgr path add add_link_dir_dir: /usr/local/share/info/dir exists;
not making symlink. add_link_dir_dir: destination
/usr/local/share/man/man5 not writable, no links from
/usr/local/texlive/2019/texmf-dist/doc/man/man5.tlmgr: An error has
occurred. See above messages. Exiting. add of symlinks had 1
error(s), see messages above.[1] 6
So maybe the problem is a path issue? In which case I have no clue how to fix. Or should I be using an alternative to the asis_output function? Any help is much appreciated. Here's the relevant bits of my code:
In the R script:
id <- 44
rmarkdown::render('mymarkdown.Rmd',
output_format = "pdf_document",
output_file = paste("report_", id,".pdf", sep=''),
output_dir = '/Users/myname/Documents/test')
In the Rmd file:
---
title: "Monitoring Activity Summary Report"
mode: selfcontained
date: "November 2019"
output:
pdf_document: default
html_document: default
self_contained: yes
---
[some code chunks...]
[then these code chunks that get stuck only for "pdf_document"...]
``` {r setup_Samp1a, echo=FALSE}
sampling_1 <- !is.na(sampling_unique[1])```
```{r conditional block, eval = sampling_1}
asis_output("### 3.1 Sampling 1\\n") # Header that is only shown if
sampling_1 == TRUE```
The error message
! Undefined control sequence.
<argument> 3.1 Sampling 1\n
Error: Failed to compile /Users/myname/Documents/test/report_44.tex.
See https://yihui.name/tinytex/r/#debugging for debugging tips. See
report_44.log for more info.

Where to put header.tex to change page to Landscape Rmarkdown

I read here how to add what is needed to give me the ability to change my page to landscape:
enter link description here
However I have saved the header.tex file everyplace I could think of and I still end up getting the following error:
Called from: (function (e)
{
if (inherits(e, "shiny.silent.error"))
return()
handle <- getOption("shiny.error")
if (is.function(handle))
handle()
})(list(message = "pandoc document conversion failed with error 43",
call = NULL))
Browse[1]>
I know this is do to me typing \blandscape and \elandscape because the PDF works as soon as I delete those commands. What I need help with is figuring out where to save the header.tex file to so that Rmarkdown knows how to locate it. Thanks in advance for everyone's your help!
Follow up:
Taking scoa's advice about adding it to my header I edited the top of my Rmd file to the following. Unfortunately it still didn't work.
---
title: "Lake Report"
output: pdf_document
header-includes:
- \usepackage{lscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
---
\newpage
\blandscape
```{r Non-Seasonal MK results, echo=FALSE}
# MKanalysis <- reactive(HM_MK(data = NonSeasonalData(),n = 4))
kable(MKanalysis(), caption = "Non-Seasonal Trend Analysis")
```
The results of the Mann Kendall analysis....
\elandscape
Latex program/verision: pdfTex, Version 3.14159265-2.6-1.40.17
Save the header.tex in the same directory your Rmd file is. Also, rather than an external file, you could just add those commands in your yaml front matter, a more portable solution:
---
title: "Lake Report"
output: pdf_document
header-includes:
- \usepackage{lscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
---