class: center, middle, title-slide # How to reprex <img src="figs/reprex.png" title="Logo for package reprex" alt="Logo for package reprex" width="20%" /> ### Ariel Muldoon ### May 25, 2021 --- ## Before we begin I wanted to quickly show you a couple of packages that are relevant to topics that came up in the interactive plots/data session. 1. You can *animate* plots in R and save the output as GIF's or videos with package **gganimate**. This works well to show patterns among groups, like through time. <a href="https://gganimate.com/" target="_blank"><img src="figs/gganimate.png" title="Logo for package gganimate" alt="Logo for package gganimate" width="5%" style="float:left; padding:10px" /></a> <br/> [**gganimate** website link](https://gganimate.com/) <br/> <br/> 2. If you are working with data collected across geographic units like states in the USA or provinces in Canada, etc., you can use package **geofacet** to make faceted plots across geographical space. <a href="https://hafen.github.io/geofacet/" target="_blank"><img src="figs/geofacet.png" title="Logo for package geofacet" alt="Logo for package geofacet" width="5%" style="float:left; padding:10px" /></a> <br/> [**geofacet** website link](https://hafen.github.io/geofacet/) ??? Folks discussed graphing by state as well as adding animations in week 8 --- class: with-logo logo-reprex ## Today's Goal Overall - **Learn how to make minimal reproducible examples in R** ??? This is needed for asking for help on R code -- We will - Discuss the components of a minimal reproducible example - Explore options to provide example data to make code runnable - Learn how to use package **datapasta** to provide a snippet of your own data - Use package **reprex** to create pasteable code and output --- ## Resources - [Intro article](https://reprex.tidyverse.org/) and [Reprex do's and don'ts](https://reprex.tidyverse.org/articles/reprex-dos-and-donts.html) from the **reprex** website - Sharla Gelfand's "make a reprex... please" [slides](https://make-a-reprex-please.netlify.app/#1) and [recording](https://t.co/860RTqMvjy) - [How to Datapasta](https://milesmcbain.github.io/datapasta/articles/how-to-datapasta.html) .center[ <img src="figs/week09_files/datapasta.png" title="Logo for package datapasta" alt="Logo for package datapasta" width="30%" /> ] --- ## What is a reprex? The term *reprex* stands for **repr**oducible **ex**ample. If you ever ask for R help on a forum or want to report an issue on a GitHub repository you will be asked for example code that reproduces the problem; a *reprex*. -- <br/><br/> Here are several nice overviews on how to make a reproducible example that are worth reviewing: [RStudio Community guide](https://community.rstudio.com/t/faq-how-to-do-a-minimal-reproducible-example-reprex-for-beginners/23061) [Stack Overflow canonical thread](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) [Tidyverse help guide](https://www.tidyverse.org/help/) --- ## What is in a reprex? <br/> A reprex should include: > - A minimal dataset, necessary to reproduce the issue > - The minimal runnable code necessary to reproduce the issue, which can be run on the given dataset, and including the necessary information on the used packages *From the RStudio Community guide* --- ## What is in a reprex? <br/> A reprex should include: > - a minimal dataset, necessary to demonstrate the problem > - the minimal runnable code necessary to reproduce the error, which can be run on the given dataset > - all necessary information on the used packages, the R version, and the OS it is run on > - in the case of random processes, a seed (set by set.seed()) for reproducibility *From the Stack Overflow thread on reproducible examples* -- <br/> Note the consistent use of the word ***minimal***. ??? We'll be talking about minimal later. --- ## Why should I reprex? Making a minimal, reproducible example can take time. It can be easy to convince yourself to skip the process and "just get the help I need". But the time is ultimately worth spending. ๐ต -- > - 80% of the time creating an excellent reprex reveals the source of your problem. Itโs amazing how often the process of writing up a self-contained and minimal example allows you to answer your own question. > - The other 20% of time you will have captured the essence of your problem in a way that is easy for others to play with. This substantially improves your chances of getting help! *From the tidyverse help guide* ??? A lot of times you can solve your own problem while working towards a minimal, reproducible example. --- ## Why should I reprex? .center[ <img src="figs/week09_files/reprex_art.png" title="Artwork by Allison Horst showing a cartoon monster first struggling to work with a support monster on a problem on the left and then the same monster enjoying success after providing a reproducible example on the right, helping others help them solve their problem" alt="Artwork by Allison Horst showing a cartoon monster first struggling to work with a support monster on a problem on the left and then the same monster enjoying success after providing a reproducible example on the right, helping others help them solve their problem" width="70%" /> Artwork by @allison_horst ] ??? It helps people help you! It also makes make it much more likely you will get a positive response. --- ## Why should I reprex? <blockquote class="twitter-tweet tw-align-center" data-lang="en"><p lang="en" dir="ltr">Slide showing the many ways you may be asked for a reproducible examples</p>— Sharla Gelfand (@sharlagelfand) <a href="https://twitter.com/sharlagelfand/status/1365665149063987201">Tweet February 27, 2021</a></blockquote> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script> ??? And you will be asked for one! I included this tweet to focus on the picture of the shared slide. --- ## The plan for today - I will introduce an R coding scenario that I need your help solving - We'll go through my attempts to provide a reproducible example, with you copying code I provide in the slides broadcast and running it in a clean R session - We'll discuss how I can improve things so my code meets the definition of a minimal reproducible example - Finally, we'll practice using package **reprex** to get code and output ready to paste into a forum and then discuss a few additional features of the package -- <br/> This gets us ready for next week when we'll: - Talk about GitHub issues and issue etiquette - Practice making an issue, writing text and pasting code and output via package **reprex** ??? GitHub issues was originally in this same slide deck but I decided it'd be better to use the time in a separate working session. --- class: center, middle, inverse # <font style="font-family: cursive; font-style:italic">Let's get started!</font> --- ## "I need help" take 1 ๐ฌ **Me:** "I'm having a problem with my **ggplot2** code. My points dodge but my error bars don't. Help! Here is my code." **You:** Copy and paste the code below into your R Console and run. What happens? .smaller[ ```r ggplot(data = mydata, aes(x = over_species, y = est1)) + geom_point(aes(color = spp), position = position_dodge(width = .5), size = 2) + geom_errorbar(aes(ymin = lo1, ymax = hi1), position = position_dodge(width = .5), width = .2) + scale_color_manual(values = c("#1c5253", "#F4790B"), labels = c("Douglas-fir", "Red alder")) + labs(color = "Species", x = "Overstory", y = "Estimate") + theme_minimal() + theme(panel.grid.major.x = element_blank(), panel.grid.minor.y = element_blank(), legend.direction = "horizontal", legend.position = "bottom", legend.box.spacing = unit(0, "pt"), plot.margin = margin(b = 0)) ``` ] --- ## "I need help" take 1 ๐ฌ ``` ## Error in ggplot(data = mydata, aes(x = over_species, y = est1)) : ## could not find function "ggplot" ``` When the error message indicates a function can't be found, most often the package hasn't been loaded. That's the case here. I didn't provide the packages I'm using in my example code. -- <br/><br/> You might now be thinking, "Well, geez, people can easily guess what package I'm using based on my code." Help people help you! ๐ค To maximize the chance of getting help you should make it as easy as possible for people to provide that help. Include your packages code, even if you think what you used is obvious. ??? This is the classic "help me help you" slide --- ## "I need help" take 1 ๐ฌ Going back to the info in the guides for reproducible examples, I should provide: > ...runnable code necessary to reproduce the issue, which can be run on the given dataset, and including the necessary information on the used packages. <br/> OK, I didn't give code for the data or packages in my first attempt. Let me try again. --- ## "I need help" take 2 ๐ฌ **Me:** "Oh, you need my packages and dataset? No problem. Here you go." .tiny[ ```r library(covr) library(car) library(nlme) library(ggbiplot) library(devtools) library(ggplot2) library(dplyr) library(emmeans) setwd("C:/Users/USER/Documents/MyProject") data = read.csv("all_project_data.csv") mydata = read.csv("all_results.csv") table = data %>% group_by(over_species, spp) %>% summarize(mean = mean(variable), sd = sd(variable)) mod = lm(variable ~ over_species*spp, data = data) summary(mod) # Save these results! ggplot(data = mydata, aes(x = over_species, y = est1)) + geom_point(aes(color = spp), position = position_dodge(width = .5), size = 2) + geom_errorbar(aes(ymin = lo1, ymax = hi1), position = position_dodge(width = .5), width = .2) + scale_color_manual(values = c("#1c5253", "#F4790B"), labels = c("Douglas-fir", "Red alder")) + labs(color = "Species", x = "Overstory", y = "Estimate") + theme_minimal() + theme(panel.grid.major.x = element_blank(), panel.grid.minor.y = element_blank(), legend.direction = "horizontal", legend.position = "bottom", legend.box.spacing = unit(0, "pt"), plot.margin = margin(b = 0)) ``` ] --- ## "I need help" take 2 ๐ฌ **You:** Are likely feeling overwhelmed right now because I just gave you a ton of code. ๐ -- <br/> I asked you earlier to note the word ***minimal*** used over and over in the reproducible example descriptions we reviewed. We'll discuss that in more detail now. --- ## *Minimal* code When asked to provide the package and dataset I made a reasonable attempt to do so. However, I went too far and provided code that wasn't relevant to the problem. -- <br/> When providing an example, give the absolute minimum amount of code that can reproduce the problem. No extra packages, no extra data manipulation steps, etc. <br/> If possible, provide the final version of a dataset. The person helping you should not have to run a bunch of code to create the dataset your are using. (*More on datasets coming up.*) ??? Don't make your potential helpers run all the code to create a dataset in a bunch of data manipulation steps. Provide them the final dataset. --- ## *Minimal* code Even when I get rid of the extraneous packages and data manipulation steps my code isn't minimal. All tweaks to the plot appearance, such as those highlighted below, are not related to the problem I am having. This code should be removed from the reprex. .tiny[ ```r library(ggplot2) setwd("C:/Users/USER/Documents/MyProject") mydata = read.csv("all_results.csv") ggplot(data = mydata, aes(x = over_species, y = est1)) + geom_point(aes(color = spp), position = position_dodge(width = .5), size = 2) + geom_errorbar(aes(ymin = lo1, ymax = hi1), position = position_dodge(width = .5), width = .2) + * scale_color_manual(values = c("#1c5253", "#F4790B"), * labels = c("Douglas-fir", "Red alder")) + * labs(color = "Species", * x = "Overstory", * y = "Estimate") + * theme_minimal() + * theme(panel.grid.major.x = element_blank(), * panel.grid.minor.y = element_blank(), * legend.direction = "horizontal", * legend.position = "bottom", * legend.box.spacing = unit(0, "pt"), * plot.margin = margin(b = 0)) ``` ] --- ## "I need help" take 3 ๐ฌ Me: "OK, I got things down to just the code that produces the problem. Here you go!" You: Copy and paste the code into your R Console and run. What happens now? ```r library(ggplot2) setwd("C:/Users/USER/Documents/MyProject") mydata = read.csv("all_results.csv") ggplot(data = mydata, aes(x = over_species, y = est1)) + geom_point(aes(color = spp), position = position_dodge(width = .5), size = 2) + geom_errorbar(aes(ymin = lo1, ymax = hi1), position = position_dodge(width = .5), width = .2) ``` --- ## "I need help" take 3 ๐ฌ ``` ## Error in setwd("C:/Users/USER/Documents/MyProject") : ## cannot change working directory ``` ``` ## Error in file(file, "rt") : cannot open the connection ## In addition: Warning message: ## In file(file, "rt") : ## cannot open file),'all_project_data.csv': No such file or directory ``` <br/> These errors are related to the dataset. I provided code for loading the dataset but you don't have access to data stored on my computer. --- ## The problem of setwd() We're going to talk more about how to provide datasets. Before we do that, though, let's talk about my use of `setwd()` when asking someone who doesn't have access to my computer to run my code. -- An excerpt from ["Reprex do's and don'ts"](https://reprex.tidyverse.org/articles/reprex-dos-and-donts.html) succinctly captures the problem: > **Pack it in, pack it out, and donโt take liberties with other peopleโs computers.** You are asking people to run this code! > - Donโt start with `rm(list = ls())`. It is anti-social to clobber other peopleโs workspaces. > - Donโt start with `setwd("C:\Users\jenny\path\that\only\I\have")`, because it wonโt work on anyone elseโs computer. -- <br/> Your goal is to make it easy for someone else to run your code and `setwd()` doesn't do that. If you regularly use `setwd()` in your work, consider switching to [Project-oriented workflows](https://www.tidyverse.org/blog/2017/12/workflow-vs-script/). ??? Can add asides here about issues of `setwd()` for your future self or your self using a different computer. --- ## Providing a dataset The thing I've seen people struggle with the most when making a reprex is in providing a dataset. This is a place where making a reproducible example can get a time-consuming. -- <br/> The three most common ways to provide a dataset in a reproducible example: 1. Use a built-in dataset like `mtcars` ๐. 2. Create a fake dataset. 3. Provide some portion of your real dataset. --- ### Use a built-in dataset Our reprex focus is on making it as easy as possible for someone else to run our code. Editing your code to work with one of the datasets built in to R is a good way to achieve this. These datasets are available to all R users with no effort on their part. -- <br/> Commonly used datasets include `mtcars` ๐ and `iris` ๐ผ, but there are a bunch more. Run `data()` in your R Console to see all available datasets that come with R. You can also opt to use datasets that come with add-on packages. However, that requires your potential helpers to have that package installed. --- ### Use a built-in dataset Using a built-in dataset means I have to edit my code to work with a different dataset. This can be time-consuming โณ, but gets easier with practice. I took `mtcars` ๐ and rewrote my code using the new variables to reproduce the problem. I chose just two rows of the dateset with the same value of `gear` but different values of `vs`. Copy the code and run it in your R Console to see the result. ```r library(ggplot2) ggplot(data = mtcars[2:3,], aes(x = factor(gear), y = mpg)) + geom_point(aes(color = factor(vs)), position = position_dodge(width = .5), size = 2) + geom_errorbar(aes(ymin = mpg - 1, ymax = mpg + 1), position = position_dodge(width = .5), width = .2) ``` ??? Also note how I didn't need actual values in the dataset for making error bars. The take home here is that the problem doesn't have to do with my real data --- ### Create fake data Another way to provide data is to make a fake dataset. This can be a good option if your data are proprietary, for example. Fake data can be a straightforward way to provide a *minimal* dataset to reproduce the problem, especially if you aren't familiar with any of the built-in datasets. -- <br/> I will show an example of making fake data on the next slide. Creating fake data takes practice, and may feel difficult when you first try it. Stick with it, it gets easier! ๐ ??? Many problems can be reproduced without working with your entire real dataset --- ### Create fake data .pull-left[ Here is an example of making a small dataset via `data.frame()` that matches the features and variables in my real dataset but with simplified values. <br/> Since my problem is with dodging I only need a single `x` variable group to reproduce the problem. My fake dataset ends up having two rows. ] .pull-right[ .smaller[ ```r mydata = data.frame(over_species = c("ACMA", "ACMA"), spp = c("DF", "RA"), est1 = c(1, 2), lo1 = c(1, 2) - 2, hi1 = c(1, 2) + 2) mydata ``` ``` ## over_species spp est1 lo1 hi1 ## 1 ACMA DF 1 -1 3 ## 2 ACMA RA 2 0 4 ``` ] ] .footnote[See other ways to make fake data, such as with functions from package **tibble**, in this [RStudio Community thread](https://community.rstudio.com/t/best-practices-how-to-prepare-your-own-data-for-use-in-a-reprex-if-you-can-t-or-don-t-know-how-to-reproduce-a-problem-with-a-built-in-dataset/5346).] --- ### Create fake data You can include the code for creating the fake dataset along with the rest of your code. Copy the code and run it in your R Console to see the result: .smaller[ ```r library(ggplot2) mydata = data.frame(over_species = rep("ACMA", times = 2), spp = c("DF", "RA"), est1 = c(1, 2), lo1 = c(1, 2) - 2, hi1 = c(1, 2) + 2) ggplot(data = mydata, aes(x = over_species, y = est1)) + geom_point(aes(color = spp), position = position_dodge(width = .5), size = 2) + geom_errorbar(aes(ymin = lo1, ymax = hi1), position = position_dodge(width = .5), width = .2) ``` ] **Note:** If your example contains randomly generated numbers, be sure to provide the seed with `set.seed()`. --- class: with-logo logo-pasta ### Provide your own data If you want to provide some portion of your actual dataset you can use helper functions from package **datapasta**. There are other options for doing this, such as function `dput()`, but **datapasta** puts the resulting dataset in a nice, readable format. -- <br/> We'll briefly use `datapasta::dpasta()` to explore how this works. There is much more you can do with **datapasta** then what we'll do here. Check out [How to Datapasta](https://milesmcbain.github.io/datapasta/articles/how-to-datapasta.html) for neat examples on copying and pasting data into R from online sources, etc. --- class: with-logo logo-pasta ### Provide your own data Run the **datapasta** code below in your R Console to see how `dpasta()` turns the first few rows and columns of `mtcars` into an easily copy-and-pasteable format. ```r datapasta::dpasta(mtcars[1:6, 1:3]) ``` -- Output: ```r data.frame( row.names = c("Mazda RX4","Mazda RX4 Wag","Datsun 710", "Hornet 4 Drive","Hornet Sportabout","Valiant"), mpg = c(21, 21, 22.8, 21.4, 18.7, 18.1), cyl = c(6, 6, 4, 6, 8, 6), disp = c(160, 160, 108, 258, 360, 225) ) ``` <br/> **Note:** I found slight alignment issue with the output format from `dpasta()`, as shown above. Highlighting and using `Ctrl + I` (MacOS `Cmd + I`) will fix that right up. ๐ --- class: with-logo logo-pasta ### Provide your own data This is the approach I wanted to use in order to include my actual dataset with my code. Here's what my actual dataset looks like: |over_species |spp | est1| lo1| hi1| est2| lo2| hi2| var1| var2| |:------------|:---|----:|----:|----:|----:|----:|----:|----:|----:| |ACMA |DF | 11.8| 9.8| 13.8| 11.5| 6.5| 16.5| -1.0| -0.7| |ACMA |RA | 7.4| 5.4| 9.4| 26.6| 21.6| 31.6| 0.1| 1.7| |ALRU |DF | 9.5| 7.5| 11.5| 27.5| 22.5| 32.5| 1.0| 0.7| |ALRU |RA | 7.3| 5.3| 9.3| 12.8| 7.8| 17.8| 0.6| -1.7| |PSME |DF | 13.6| 11.6| 15.6| 16.4| 11.4| 21.4| 1.8| 0.6| |PSME |RA | 8.1| 6.1| 10.1| 21.8| 16.8| 26.8| 0.1| 0.5| Take note of all the extra columns. In keeping with making things minimal, I'll remove all columns not directly relevant to the problem prior to using `dpasta()`. ??? This works because my dataset is so small. Otherwise providing a portion of dataset with head() or something may be better --- ## "I need help" take 4 ๐ฌ **Me:** "I'm having a problem with my **ggplot2** code. My points dodge but my error bars don't. Help! Here is a minimal reproducible example." **You:** Can now copy and paste the code into your R Console and immediately get it to run. ๐ .smaller[ ```r library(ggplot2) mydata = data.frame( stringsAsFactors = FALSE, over_species = c("ACMA", "ACMA", "ALRU", "ALRU", "PSME", "PSME"), spp = c("DF", "RA", "DF", "RA", "DF", "RA"), est1 = c(11.8, 7.4, 9.5, 7.3, 13.6, 8.1), lo1 = c(9.8, 5.4, 7.5, 5.3, 11.6, 6.1), hi1 = c(13.8, 9.4, 11.5, 9.3, 15.6, 10.1) ) ggplot(data = mydata, aes(x = over_species, y = est1)) + geom_point(aes(color = spp), position = position_dodge(width = .5), size = 2) + geom_errorbar(aes(ymin = lo1, ymax = hi1), position = position_dodge(width = .5), width = .2) ``` ] ??? I did re-format the output from dpasta() with Ctrl + I You can now immediately start helping me instead of figuring out what I meant :) --- class: with-logo logo-reprex ## The reprex package The goal of this package is to >Render bits of R code for sharing, e.g., on GitHub or StackOverflow -- <br/> The package runs a chunk of code you provide, returning both the code and the code output on your clipboard. Paste the result into your question on Stack Overflow or when reporting a potential issue on GitHub. This allows someone to copy your code and run it. Importantly, they can also see what the output looks like *without having to run any code at all*. --- class: with-logo logo-reprex ## The reprex package One simple way to use the **reprex** package is to copy the code from your minimal reproducible example onto your clipboard. Here's my working example to copy again: .smaller[ ```r library(ggplot2) mydata = data.frame( stringsAsFactors = FALSE, over_species = c("ACMA", "ACMA", "ALRU", "ALRU", "PSME", "PSME"), spp = c("DF", "RA", "DF", "RA", "DF", "RA"), est1 = c(11.8, 7.4, 9.5, 7.3, 13.6, 8.1), lo1 = c(9.8, 5.4, 7.5, 5.3, 11.6, 6.1), hi1 = c(13.8, 9.4, 11.5, 9.3, 15.6, 10.1) ) ggplot(data = mydata, aes(x = over_species, y = est1)) + geom_point(aes(color = spp), position = position_dodge(width = .5), size = 2) + geom_errorbar(aes(ymin = lo1, ymax = hi1), position = position_dodge(width = .5), width = .2) ``` ] --- class: with-logo logo-reprex ## The reprex package .pull-left[ Once you have code copied to your clipboard, run `reprex::reprex()` in your R Console. <br/> All code and output will be copied to your clipboard and a rendered HTML preview will show in your `Viewer` Pane. <br/> The result is automatically put on your clipboard, ready to be pasted and render correctly on GitHub or Stack Overflow. (*We will practice this next week.*) ] .pull-right[ <img src="figs/week09_files/reprex1.png" title="Screen shot showing the Viewer Pane after copying my reproducible example code and then running reprex::reprex() in my R Console" alt="Screen shot showing the Viewer Pane after copying my reproducible example code and then running reprex::reprex() in my R Console" width="100%" /> ] ??? See the example Viewer pane on the right throughout this section Students can paste **reprex** output into an empty R script or something if they want to see how things look today but we will look at an actual example next week --- class: with-logo logo-reprex ## The reprex package An alternative to the copying + `reprex()` technique is to wrap your reproducible example code within curly braces inside `reprex::reprex()`. Copy and run the following to see this in action: .smaller[ ```r reprex::reprex({ library(ggplot2) mydata = data.frame( stringsAsFactors = FALSE, over_species = c("ACMA", "ACMA", "ALRU", "ALRU", "PSME", "PSME"), spp = c("DF", "RA", "DF", "RA", "DF", "RA"), est1 = c(11.8, 7.4, 9.5, 7.3, 13.6, 8.1), lo1 = c(9.8, 5.4, 7.5, 5.3, 11.6, 6.1), hi1 = c(13.8, 9.4, 11.5, 9.3, 15.6, 10.1) ) ggplot(data = mydata, aes(x = over_species, y = est1)) + geom_point(aes(color = spp), position = position_dodge(width = .5), size = 2) + geom_errorbar(aes(ymin = lo1, ymax = hi1), position = position_dodge(width = .5), width = .2) }) ``` ] --- class: with-logo logo-reprex ## The reprex package The **reprex** package helps you check if your code is reproducible or not. It returns errors if the code within `reprex()` can't be run as-is. ๐ ??? This can be really useful because it draws your attention to things you may have loaded in your workspace but have forgotten to include in your example code. -- .pull-left[ Copy and run my original code within `reprex()`: .smaller[ ```r reprex::reprex({ ggplot(data = mydata, aes(x = over_species, y = est1)) + geom_point(aes(color = spp), position = position_dodge(width = .5), size = 2) + geom_errorbar(aes(ymin = lo1, ymax = hi1), position = position_dodge(width = .5), width = .2) }) ``` ] ] -- .pull-right[ You'll see the reprex is created in your `Viewer` pane but outputs an error because the package wasn't loaded. <img src="figs/week09_files/reprex_error1.png" title="Screen shot showing the Viewer Pane with code and output after running the original ggplot2 code I provided that didn't have the package or dataset. The output has an error message about not being able to find function ggplot" alt="Screen shot showing the Viewer Pane with code and output after running the original ggplot2 code I provided that didn't have the package or dataset. The output has an error message about not being able to find function ggplot" width="100%" /> ] --- class: with-logo logo-reprex ## The reprex package .pull-left[ Next, copy and run the **ggplot2** reprex without including the dataset: .smaller[ ```r reprex::reprex({ library(ggplot2) ggplot(data = mydata, aes(x = over_species, y = est1)) + geom_point(aes(color = spp), position = position_dodge(width = .5), size = 2) + geom_errorbar(aes(ymin = lo1, ymax = hi1), position = position_dodge(width = .5), width = .2) }) ``` ] ] -- .pull-right[ Now the error is about the dataset not being found, indicating you need to add one to make your example code reproducible. <img src="figs/week09_files/reprex_error2.png" title="Screen shot showing the Viewer Pane with code and output after running the ggplot2 code I provided without providing the dataset. The output has an error message about not being able to find the dataset." alt="Screen shot showing the Viewer Pane with code and output after running the ggplot2 code I provided without providing the dataset. The output has an error message about not being able to find the dataset." width="100%" /> ] <br/> **Note:** There is a `wd` argument in `reprex()` if you absolutely must use a local file in the reprex. --- class: with-logo logo-reprex ## The reprex package Let's practice making a reprex with different code in order to explore a couple more options. .pull-left[ Here's the classic **dplyr** before **plyr** problem with `summarise()`. Copy and run: ```r reprex::reprex({ library(dplyr) library(plyr) mtcars %>% group_by(cyl) %>% summarise(mpg = mean(mpg)) }) ``` Note the many, many messages from the packages. ] .pull-right[ <img src="figs/week09_files/reprex_dplyr.png" title="Screen shot showing the Viewer Pane with code and output after using the provided dplyr-plyr code in reprex::reprex(). It contains many messages from the packages about masking, and includes a message about the issue with loading plyr after dplyr" alt="Screen shot showing the Viewer Pane with code and output after using the provided dplyr-plyr code in reprex::reprex(). It contains many messages from the packages about masking, and includes a message about the issue with loading plyr after dplyr" width="100%" /> ] --- class: with-logo logo-reprex ### Suppress startup messages In this case one of the start-up messages is directly relevant to the problem. However, often when asking for help it is not useful to include the start-up messages and you'd want to suppress them. .pull-left[ One option is to use `suppressPackageStartupMessages()` on chatty packages like **dplyr**. .smaller[ ```r reprex::reprex({ suppressPackageStartupMessages(library(dplyr)) suppressPackageStartupMessages(library(plyr)) mtcars %>% group_by(cyl) %>% summarise(mpg = mean(mpg)) }) ``` ] ] .pull-right[ <img src="figs/week09_files/reprex_dplyr2.png" title="Screen shot showing the Viewer Pane after using the dplyr-plyr code in reprex::reprex() while suppressing messages" alt="Screen shot showing the Viewer Pane after using the dplyr-plyr code in reprex::reprex() while suppressing messages" width="100%" /> <br/> Note the output from the code is commented out. This allows someone else to copy the entire chunk of example code, including any output, and run it as-is in R. ] ??? If output isn't commented out, the helper may have to carefully avoid or skip output in between chunks of code --- class: with-logo logo-reprex ### Suppress startup messages Another option is to use a special comment that work like Rmd chunks, `#+`, to add **knitr** chunk options to suppress messages and warnings. .pull-left[ ```r reprex::reprex({ #+ message = FALSE, warning = FALSE library(dplyr) library(plyr) mtcars %>% group_by(cyl) %>% summarise(mpg = mean(mpg)) }) ``` ] .pull-right[ <img src="figs/week09_files/reprex_dplyr3.png" title="Screen shot showing the Viewer Pane after using the dplyr-plyr code in reprex::reprex() while suppressing messages with #+ chunks" alt="Screen shot showing the Viewer Pane after using the dplyr-plyr code in reprex::reprex() while suppressing messages with #+ chunks" width="100%" /> ] .footnote[ Read about these options and more in the [Suppress package startup messages article](https://reprex.tidyverse.org/articles/articles/suppress-startup-messages.html) ] --- class: with-logo logo-reprex ### Adding your session info Your *session info* includes information on your operating system, R version, and package versions. It can be relevant to some problems in R. Unfortunately the session info can get very long. This can distract from the question, and sometimes you will be explicitly asked *not* to provide it unless requested. -- <br/><br/> **reprex** allows you to add your session info in a collapsible format on GitHub using the `session_info` argument. **Note:** Stack Overflow does not allow for collapsible sections at this time so the entirety of your session info will show there if using this option. ??? This is useful, as it allows you to provide your session info but it doesn't take up a ton of space. --- class: with-logo logo-reprex ### Adding your session info Add session info by using `session_info = TRUE` in `reprex()` (outside the curly braces). .pull-left[ ```r reprex::reprex(session_info = TRUE, { #+ message = FALSE, warning = FALSE library(dplyr) library(plyr) mtcars %>% group_by(cyl) %>% summarise(mpg = mean(mpg)) }) ``` <br/> You can see a section with your session info is included but collapsed in the rendered view. ] .pull-right[ <img src="figs/week09_files/reprex_session.png" title="Screen shot showing the Viewer Pane after using the dplyr-plyr code in reprex::reprex() with collapsed session info" alt="Screen shot showing the Viewer Pane after using the dplyr-plyr code in reprex::reprex() with collapsed session info" width="100%" /> ] ??? This is an argument to `reprex()` so goes outside the curly braces --- class: with-logo logo-reprex ### Adding your session info Here's what the session info looks like if we expand the arrow: .pull-left[ <img src="figs/week09_files/reprex_session_long.png" title="Screen shot showing the Viewer Pane after using the dplyr-plyr code in reprex::reprex() with session info uncollapsed" alt="Screen shot showing the Viewer Pane after using the dplyr-plyr code in reprex::reprex() with session info uncollapsed" width="100%" /> ] -- .pull-right[ There's more you can do with **reprex** that we won't cover. Refer to the package website at https://reprex.tidyverse.org/index.html for more info! ] --- class: with-logo logo-reprex ### Change venue The default output from **reprex** is in Markdown that will work well for pasting code and output onto GitHub or Stack Overflow. You can change the venue with the `venue` argument. Options are: - `venue = "R"` for commented code and output. Uncomment code sections in RStudio using `Ctrl + Shift + C` (MacOS `Cmd + Shift + C`) - `venue = "slack"` for Markdown that works on Slack - `venue = "html"` to paste output to sites that don't support Markdown - `venue = "rtf"` to paste [syntax-highlighted code snippets](https://reprex.tidyverse.org/articles/articles/rtf.html) into PowerPoint (experimental, requires installation of [highlight](http://www.andre-simon.de/doku/highlight/en/highlight.php)) --- class: with-logo logo-reprex ### Change venue I found `venue = "slack"` useful for pasting code and output into an email. .pull-left[ ```r reprex::reprex(venue = "slack", { #+ message = FALSE, warning = FALSE library(dplyr) library(plyr) mtcars %>% group_by(cyl) %>% summarise(mpg = mean(mpg)) }) ``` ] .pull-right[ If I run the `reprex()` code and paste into an email or an R script the output looks like: ```r library(dplyr) library(plyr) mtcars %>% group_by(cyl) %>% summarise(mpg = mean(mpg)) #> mpg #> 1 20.09062 ``` ] --- ## Next week - We'll discuss the process and etiquette for reporting issues on GitHub <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512" class="rfa" style="height:0.75em;fill:currentColor;position:relative;"><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg> repositories - We will take the time to make a practice GitHub issue, using package **reprex** to add code and output .footnote[ [Code for slides](https://github.com/aosmith16/spring-r-topics/tree/main/docs/slides) Slides created via the R packages: [**xaringan**](https://github.com/yihui/xaringan), [gadenbuie/xaringanthemer](https://github.com/gadenbuie/xaringanthemer), [gadenbuie/xaringanExtra](https://github.com/gadenbuie/xaringanExtra) .center[*This work is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/.*] ]