Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Shiny app consisting of multiple files into an easily shareable and reproducible Shiny example?

Tags:

r

r-faq

shiny

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 shiny 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. shiny 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 shiny app into a good reproducible example?

like image 933
Florian Avatar asked Jan 19 '18 14:01

Florian


People also ask

How do you run a shiny app with two files?

To create a two-file app, create a new directory (for example, newdir/ ) and place two files, called ui. R and server. R , in the directory. To run it, call runApp("newdir") .

How do you organize a shiny app?

If you are creating a large or long-term Shiny app, I highly recommend that you organise your app in the same way as an R package. This means that you: Put all R code in the R/ directory. Write a function that starts your app (i.e. calls shinyApp() with your UI and server).

What are the main 2 parts of an R shiny app?

Shiny applications have two components, a user interface object and a server function, that are passed as arguments to the shinyApp function that creates a Shiny app object from this UI/server pair.


1 Answers

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;}'))),
like image 97
8 revs, 2 users 91% Avatar answered Oct 17 '22 22:10

8 revs, 2 users 91%