How to convert a Shiny app, consisting of multiple files, into a playable example that can be shared in a question?

Asked

Viewed 323 times

13

Problem

Here at Stackoverflow in English there is guides and also questions whose theme are the reproducible examples. However there is nothing equivalent to the what can make life difficult for beginners and also imply a lower quality of the answers or lack of interest of the most experienced users to answer the question.

Generate a good question for according to site standards can be very difficult since a minimum Shiny example should include a server file. R, ui. R and still need a global.R. Even if the single file pattern is used, an app. A, still there may be a need to include the styles in CSS and also HTML templates. So my general question would be how generate a minimum, complete and verifiable example for the ?

Summary

I would like the answer to be included:

  • general lines of a good question.
  • a reproducible example.
  • R functions and other resources that may be useful.
  • That question shouldn’t be on the finish line?

  • 1

    No. It is about a technical difficulty how to make an application in shiny reproducible. This can be used for Sopt, but also to submit issues to package maintainers, etc.

1 answer

10

Broad lines

The general lines or principles that will guide the construction of the question are the same listed here, here and in the link quoted in the question. What will change in the is as implement these principles.

These principles can be summarised as follows::

  1. Share the data used (or at least a part of them);
  2. Share a minimum code which reproduces the problem
  3. Share the result obtained (generated error messages, warnings or screens);
  4. Share what would be the expected result with the shared code;
  5. Share the session information (version of , used packages) with sessionInfo().

How items 1 and 5 are solved in the same way as for the in general, the rest of the response focuses on items 2 to 4.

Say the question was:

How to allow the user to choose a data set in a ?

Minimum code

Step 1: Reproduce the problem

In order for others to help, they will need to reproduce their problem. So start a new file (app.R) and insert in it the code of your .

At this stage if there is a global.R in that , this will be the first content of the new script. Then copy and paste the contents of ui.R and server.R defining the variables ui and server. End up calling the function shinyApp(ui, server) with the newly created objects.

The great advantage of presenting your application in a single script is that it facilitates the life of those who will answer the question, as just a Ctrl + c and Ctrl + v to get the application running and start working on solving the problems presented.

Thus we would end up with a content similar to what follows in app.R.

# antigo global.R
library(shiny)
library(lubridate)

gerar_valores <- function(x) {
  rnorm(x, 10, 2)
}

set.seed(123)

# antigo ui.R
ui <- fluidPage(
  column(
    6,
    sliderInput("n", "Quantidade de observações", 30, 100, 65),
    plotOutput("hist")
  ),
  column(
    6,
    selectInput("dados", "Selecione o conjunto de dados",
                choices = list(carro = mtcars, flores = iris)),
    plotOutput("plot")
  )
)

# antigo server.R
server <- function(input, output, session) {
  output$hist <- renderPlot({
    hist(gerar_valores(input$n))
  })

  output$plot <- renderPlot({
    plot(input$dados)
  })
}

shinyApp(ui, server)

Step 2: Isolate the problem

In the application of the above script we have two inputs and two outputs, but the problem is related only to a set of input/output.

Providing all the application code in question, before helping will mess up. This is because whoever is going to address the problem will have an extra job (identify which part of the script comes from the error). In some cases, this can even cause you to give up helping.

To isolate the problem is enough get rid of all the code that has no relation to it. In the above example this means removing all of the code related to the creation of histograms both in the ui how much in the server. Then we would have:

# antigo global.R
library(shiny)

# antigo ui.R
ui <- fluidPage(
  selectInput("dados", "Selecione o conjunto de dados",
              choices = list(carro = mtcars, flores = iris)),
  plotOutput("plot")
)

# antigo server.R
server <- function(input, output, session) {
  output$plot <- renderPlot({
    plot(input$dados)
  })
}

shinyApp(ui, server)

Result obtained

To share the result obtained with the shared code there are at least 3 options.

  1. Describe unwanted behavior found;

  2. Share a link to the application with the problem. Among the services that may host your can I quote the shinyapps.io and the now.sh.

  3. Share a screenshot of the application with the problem.

Regardless of the method you choose, don’t forget to share error messages or warnings from both the console and the ui of the application.

In the above case we would have something like:

The list of options, instead of displaying the names of the data sets, presents a series of names and numbers, as shown below. I also get these errors on my console:

Warning in min(x) : no missing argument for min; returning Inf

Warning in max(x) : no missing argument for max; returning -Inf

Warning: Error in Plot.window: finite values are required for 'ylim' [No stack trace available]

tela de erro

Expected result

To explain the result I expected I see 3 options:

  1. Describe in words the expected result;

  2. Share a link to a case (another application or page) that is serving as inspiration;

  3. Rotate the desired result outside the and share it.

In the fictional example, it could be something like.

I get the result I’m waiting for with the command

plot(mtcars)

Why doesn’t it work inside the ?

Other materials (HTML/CSS/JS)

The same principles used for the code can be used for other languages: share only the essential and if you are problem-related shared. Share each into a piece of code within the question. Something like:

This is the that I’m wearing:

.resultado {
  background-color: green;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.