Shine bright like an open source app

Ottavia M. Epifania

The very first app

appExamples/basic-App.R


ui = fluidPage(selectInput(inputId = "mySelection",
                          label = h3("Select box"),
                          choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3),
                          selected = 1),
         verbatimTextOutput( outputId = "myOutput")
         )
server = function(input, output) {
           output$myOutput = renderPrint({
             paste(paste("This is my choice"),  input$mySelection)
           })
}
shinyApp(ui, server)
1
Define the ID of the input in the UI (internal use)
2
Define the label of the element (external use)
3
Options (labels) and their internal labels
4
Default selected option
5
Define the label (internal use) of the outuput in the UI (where the results elaborated by the server will be stored)
6
Function in the server to render the results (consistent with the UI definition)
7
Call for the label of the input in the UI

App layout

fluidPage()

fluidRow(
  column(6, ),
  column(6, )
)

sidebarLayout(
sidebarPanel(...),
mainPanel(...)
)

# and many more!
1
Rows with columns within 12-unit wide grid
2
Custom the page in two columns of width 6 out of the 12-unit on a single row
3
Automatic layout with sidebar and main area

Complete list (look for “Application layout”)

The app


appExamples/first-app.R

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(    
      selectInput(inputId = "dataset",  
                  label = "Choose a dataset:", 
                  choices = c("rock", "pressure", "cars")) 
    ),
    mainPanel(
      plotOutput( "graph"  ),
      verbatimTextOutput( "summary")
    ) 
  )
)
1
Define that specific type of output labeled “graph” is a graph
2
Define that specific type of output labeled “summary” is a code
server <- function(input, output){
  output$graph <- renderPlot({
   data =  switch(input$dataset,
           rock = rock,
           pressure = pressure,
           cars = cars)
    plot(data[, c(1:2)])
  })
output$summary <- renderPrint({
   data =  switch(input$dataset,
           rock = rock,
           pressure = pressure,
           cars = cars)
    summary(data[, c(1:2)])
  })
}
1
renderPlot is the function in the server mirroring the plotOuput function in the UI
2
renderPrint is the function in the server mirroring the verbatimTextOutput in the UI
shinyApp(ui, server)

Don’t repeat yourself

The “shelf” solution

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "dataset",
                  label = "Choose a dataset:", 
                  choices = c("rock", "pressure", "cars")) 
    ),
    mainPanel(
      plotOutput("graph"),
      verbatimTextOutput("summary")
    ) 
  )
)
server <- function(input, output){ 
values <- reactiveValues()
  dataInput <- reactive({
     data =  switch(input$dataset,
           rock = rock,
           pressure = pressure,
           cars = cars)
  })
observe({
    values$data <- data.frame(dataInput())
  })
output$graph <- renderPlot({
       plot(values$data[, c(1:2)])
    })
output$summary <- renderPrint({
    summary(values$data[, c(1:2)])
})
}
1
Define a variable as reactiveValues (i.e., a variable that reacts to what happen in the setting configuration of the UI)
2
Define the data as reacting to what happens in the UI
3
Link the reactive variable with the data input (i.e., create the shelf)
4
Use the shelf for the analysis
shinyApp(ui, server)

reactive()`

ui = fluidPage(
    sidebarLayout(sidebarPanel(
        selectInput(inputId = "dataset",
                    label = "Choose a dataset:",
                    choices  = c("rock", "pressure", "cars"))
      ),
mainPanel(
      plotOutput("graph"), 
      verbatimTextOutput( 
          "summary"   
      ))))
server <- function(input, output){
    dataInput <- reactive({
    data =  switch(input$dataset,
           rock = rock,
           pressure = pressure,
           cars = cars)
    })
output$graph <- renderPlot({
      data <- dataInput()
      plot(data[, c(1:2)])
    })
output$summary <- renderPrint({
      data <- dataInput()
      summary(data[, c(1:2)])
    })
  }
1
Define the reactive dataset with the reactive function
2
Use the reactive dataset for the analysis
shinyApp(ui, server)

Prevent the action

Action buttons

They allow for conditioning a specific action to an event

As such, the app will show the output only if a specific event is observed

Both shelf and reactive solutions work with the action buttons

We are studying the solution with the reactive functions because they are more common and used (more documentation is availble)