29

There are resources on how to create a Minimal, Complete, and Verifiable example in general on Stack Overflow, and on how to make a great R reproducible example. However, there are no similar guidelines for questions, while adhering to certain standards makes it much more likely that quality answers will be given, and thus that your question will be resolved.

However, asking a good Shiny question can be difficult. apps are often large and complex, use multiple data sources, and the code is often split over multiple files, making it difficult to share easily reproducible code with others. Even though a problem may be caused in server.R, the example is not reproducible without the contents of ui.R (and possibly other files like stylesheets or global.R). Copy-pasting the contents of all these files individually is cumbersome, and requires other users to recreate the same file structure to be able to reproduce the problem.

So; how to convert your app into a good reproducible example?

3
  • 3
    Would this be a better question for meta? Jan 23, 2018 at 16:56
  • 2
    Could be, but I might not be the best one to judge that since I rarely visit meta. The help center is pretty broad in its definition of 'good' questions for Stack Overflow. The reason I asked this question here is mainly just that similar questions regarding r and pandas are also on Stack Overflow rather than on meta, so I assumed that was the right thing to do.
    – Florian
    Jan 23, 2018 at 17:35
  • 1
    @Pac0 I do see your point - although I am not sure if I fully agree. Maybe it is a good idea to move this from the comments to a discussion on Meta to also get the opinions of others regarding this question and possible future questions that are similar in nature.
    – Florian
    Feb 6, 2018 at 21:27

1 Answer 1

28

Example data

Of course, all guidelines regarding sample data mentioned in the answer on the question “how to make a great R reproducible example” also hold when creating questions related to Shiny. To summarize: Make sure no additional files are needed to run your code. Use sample datasets like mtcars, or create some sample data with data.frame(). If your data is very complex and that complexity is really required to illustrate the issue, you could also use dput(). Avoid using functions like read.csv(), unless of course you have questions related to functions like fileInput.

Example code

Always reduce your code to the bare minimum to reproduce your error or unexpected behavior. This includes removing calls to additional .CSS files and .js files and removing unnecessary functions in the ui and the server.

Shiny apps often consist of two or three files (ui.R, server.R and possibly global.R), for example this demo application. However, it is preferable to post your code as a single script, so it can easily be run by others without having to manually create those files. This can easily be done by:

  • wrapping your ui with ui <- fluidPage(…),
  • the server with server <- function(input,output, session) {…},
  • and subsequently calling shinyApp(ui, server).

So a simple skeleton to start with could look as follows:

library(shiny)

ui <- fluidPage(

  )

server <- function(input,output,session) {

}

shinyApp(ui, server)

Working Example

So, taking all the above into account, a good Minimal, Complete, and Verifiable example for a Shiny application could look as follows:

library(shiny)

df <- data.frame(id = letters[1:10], value = seq(1,10))

ui <- fluidPage(
  sliderInput('nrow', 'Number of rows', min = 1, max = 10, value = 5),
  dataTableOutput('my_table')
  )

server <- function(input, output, session) {
  output$my_table <- renderDataTable({
    df[1:input$nrow,]
  })
}

shinyApp(ui, server)

Adding CSS

There are multiple ways to add custom CSS to a Shiny application, as explained here. The preferred way to add CSS to a Shiny application in a reproducible example is to add the CSS in the code, rather than in a separate file. This can be done by adding a line in the ui of an application, for example as follows:

tags$head(tags$style(HTML('body {background-color: lightblue;}'))),
5
  • I think the function shinyUI is obsolete and unnecessary.
    – GyD
    Jan 19, 2018 at 15:21
  • @GyD, I think you are right, I updated the answer accordingly.
    – Florian
    Jan 19, 2018 at 15:23
  • @Florian. Maybe you could address when the app required to load a file.
    – MLavoie
    Feb 15, 2018 at 19:30
  • @MLavoie, good suggestion, I was hoping to capture that with "If your data is very complex and that complexity is really required to illustrate the issue, you could also use dput(). Avoid using functions like read.csv()..." Do you think that covers it, or were you thinking of something else?
    – Florian
    Feb 16, 2018 at 10:59
  • Yes I think that will do.
    – MLavoie
    Feb 16, 2018 at 11:02

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.