Search code examples
r-markdownbookdown

RMarkdown code chunk works in preview but not when "build" with bookdown


I have the following Rmarkdown code for COVID 19 here:

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)

This runs fine in RStudio preview and within the R console itself.

However, if I try to build my book with bookdown, the full_join() in the last line will fail. I get error:

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)

How would I change my code to run with bookdown?


Solution

  • I did got a similar error while troubleshooting.

    enter image description here

    This was resolved by individually running each command/chunk until it completes, I did have intermittent execution issues, sometimes I got errors, sometimes it ran successfully, I just reran it until it completed.

    To better help me understand, What does your YAML look like? And how exactly are you building the book? is it a gitbook?

    The way I found a solution was with a little bit of bookdown understanding. I had 2 files, the index.Rmd and the 01-covid19.Rmd the index.Rmd is empty and only has YAML for the book. in the 01-covid19.Rmd I pasted your code

    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
    ```
    

    Which eventually rendered to a Rmarkdown Bookdown output with the R code in the 01 position. Both files have to be in the same directory with other Bookdown dependencies, also try creating a fresh R project. Let me know, Thank you.

    enter image description here