Skip to content

Object not found using variables as named parameter values in render_book #835

Closed

Description

@matthew-brett

By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.org/issue/.
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('bookdown'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('rstudio/bookdown').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

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

Please forgive me if this is expected, but I was very surprised by the following behavior.

I have a R file do_render.R, with this content:

val <- NULL
bookdown::render_book('index.Rmd', output_dir=val)

If I run this as an R script, with Rscript do_render.R in the bookdown-demo repository (or another Bookdown project), I get the following output:

$ Rscript do_render.R 
Error in output_dirname(output_dir, config) : object 'val' not found
Calls: <Anonymous> -> output_dirname
Execution halted

I get the same error for any variable used as the value for a named parameter, for other named parameters after the ... in the parameter list, such as clean=val, where val <- TRUE, or config_file=val where val <- "_bookdown.yml".

I do not get an error, and get a normal build, if I source('do_render.R') from R.

Is this expected? If so, is there any way to use variables as values for named arguments in this situation?

Output of xfun::session_info('bookdown')

R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

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  bookdown_0.16.2  digest_0.6.23    evaluate_0.14   
  glue_1.3.1       graphics_3.5.2   grDevices_3.5.2  highr_0.8       
  htmltools_0.4.0  jsonlite_1.6     knitr_1.26       magrittr_1.5    
  markdown_1.1     methods_3.5.2    mime_0.7         Rcpp_1.0.3      
  rlang_0.4.2      rmarkdown_1.18.2 stats_3.5.2      stringi_1.4.3   
  stringr_1.4.0    tinytex_0.17     tools_3.5.2      utils_3.5.2     
  xfun_0.11        yaml_2.2.0 

Activity

  1. cderv commented on Dec 1, 2019

    @cderv
    Collaborator

    bookdown::render_book have an argument to decide to clean or not the R environment before rendering the book. See for clean_envir in https://bookdown.org/yihui/bookdown/build-the-book.html

    By default it will be FALSE in interactive use but TRUE in non interactive mode. That is why val is not found when you run it with Rscript

    this should work as you expect

    val <- NULL
    bookdown::render_book('index.Rmd', output_dir=val, clean_envir = FALSE)

    It is all explain in the book for more details
    https://bookdown.org/yihui/bookdown/build-the-book.html

  2. matthew-brett commented on Dec 1, 2019

    @matthew-brett
    Author

    Aha - thank you. I did not have a deep enough understanding of R to realize that even not-default named parameters don't get evaluated until the function is running.

    I suppose the ideal would be if it were possible to evaluate these, and these only, before clearing the R workspace.

  3. github-actions commented on Nov 11, 2020

    @github-actions

    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.

  4. locked as resolved and limited conversation to collaborators on Nov 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions