Code chunk works in R markdown file interactively, but not when I "build book" with bookdown

I'm working on an electronic book using the bookdown package. I'm about 31 chapters in and have had very few problems up to this point. One of my chapter Rmd files includes the following code chunks:

library(dplyr)
set.seed(123)
df <- tibble(
  x = rnorm(10),
  y = rnorm(10),
  z = rnorm(10)
) %>% 
  print()
df %>%
  summarise(
    across(
      .cols  = everything(),
      .fns   = mean,
      .names = "{col}_mean"
    )
  )

When I run the code chunks interactively, they produce the expected results without a problem. However, when I click the "Build Book" button, I get the following error:

Quitting from lines 10530-10538 (Book.Rmd) 

Error: Problem with `summarise()` input `..1`.
x Problem with `across()` input `.fns`.
ℹ Input `.fns` must be NULL, a function, a formula, or a list of functions/formulas.
ℹ Input `..1` is `across(.cols = everything(), .fns = mean, .names = "{col}_mean")`.
Backtrace:
     █
  1. ├─rmarkdown::render_site(output_format = "bookdown::gitbook", encoding = "UTF-8")
  2. │ └─generator$render(...)
  3. │   ├─xfun::in_dir(...)
  4. │   └─bookdown:::render_book_script(output_format, envir, quiet)
  5. │     └─bookdown::render_book(...)
  6. │       └─bookdown:::render_cur_session(...)
  7. │         └─rmarkdown::render(...)
  8. │           └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
  9. │             └─knitr:::process_file(text, output)
 10. │               ├─base::withCallingHandlers(...)
 11. │               ├─knitr:::process_group(group)
 12. │               └─knitr:::process_group.block(gr
Execution halted

Exited with status 1.

Here's my session info:

R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6     bookdown_0.20    tidyr_1.1.0      crayon_1.3.4    
 [5] digest_0.6.25    dplyr_1.0.1      R6_2.4.1         lifecycle_0.2.0 
 [9] magrittr_1.5     evaluate_0.14    pillar_1.4.4     rlang_0.4.7     
[13] rstudioapi_0.11  ellipsis_0.3.1   vctrs_0.3.2      generics_0.0.2  
[17] rmarkdown_2.1    tools_4.0.2      glue_1.4.1       purrr_0.3.4     
[21] xfun_0.13        yaml_2.2.1       compiler_4.0.2   pkgconfig_2.0.3 
[25] htmltools_0.4.0  tidyselect_1.1.0 knitr_1.28       tibble_3.0.1    

Does anybody know what this is about?

1 Like

Looks like the second code block is getting passed around from rmarkdown to bookdown to knitr enough to lose track of the arguments. What I would try is to save the second chunk to an output object and then call it at the end, so that it is already calculated before it has to be rendered.

Passing strange that base got lost from the namespace. Glad you ran it to ground. Thx for marking soln

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Thank you very much for the feedback, @technocrat!

I got a solution that worked on StackOverflow. The bottom line is that I had to replace mean with base::mean in all instances where I used mean inside of across().

user2554330, who posted the solution that worked on SO, theorized that the problem was that I had named a variable "mean" somewhere else in my code prior to hitting "Build Book." Maybe I did, but (1) I typically don't do things like that, and (2) I did use the find tool in RStudio to search for "mean <-" and "mean =" in the > 12,000 lines of my compiled Rmd file. No instances of "mean <-" were found at all, and all the instances of "mean =" were inside of summarise. So, I'm inclined to think that I didn't name any variables mean.

In summary:

  • The code worked when I ran it interactively.
  • It worked with bookdown when I replaced mean with base::mean
  • It worked with bookdown when I used ~ mean(.x)
  • mean worked elsewhere in my code. Just not inside of across
  • I may have named an object "mean" at some point, but I couldn't find it.