-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Creating a Reproducible Example
If you need to ask for help, you are much more likely to get help if you have a minimal reproducible example ("reprex") which other people can copy and paste to see the same problem. When the code is minimal, it makes it easy for others to see where the problem is. And if the problem can be reproduced by copying and pasting, it makes it much easier for others to debug the problem.
Here is an example. If you copy and paste the code in an R console, the application will show an error: non-numeric argument to binary operator
.
shinyApp(
ui = fluidPage(
selectInput("n", "N", 1:10),
plotOutput("plot")
),
server = function(input, output, session) {
output$plot <- renderPlot({
n <- input$n * 2
plot(head(cars, n))
})
}
)
Because anyone can reproduce the problem by just copying and pasting the code, they can easily explore your code and test possible solutions.
If you don't know what part of your code is triggering the problem, a good way to find it is to remove sections of code from your application, piece by piece, until the problem goes away. If removing a particular piece of code makes the problem stop, it's likely that that code is related to the problem. Once you've isolated the code that triggers the problem, you can go on to the next step.
To create a minimal example, you can go one of two ways: you can either keep removing code from your application until all the extraneous parts are gone, or you can construct a new Shiny application and copy the problem code into the new application. It's generally easier for others to understand the problem when you construct a new example, although either way is much better than not having a reproducible example at all.
If you want to construct a minimal example from scratch, you can start with this template of very basic application, which you can always find by running ?shinyApp
(that shows the documentation for the function).
shinyApp(
ui = fluidPage(
numericInput("n", "n", 1),
plotOutput("plot")
),
server = function(input, output, session) {
output$plot <- renderPlot( plot(head(cars, input$n)) )
}
)
Start with the template and modify it by adding code that reproduces your problem. In many cases, the act of creating a reproducible example will reveal to you the cause of the problem, before you even ask for help.
If your reproducible example uses a data set that is stored on your computer, it will be difficult for others to reproduce the problem, because they won't be able to just copy and paste your code; they'll have to download the data, run R in the correct directory, and so on.
You can make it easier for others by providing a data set in a way that can be copied and pasted. This can be either your original data set, or a simplified version of it that still causes the issue. For example, if you have a data frame with 1,000 rows, you might try to take the first 20 rows and see if that will still cause the problem. In general it is best to provide a simplified version of the data set, so that it is easy for others to understand it.
Option 1: Provide regular R code that generates a data set. For example:
mydata <- data.frame(x = 1:5, y = c("a", "b", "c", "d", "e"))
Option 2: Use dput()
. If you have an object x
, calling dput(x)
will print out code that will generate an object just like x
. For example, if you use dput(mydata)
, it will print the following:
> dput(mydata)
structure(list(x = 1:5, y = structure(1:5, .Label = c("a", "b",
"c", "d", "e"), class = "factor")), class = "data.frame", row.names = c(NA,
-5L))
If you run the structure(list(....))
code, it will return a data frame exactly like mydata
. Once you have that code, you can put this in your application to generate mydata
:
mydata <- structure(list(x = 1:5, y = structure(1:5, .Label = c("a", "b",
"c", "d", "e"), class = "factor")), class = "data.frame", row.names = c(NA,
-5L))
If dput()
on your original data generates too much code, try taking a subset of your data and see if it will reproduce the problem.
Please bear in mind that dput()
is not perfect, and cannot faithfully reproduce every kind of R object. It will, however, work for most objects, like data frames and vectors.
Option 3: If your data set can't be put in your code in a way that can be copied and pasted, you might have to just upload it so that others can download it. It is best to avoid this if possible.
Users should not share personal or protected information in topic threads or data. What if you need to share contact info or an ip, or discuss data for an example related to a protected dataset? What if you see a violation of this policy? Check out https://community.rstudio.com/t/personally-identifiable-protected-information/9993 guide and our Privacy Policy