Getting help

library(learnr)
library(tutorial.helpers)
library(tidyverse)
library(knitr)
library(reprex)
knitr::opts_chunk$set(echo = FALSE)
options(tutorial.exercise.timelimit = 60, 
        tutorial.storage = "local") 


Introduction

This tutorial covers material from Chapter 8 Workflow: getting help in R for Data Science (2e) by Hadley Wickham, Mine Çetinkaya-Rundel, and Garrett Grolemund. This tutorial will give you a basic understanding of how to search for help and create reproducible examples. The most important lesson is to make extensive use of generative artificial intelligence, generally via large language models (LLMs) like ChatGPT or Bard.

AI

Before 2023, the most common approaches to seeking help with R code involved web searches and regular expressions. Those approaches can still be useful, which is why we explain them in the sections which follow. But, the rise of generative artificial intelligence, usually via large language models (LLMs) like ChatGPT or Bard, has radically changed how most skilled programmers seek help. We now ask the AI. Some tips for doing so include:

If AI is so important, then why do all the tutorials in this package make no use of it?

Google is your friend

If you get stuck, start with Google. Typically adding “R” to a query is enough to restrict it to relevant results. If the search result isn’t useful, it often means that there aren’t any R-specific results available.

Exercise 1

Run the query "how to make a boxplot in R" in Google or in your favorite search engine. What is the URL of the top (non-sponsored) result?

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 3)

Google is particularly useful for error messages. If you get an error message and you have no idea what it means, try googling it! Chances are that someone else has been confused by it in the past, and there will be help somewhere on the web. (If the error message isn’t in English, run Sys.setenv(LANGUAGE = "en") and re-run the code; you’re more likely to find help for English error messages.)

Exercise 2

Adding package names like “tidyverse” or “ggplot2” will help narrow down the results to code that will feel more familiar to you. Consider “how to make a boxplot in R” vs. “how to make a boxplot in R with ggplot2”. Copy the URL for top result to this new search.

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 3)

If Google doesn’t help, try Stack Overflow. Start by spending a little time searching for an existing answer, including [R], to restrict your search to questions and answers that use R.

Exercise 3

Another useful forum is Posit Community, maintained by Posit, the company behind RStudio.

Copy/paste the URL for the most recent topic (the one after the default "Welcome to the Posit Community!"). Look to the righthand column.

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 3)

It's basically like Stack Overflow but specifically focused on R/RStudio and other Posit products.

Exercise 4

The documentation for R is excellent. You can access it with using either ? or the help() command. Run help(geom_point) in the Console, then copy-paste the "Description" section into the space below.

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 3)

Sometimes, you may need to use a double question mark --- ??function_name --- if the function is part of a package you don't have installed. RStudio will prompt you to try ?? if ? doesn't work.

Making a reprex

If your googling doesn’t find anything useful, it’s a really good idea to prepare a reprex, short for minimal reproducible example. A good reprex makes it easier for other people to help you, and often you’ll figure out the problem yourself in the course of making it.

Exercise 1

So let's go over creating our own question and posting it to Posit Community, with the correct context.

Create a simple plot using the ggplot() function by setting data to mtcars then mapping x to cyl and y to mpga using the aes() function. Then add the geom_col() function so that the graph can actually be shown as a bar graph.

This code will fail when you press "Run Code."

ggplot(data = mtcars, 
       mapping = aes(x = cyl, y = mpga)) +
  geom_col()

Let's use this error as the basis for creating a reproducible example.

Exercise 2

First, we need to make a reproducible example

We'll do this by bringing our code into an R Script file.

Copy the code created in the previous exercise and paste it into a new R Script file. This R Script should be in your current R project folder. Save your R Script file and name it script.R.

Try to run this code without loading any libraries.

It is a good idea to restart R with Cmd/Ctrl + Shift + 0. This will unload any currently loaded libraries and ensuring that you are starting from scratch.

Run list.files() in the Console. CP/CR.

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 3)

Always bring your code into an R Script file before doing anything else.

This is because an R Script file is like an independent environment: there's no text, no setup chunks, and no weird formatting errors. It's just your code in its cleanest, plainest format. That means that it's easy to reproduce errors without R Markdown text interfering.

Exercise 3

As you can see, there is an error, but not the one we saw when running this code interactively.

Instead, it's an error about us not loading tidyverse and as such not having the ggplot() function available in our R script.

This is why its important to include any necessary libraries in your example or people trying to help you will be missing key components of your code.

Let's fix that. Load tidyverse using the library() command at the top of script.R.

Then run the code and make sure that you're getting an object not found error rather than a ggplot() not found error.

From the Console, run show_file("script.R"). CP/CR.

Obviously, this command will fail if you have not loaded the tutorial.helpers package in the Console with the library() command.

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 5)

The best way to think about this is trying to help a relative solve a tech problem over the phone - except you are the relative. If you don't want the helper to start pulling their hair out, you need to show every relevant detail and state everything that you are doing.

Exercise 4

Let's talk about some conventions for a reproducible example.

In our regular file, we might be working with complex data from some external source. When creating a reproducible example, we want to use built-in R data so that helpers don't need to look at external data to figure out the issue.

We will still get the same error regardless of the data and helpers will be able to explore the data and work with it easily.

If you don't get the same error, you just answered your own question: the problem is with the data itself, not your code!

Run data() in your console and copy and paste the names of the first three datasets that appear.

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 3)

All of these datasets are built into R, so people can easily help you when looking at your reproducible example. Notice that if you keep scrolling down the "R data sets" page, you will eventually see mtcars.

Exercise 5

The end goal when you ask a question is that the helper doesn't have to do anything to answer your question. They don't have to load it into R, they don't have to run the program, they can just look at your code and say "here is where you messed up".

In terms of programming, we need to show the errors and warnings that we got when we ran the code. We can do this by using the reprex package.

Load the reprex library into your R session by using the library() command in the console.

Run search() in the console and copy and paste the output below. "package:reprex" should be included in it.

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 3)

A reprex, or reproducible example, is a code snippet that contains the output of every function as a comment, making it easy to show off code and its output without making it hard to copy-paste and run in its own session. You can learn more about reprex and its uses here.

Exercise 6

Now let's create a reprex. Select all the text in your Script file. Then go to "Addins" at the top of RStudio.

Click "Render Reprex". Under "Where is reprex source?" select "current selection". Under "Target venue", make sure "Github or Stack Overflow" is chosen. Also make sure "Preview HTML" is selected.

Then click Render.

Your Reprex is now copied to your clipboard. You can also see it in the "Viewer" tab in the bottom right. Simply paste it below.

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 5)

Notice that when you go to "Render Reprex", you can tailor your reprex for different places. For example, you can tailor it for GitHub, or you can tailor it as a Slack Message. Keep this in mind when creating your reprex depending on where you want to post it and ask your question.

Exercise 7

That isn't the only way to create a reprex however...

Copy all of the code in script.R onto your clipboard then run reprex() in the Console.

This should have copied the reprex onto your clipboard. Paste the reprex text below.

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 5)

This makes it much easier to create a reprex automatically, but it doesn't allow you to set the source. It's only going to pull the code from the clipboard, so make sure to double-check your reprex before you start to do anything.

See here to learn more about reprex, its features, and some shortcuts.

This video is another very useful resource.

Exercise 8

There are a variety of on-line communities at which you might post your question and the accompanying reprex. We recommend Posit Community.

Go to this thread and post your reprex as a reply.

Copy/paste the URL for your reply below.

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 3)

If that post is not accepting your reply, don't worry about it. Posit closes off threads after a few weeks. Just skip this question.

Investing in yourself

You should also spend some time preparing yourself to solve problems before they occur. Investing a little time in learning R each day will pay off handsomely in the long run.

Exercise 1

Keep up with news about the Tidyverse by reading the tidyverse blog. Paste the URL for the latest post here.

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 3)

Exercise 2

To keep up with the R community more broadly, we recommend reading R Weekly: it’s a community effort to aggregate the most interesting news in the R community each week. Paste the URL for the latest issue below.

question_text(NULL,
    answer(NULL, correct = TRUE),
    allow_retry = TRUE,
    try_again_button = "Edit Answer",
    incorrect = NULL,
    rows = 3)

Summary

This tutorial covered material from Chapter 8 Workflow: getting help in R for Data Science (2e) by Hadley Wickham, Mine Çetinkaya-Rundel, and Garrett Grolemund. This tutorial gave you a basic understanding of how to search for help and create reproducible examples. The most important lesson was to make extensive use of generative artificial intelligence, generally via large language models (LLMs) like ChatGPT or Bard.




Try the r4ds.tutorials package in your browser

Any scripts or data that you put into this service are public.

r4ds.tutorials documentation built on April 3, 2025, 5:50 p.m.