Skip to content

Building parameterized books with script fails  #932

Closed

Description

@jenslaufer

I am trying to build different books with a kind of parameterized script:

library(tidyverse)
library(glue)
library(bookdown)


parameterized_book_render <- function(.title, .param1) {
  outputdir <- "_book/PDF/{.title}/{.param1}" %>% glue()
  
  parameters <- list(title = .title,
                     param1 = .param1)
  
  bookdown::render_book("index.Rmd",
                        "bookdown::pdf_book",
                        output_dir = outputdir,
                        params = parameters)
}

data <-
  tibble(title = c("title1", "title2"),
         param1 = c("bla", "blubb"))

data %>%
  pmap( ~ parameterized_book_render(..1,
                                    ..2))

The script works perfectly when it's ran within RStudio.

However, in case I run the code with

Rscript build.R

I get this error:

Error in output_dirname(output_dir, config) : 
  object 'outputdir' not found
Calls: %>% ... parameterized_book_render -> <Anonymous> -> output_dirname
Execution halted

I am also using a Docker container for building (see source code). I am getting exactly the same error there.

Full Source Code on GitHub


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.

Activity

  1. cderv commented on Aug 25, 2020

    @cderv
    Collaborator

    That is strange. FWIW I can reproduce with something more minimal

    parameterized_book_render <- function(.title, .param1) {
      outputdir <- sprintf("_book/PDF/%s/%s", .title, .param1)
      parameters <- list(title = .title,
                         param1 = .param1)
      message("==> Output directory will be ", outputdir)
      bookdown::render_book("index.Rmd",
                            "bookdown::pdf_book",
                            output_dir = outputdir,
                            params = parameters)
    }
    
    parameterized_book_render("title1", "bla")

    it is always a good idea to reduce your example to the bare minimum for easier debugging.

  2. cderv commented on Aug 25, 2020

    @cderv
    Collaborator

    Oh yes! I always forgot about this one !
    You need to set clean_envir = FALSE in render_book(). By default it is, clean_envir = !interactive(), hence the difference you see between interactive session and non interactive one. See https://bookdown.org/yihui/bookdown/build-the-book.html

    I let you try.

  3. cderv commented on Aug 25, 2020

    @cderv
    Collaborator

    Also for reference duplicate of #835 and #592

  4. cderv commented on Aug 25, 2020

    @cderv
    Collaborator

    @yihui It seems unfortunate that this kind of batch mode raise this issue silently. It would not happen I guess if the argument of render_book would be evaluated (like using force()) before cleaning the environment. Or in this case if the part using output_dir would be run before cleaning.

    But indeed, workaround is pretty simple when you know it though, and it is document in the book.

  5. jenslaufer commented on Aug 26, 2020

    @jenslaufer
    Author

    Thanks so much; indeed clean_envir = FALSE fixes the problem.

    So it's no bug or something.

  6. added a commit that references this issue on Sep 9, 2020
    a4c97d8
  7. yihui commented on Sep 9, 2020

    @yihui
    Collaborator

    In retrospect, clean_envir = TRUE was a really bad idea. Actually I have also been bitten by this issue by myself. Since it has bitten another person, I just deprecated this argument, and you no longer need it in the future. You may install and test the development version via

    remotes::install_github('rstudio/bookdown')

    Thanks for the report!

  8. added a commit that references this issue on Sep 24, 2020
    50cd58e
  9. github-actions commented on Mar 10, 2021

    @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.

  10. locked as resolved and limited conversation to collaborators on Mar 10, 2021
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