0

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?

boris
  • 3
  • 3
  • What are you attempting in rmarkdown::render() with the encoding argument? The encoding is always assumed to be UTF-8. Have you attempted the markdown with the default: encoding = "UTF-8"? – Susan Switzer Jan 23 '23 at 18:50
  • I followed starjas instructions without giving this argument a second thought. I should have realized that the placeholder needs to be substituted. I did that now (and edited my first post). The problem persists. Moreover, I tried an other solution: I called the .Rmd file from a regular R script: ```rmarkdown::render(input = "Folder/Script.Rmd", output_format = "pdf_document", output_file = "Report.pdf", output_dir = "05-Reports")``` This results in nearly the same error message: Error in file(filename, "r", encoding = encoding) : cannot open the connection – boris Jan 25 '23 at 12:39
  • Update: I found a workaround: In the part where I source the regular R script, I explicitly set the wd and added the absolute path to the source: ```setwd("/Users/my-User/Documents/studyXYZ/1-XYZdata/6-R/studyXYZ-R-Project")``` ```source("/Users/my-User/Documents/studyXYZ/1-XYZdata/6-R/studyXYZ-R-Project/02-Scripts/03-LogMod-6-plot.R")``` And voilà - the pdf finally is stored in the desired output directory ("05-Reports"). However, I think that there are problems of redundancy in this solution. Furthermore, I want to avoid absolute paths for interchangeability reasons. – boris Jan 25 '23 at 14:00

1 Answers1

1

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())})
starja
  • 9,887
  • 1
  • 13
  • 28
  • Yes, this works! Thank you very much, starja! You saved me additional hours of frustrating attempts to solve my problem. Awkward that R in this regard ignores both a setting from global options as well as an option set by code but will only comply with this specific argument... – boris Jan 25 '23 at 20:24