Chapter 10 Seeking Help from Others with reprex

If you’re having trouble using a function or package, or getting the syntax correct, 9 times out of 10 the answers you are seeking have already been asked and answered on-line:

If you can’t find the answer on-line, there are some on-line forums to seek help from the bcgov R community:

10.1 Help People Help You - reprex

Whan starting a conversation about code, there are a few things that when included make that conversation more efficient and pleasant for everyone:

  • some sample data (preferably provided in the code)
  • some code (with comments, white-space, indents == make it easy to read)
  • information about your computing environment, like your operating system, version of R, the packages you have loaded

reprex: An R package to help prepare a reproducible example — or reprex — for posting to GitHub issues, StackOverflow, RocketChat snippets, or email and so on.

# install.packages("reprex")

# load libraries we need
library(reprex)
library(readr)
library(dplyr)
library(ggplot2)

# read in data
cats <- read_csv(file = "data/feline-data.csv")

# munge data and make bar plot
cats %>% 
  select(coat, weight) %>% 
  ggplot(aes(x = coat, y = weight)) +  
  geom_col()

# error in syntax - this does not work
cats %>% 
  select(coat, weight) %>% 
  ggplot(aes(x = coat, y = weight)) %>%   
  geom_col()
Error: `mapping` must be created by `aes()`
Did you use %>% instead of +?

You have read the error message but it was not helpful, you used ?ggplot2, and looked on-line and I still cannot find an answer. Maybe someone in the bcgovR community knows?

We need to invest a bit of time making the buggy code shareable – the easier it is for someone to run the code, the easier – and likely faster – someone might be able to provide some help.

If the data is shareable (e.g. available in the B.C. Data Catalogue with appropriate licence, publicly available) then we can use dput():

# use dput to make data set with code
dput(cats)
structure(list(coat = c("calico", "black", "tabby"), weight = c(2.1, 
5, 3.2), likes_string = c(1, 0, 1)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L), spec = structure(list(
    cols = list(coat = structure(list(), class = c("collector_character", 
    "collector")), weight = structure(list(), class = c("collector_double", 
    "collector")), likes_string = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))
cats <- structure(list(coat = c("calico", "black", "tabby"), weight = c(2.1, 
5, 3.2), likes_string = c(1, 0, 1)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L), spec = structure(list(
    cols = list(coat = structure(list(), class = c("collector_character", 
    "collector")), weight = structure(list(), class = c("collector_double", 
    "collector")), likes_string = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

# error in syntax - this does not work
cats %>% 
  select(coat, weight) %>% 
  ggplot(aes(x = coat, y = weight)) %>%  
  geom_col()
Error: `mapping` must be created by `aes()`
Did you use %>% instead of +?

dput() will dump the data you’re working with into a format so that it can be copy and pasted by anyone else into their R session.

If the data is not shareable, you can use some built in data and re-create the issue:

# ?datasets # built in datasets
# ?starwars # ships with dplyr

# View(starwars)

# error in syntax - this does not work
starwars %>% 
  select(name, height) %>% 
  ggplot(aes(x = name, y = height)) %>%  
  geom_col()
Error: `mapping` must be created by `aes()`
Did you use %>% instead of +?

Once the code is ready for sharing, select the code and use reprex_selection() or the clickable RStudio Addin to copy the formatted code to the clipboard for sharing on-line (demonstrated).

Sometimes it is useful to share information about your computing environment as well:

sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.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/3.6/Resources/lib/libRlapack.dylib

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

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

other attached packages:
[1] reprex_0.3.0   tidyr_1.0.0    knitr_1.25     ggplot2_3.2.1 
[5] dplyr_0.8.3    readr_1.3.1    testthat_2.2.1 devtools_2.2.1
[9] usethis_1.5.1 

loaded via a namespace (and not attached):
 [1] tidyselect_0.2.5  xfun_0.9          remotes_2.1.0    
 [4] purrr_0.3.2       colorspace_1.4-1  vctrs_0.2.0      
 [7] htmltools_0.3.6   yaml_2.2.0        utf8_1.1.4       
[10] rlang_0.4.0       pkgbuild_1.0.5    pillar_1.4.2     
[13] glue_1.3.1        withr_2.1.2       sessioninfo_1.1.1
[16] lifecycle_0.1.0   stringr_1.4.0     munsell_0.5.0    
[19] gtable_0.3.0      memoise_1.1.0     evaluate_0.14    
[22] labeling_0.3      callr_3.3.2       ps_1.3.0         
[25] fansi_0.4.0       Rcpp_1.0.2        clipr_0.7.0      
[28] backports_1.1.4   scales_1.0.0      desc_1.2.0       
[31] pkgload_1.0.2     fs_1.3.1          hms_0.5.1        
[34] packrat_0.5.0     digest_0.6.21     stringi_1.4.3    
[37] bookdown_0.13     processx_3.4.1    bcgovr_1.0.3     
[40] rprojroot_1.3-2   grid_3.6.1        cli_1.1.0        
[43] tools_3.6.1       magrittr_1.5      lazyeval_0.2.2   
[46] tibble_2.1.3      crayon_1.3.4      whisker_0.4      
[49] pkgconfig_2.0.3   zeallot_0.1.0     ellipsis_0.3.0   
[52] praise_1.0.0      rsconnect_0.8.15  prettyunits_1.0.2
[55] assertthat_0.2.1  rmarkdown_1.15    rstudioapi_0.10  
[58] R6_2.4.0          compiler_3.6.1   

sessionInfo() will print out your current version of R, as well as any packages you have loaded. This can be useful for others to help reproduce and debug your issue.

10.2 Other Helpful Resources