0

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?

Brad Cannell
  • 3,020
  • 2
  • 23
  • 39

1 Answers1

2

If I set

mean <- 3

before running your code chunk interactively, I get exactly the error you saw. You've probably used mean as a variable name in some earlier code chunk. You can force it to use the function by writing your code as

df %>%
  summarise(
    across(
      .cols  = everything(),
      .fns   = base::mean,
      .names = "{col}_mean"
    )
  )
user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Thanks, @user2554330. My compiled Rmd file has > 12,000 lines, so I wasn't able to search it manually for instances where I might have named a variable "mean" (I should know better, though). However, I did use the find tool in RStudio to search for "mean <-" and "mean =". 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. Having said that, your solution worked. When I added `base::` in front of `mean`, the error went away. I only had to do this when I used `mean` with `across`. – Brad Cannell Aug 04 '20 at 00:54
  • 1
    If you are interested in finding it, I'd suggest adding a few statements that print the value of `mean` earlier in the document. A binary search wouldn't take that long to find when it changed. – user2554330 Aug 04 '20 at 08:39