install.packages("reprex")
library(reprex)
3 Getting Help
This section is also available as slides in this presentation.
Getting help in R and with code in general can be tricky. Not because people are not willing to help, but because it is hard for someone else to replicate the problem you are facing. Between environment settings, package versions and the code itself, many elements need to be matched for someone else to be able to reproduce the issue.
However, reproducing the issue is the first step to solving it. Depending on how help is asked, the chances of getting a useful answer can vary greatly. For example, sending a screenshot is rarely helpful, as it is not possible to copy-paste code from it. Similarly, sending a script that does not load the necessary packages or data is not helpful either, as the person trying to help will not be able to run the code. But there are also simple ways to make code/problems reproducible and to make it easier for others to help you, this chapter will guide you through them.
3.1 How to ask for help
How to ask for help depends on the type of task that is being performed. Guidance on how to ask for help can be divided into three categories. For each one of them, there are specific instructions on how to ask for help:
- Setup problems: These are problems related to the installation of R, RStudio, or a package, or to the configuration of your environment. For example, package installation that fails, a project environment that is not working, authentication issues etc. See instructions here.
- Specific R problems: Problems related to a specific task to perform. For example, an error message not understood, a plot that is not coming out as expected, etc. See instructions here.
- General R questions: Not related to a specific problem, but rather to the language itself. This type of question should not depend on any element of context that is specific to an environment or project. For example, how to use a specific function, how to make a pdf table from a dataframe, how to export a plot to a file, etc. See instructions here.
3.1.1 Setup problems
When facing setup problems, the best way to ask for help is to provide as much information as possible about the environment. This includes:
- The operating system,
- The version of R,
- If trying to install a package, the version of the package,
- The steps taken and what was already tried,
- The error message displayed or the problem faced.
Easily get versions information by following these instructions.
Because this kind of problem is often impossible to reproduce on another computer, the more information is provided, the better. This will help the person trying to help to understand the context and to provide a solution.
For this type of problem, information can be shared as text or screenshots. However, text is always preferred, so prioritize it when possible.
3.1.2 Specific R problems
When facing specific R problems, the best way to ask for help is to provide a reproducible example. This is a piece of code that reproduces the problem you are facing. A reproducible example should be:
- Minimal: Include only the necessary code to reproduce the problem.
- Complete: Include all necessary packages and data.
- Reproducible: Include code that can be run by someone else to reproduce the problem.
Creating a reproducible is easier when using the {reprex}
(for reproducible example) package. Here’s how to do it:
3.1.2.1 Install and load the {reprex}
package
Make sure you have the reprex package installed and load it.
3.1.2.2 Create a reprex
To create a reprex, you can use the reprex()
function. This function will take the code you provide, run it, and format it in a way that is easy to read and copy-paste. Here’s how to use it:
reprex({
<- 1:4
y mean(y + x)
})
<- 1:4
y mean(y + x)
#> Error: object 'x' not found
Notice that the error message is also included in the reprex output. This way, the person trying to help can see the problem but also directly copy-paste the code on its computer.
3.1.2.3 Good Reprex
A good reprex should include all the necessary code to reproduce the problem: 1. Libraries 2. Data 3. Code
Here’s an example:
reprex({
# Library
library(ospsuite)
# Data
<- system.file("extdata",
filePath "Diclofenac_PK_data_complete.xlsx",
package = "ospsuite.parameteridentification")
# Code
<- createImporterConfigurationForFile(filePath = filePath)
importConfig
<- loadDataSetsFromExcel(xlsFilePath = filePath,
obsData importerConfigurationOrPath = importConfig,
importAllSheets = TRUE)
})
3.1.2.4 Good practices
- When possible, use example or built-in data (mtcars dataset, aciclovir simulation, etc.)
- Remove all unnecessary code to make it easy for other to understand what your code does.
3.1.3 General R questions
Before asking a general R question, it is important to check if the answer is already available. This can be done in this suggested order:
- Access functions documentation with
?function_name
orhelp("function_name")
in the console, or by using theHelp
tab directly in RStudio.. - Reading and searching package documentation. Most packages have well designed documentation websites that include examples and explanations. (.i.e ggplot2, dplyr, readr, etc.)
- There’s some chance that someone else already asked this question so searching the web (Google, StackOverflow, Github issues, RStudio Community forum etc.) is often a good option.
If after these steps you still have a question, use the dedicated company channels to ask your question to colleagues.
Search engine do not always reference packages websites well. Another way to find them is to go to the package’s GitHub repository. The url to the documentation website is usually listed there.
This is the recommended method for OSP and ESQlabs packages.
3.2 Where to ask for help
Once you wrote your reprex or your general question, you can head to the dedicated R support Teams Channel (Modeling and Simulation | R Support) (use the search feature) and share it there to get help from your colleagues:
- Create a Code block,
- Paste and pick language,
- Add short description of what you are trying to do and attach necessary files,
- Click on post.
3.3 Tips and Tricks
3.3.1 Get session information
To get session information, use the sessionInfo()
function. This will print out all the information about your R session, including the version of R, the version of the loaded packages, etc.
sessionInfo()
When creating a reprex()
, this information can be added by setting session_info = TRUE
.
reprex({
<- 1:4
y mean(y + x)
session_info = TRUE) },