I am new to Stackoverflow - so please excuse any possible strangeness in my post.
I have a simmilar question as Tom, posted here.
I want to save a pdf created by markdown in a specific directory. After several unsuccessful attempts (including a frustrating chat with GPT), I followed starjas instructions in the post mentioned, yet unsuccessfully.
Here's my YAML header:
---
title: 'MyTitle'
subtitle: 'Analysis XYZ'
author: "Boris et al."
date: "2023-01-22"
knit: (function(input, encoding) {
rmarkdown::render(input,
encoding = "UTF-8",
output_dir = "05-Reports")})
output: pdf_document
---
… and here some code snippets from the scripts main body:
knitr::opts_chunk$set(echo = FALSE, message = TRUE)
source("02-Scripts/03-LogMod-6-plot.R", local = knitr::knit_global())
{r model results} model6.res.table <- simple_kable <- knitr::kable(model6.res, format = "pipe") model6.res.table
Here's the error message I get:
Error in file(filename, "r", encoding = encoding) : cannot open the connection Calls: <Anonymous> ... eval_with_user_handlers -> eval -> eval -> source -> file Execution halted
I believe that the problem has to do something with R not being able to find the output-directory specified (05-Reports). But I have no clue why, as this output-directory is within the working directory:
getwd() [1] "/Users/my-User/Documents/studyXYZ/1-XYZdata/6-R/studyXYZ-R-Project"
list.files() [1] "01-Data" "02-Scripts" "03-Outputs" [4] "04-Plots" "05-Reports" "studyXYZ-R-Project.Rproj"
Maybe important to know: I am working with a R-project. Furthermore, in the Global Options under R Markdown, I set "Evaluate chunks in directory:" "Project", as Julius Diel mentioned in a thread regarding a similar topic:
"Error "cannot open the connection" in executing "knit HTML" in RStudio" Before setting R's Global Options like this, I had other issues regarding the scource()-command from my markdown-script adressing the R-scripts containing the analysis-commands. Now the scourcing works fine and I get the results as expected when conducting the markdown script without specifying the output directory (in html and pdf). It's just that the pdf won't be saved where I want it. Does anyone have a clue what causes this problem?
I didn't know about the option Evaluate chunks in directory: "Project"
. Previously, I used the following in the setup chunk:
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
However, this option is ignored (as well as the option set in RStudio) when using rmarkdown::render
in the header of the document. You need to set knit_root_dir
to define the root directory from which you the can specify relative paths in source
used in your rmarkdown document:
knit: (function(input, encoding) {
rmarkdown::render(input,
output_dir = "05-Reports",
knit_root_dir = rprojroot::find_rstudio_root_file())})