Tools for Shiny Apps

Haley Jeppson, Joe Papio,
Sam Tyner

June 15, 2017

Other User Interface Options

  • tabsetPanel() - make multiple different output views (i.e. a plot in one tab, a data table in another)
  • helpText() - create additional text to help users navigate your applet
  • submitButton() - only update outputs when this button is clicked
  • conditionalPanel() - only show certain UI options when conditions are met (i.e. if a certain tab is open, or a certain input is selected)

resource

Getting the shiny app to react: server

  • the server function connects inputs to outputs
  • render functions create outputs of different types
server  <-  function(input, output) {
  # make output objects from inputs  
  
  output$hist <- renderPlot({
    # code for plot
  
  })
}

Render functions

  • renderDataTable() - outputs an interactive, sortable data table
  • renderPlot() - output an R plot
  • renderPrint() - output text from print() in R
  • renderTable() - output an HTML table
  • renderText() - output text from R
  • renderUI() - output a custom part of the user interface
  • renderImage() - print an image to the page
  • htmlOutput() - output html elements

Example server function

server <- function(input, output) {
    output$hist <- renderPlot({
        dist <- rnorm(n = input$num)
        gg <- data.frame(dist) %>% 
          ggplot(aes(x = dist)) + geom_histogram(binwidth = 0.25) +
          xlim(c(-10,10))
        print(gg)
    })
}

Note the use of curly brackets { } in renderPlot.

The last statement in the code block must be the plot.

Your Turn

For the 01_Hello app:

  • expand on the server functionality to make the plot react to the input

An example

runApp("02_Reactivity", display.mode = "showcase")

Your Turn

Using your own data or the NYC crime data provided, create a simple Shiny app. Use the NYC_Emergency app as a starting point.

runApp("03_NYC_Emergency")
  • Ideas:
    • Plot some aspect of the data with color based on another aspect of the data
    • Use subset() and checkboxInput() to plot user-selected subsets
    • Use tabsetPanel() to display different tables/plots
    • Extra Challenging: Can you make a map of NYC crime by location?