【问题标题】:RMarkdown code chunk works in preview but not when "build" with bookdownRMarkdown 代码块在预览中有效,但在使用 bookdown“构建”时无效
【发布时间】:2026-01-16 03:30:01
【问题描述】:

我有以下 COVID 19 的 Rmarkdown 代码:

library(tidyverse)
# Importiere Johns Hopkins Github data
confirmedraw <- read.csv( "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")
deathsraw <- read.csv( "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv")
recoveredraw <- read.csv( "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv")
confirmed <- confirmedraw %>% 
  gather(key="date", value="confirmed", -c(Country.Region, Province.State, Lat, Long)) %>%    group_by(Country.Region, date) %>% 
  summarize(confirmed=sum(confirmed))
deaths <- deathsraw %>% 
  gather(key="date", value="deaths", -c(Country.Region, Province.State, Lat, Long)) %>% 
  group_by(Country.Region, date) %>% 
  summarize(deaths=sum(deaths))
recovered <- recoveredraw %>% 
  gather(key="date", value="recovered", -c(Country.Region, Province.State, Lat, Long)) %>% 
  group_by(Country.Region, date) %>% 
  summarize(recovered=sum(recovered))
summary(confirmed)

country <- full_join(confirmed, deaths) %>% 
  full_join(recovered)

这在 RStudio 预览版和 R 控制台本身中运行良好。

但是,如果我尝试使用 bookdown 构建我的书,最后一行中的 full_join() 将失败。 我得到错误:

Error: `by` must be supplied when `x` and `y` have no common variables.
ℹ use by = character()` to perform a cross-join.
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(main, output_format, ..., clean = clean, envir = envir)
  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(group)
 13. │                 └─knitr:::call_block(x)
 14. │                   └─knitr:::block_exec(params)
 15. │                     ├─knitr:::in_dir(...)
 16. │                     └─knitr:::evaluate(...)
 17. │                       └─evaluate::evaluate(...)
 18. │                         └─evaluate:::evaluate_call(...)
 19. │                           ├─evaluate:::timing_fn(...)
 20. │                           ├─base:::handle(...)
 21. │                           ├─base::withCallingHandlers(...)
 22. │                           ├─base::withVisible(eval(expr, envir, enclos))
 23. │                           └─base::eval(expr, envir, enclos)
 24. │                             └─base::eval(expr, envir, enclos)
 25. ├─dplyr::full_join(confirmed, deaths) %>% dplyr::full_join(., recovered)
 26. ├─dplyr::full_join(., recovered)
 27. ├─dplyr::full_join(confirmed, deaths)
 28. └─dplyr:::full_join.data.frame(confirmed, deaths)
 29.   └─dplyr:::join_mutate(...)
 30.     └─dplyr:::join_cols(...)
 31.       └─dplyr:::standardise_join_by(by, x_names = x_names, y_names = y_names)

如何更改我的代码以使用 bookdown 运行?

【问题讨论】:

    标签: r-markdown bookdown


    【解决方案1】:

    我在进行故障排除时确实遇到了类似的错误。

    通过单独运行每个命令/块直到它完成来解决这个问题,我确实遇到了间歇性执行问题,有时我遇到错误,有时它运行成功,我只是重新运行它直到它完成。

    为了更好地帮助我理解,您的 YAML 是什么样的?你到底是如何制作这本书的?是 gitbook 吗?

    我找到解决方案的方法是对 bookdown 有一点了解。我有 2 个文件,index.Rmd01-covid19.Rmd index.Rmd 是空的,只有 YAML 用于这本书。在01-covid19.Rmd我粘贴了你的代码

    index.Rmd

    --- 
    title: ""
    author: ""
    date: "`r Sys.Date()`"
    output: bookdown::gitbook
    site: bookdown::bookdown_site
    description: ""
    ---
    
    # Introduction {-}
    

    01-covid19.Rmd

    # Covid19
    
    ```{r}
    library(tidyverse)
    # Importiere Johns Hopkins Github data
    confirmedraw <- read.csv( "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_cov    id19_confirmed_global.csv")
    deathsraw <- read.csv( "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid1    9_deaths_global.csv")
    recoveredraw <- read.csv( "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_cov    id19_recovered_global.csv")
    confirmed <- confirmedraw %>% 
      gather(key="date", value="confirmed", -c(Country.Region, Province.State, Lat, Long)) %>%    group_by(Country.Region, date) %>% 
      summarize(confirmed=sum(confirmed))
    deaths <- deathsraw %>% 
      gather(key="date", value="deaths", -c(Country.Region, Province.State, Lat, Long)) %>% 
      group_by(Country.Region, date) %>% 
      summarize(deaths=sum(deaths))
    recovered <- recoveredraw %>% 
      gather(key="date", value="recovered", -c(Country.Region, Province.State, Lat, Long)) %>% 
      group_by(Country.Region, date) %>% 
      summarize(recovered=sum(recovered))
    summary(confirmed)
    
    country <- full_join(confirmed, deaths) %>% 
      full_join(recovered)
    
    country
    ```
    

    最终渲染为 Rmarkdown Bookdown 输出,R 代码位于 01 位置。这两个文件必须与其他 Bookdown 依赖项位于同一目录中,也可以尝试创建一个新的 R 项目。告诉我,谢谢。

    【讨论】:

      最近更新 更多