yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.38k stars 876 forks source link

knitr::opts_knit$set(root.dir = params$working_dir) - params not found error #2185

Closed lucygarner closed 1 year ago

lucygarner commented 1 year ago

This issue is also posted on StackOverflow but I haven't had a satisfactory answer yet: https://stackoverflow.com/questions/74090393/knitropts-knitsetroot-dir-paramsworking-dir-params-not-found-error

I am trying to set my working directory in the R Markdown setup chunk as follows.

```{r setup, include = FALSE}
knitr::opts_knit$set(root.dir = params$working_dir)
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```

My working directory is a parameter in the YAML header.

---
title: "Test script"
author: "Author"
date: "`r Sys.Date()`"
params:
  working_dir: "/my/working/directory"
output: html_document
---

However, I keep getting the following error.

Error in resolve(...) : object 'params' not found

xfun::session_info()

R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.6, RStudio 2022.7.2.576

Locale: en_GB.UTF-8 / en_GB.UTF-8 / en_GB.UTF-8 / C / en_GB.UTF-8 / en_GB.UTF-8

Package version:
  base64enc_0.1.3 bslib_0.4.0     cachem_1.0.6    cli_3.4.1       compiler_4.2.1 
  digest_0.6.29   evaluate_0.17   fastmap_1.1.0   fs_1.5.2        glue_1.6.2     
  graphics_4.2.1  grDevices_4.2.1 highr_0.9       htmltools_0.5.3 jquerylib_0.1.4
  jsonlite_1.8.2  knitr_1.40      magrittr_2.0.3  memoise_2.0.1   methods_4.2.1  
  R6_2.5.1        rappdirs_0.3.3  renv_0.16.0-15  rlang_1.0.6     rmarkdown_2.17 
  rstudioapi_0.14 sass_0.4.2      stats_4.2.1     stringi_1.7.8   stringr_1.4.1  
  tinytex_0.42    tools_4.2.1     utils_4.2.1     xfun_0.33       yaml_2.3.5   

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

yihui commented 1 year ago

Like user2554330 on StackOverflow, I can't reproduce the error, either. Could you provide more instructions on how exactly to reproduce the problem?

lucygarner commented 1 year ago

It seems to be intermittent. It happens when I run the code interactively.

yihui commented 1 year ago

I see. In that case, I think it's an RStudio IDE problem, not directly relevant to knitr. In theory, when you run a code chunk interactively via the green right arrow on the right of the code chunk, RStudio should read params from your YAML metadata and create the params variable in the R session. I'm not sure why it could fail sometimes. Anyway, if you report the problem to RStudio, they'd need instructions on how to reproduce the problem, too. Thanks!

github-actions[bot] commented 1 year ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.