Shiny building blocks

# R ENVIRONMENT
library(shiny)

# UI, this is a function call, so contents are elements = need commas between
ui <- fluidPage()

# SERVER, this is a function definition, so contents are objects
server <- function(input, output){}

shinyApp(ui = ui, server = server)

Our components

In the R environment

We want to load our packages, maybe set up some data, e.g.:

library(shiny)
library(leaflet)

In the UI

We want to include:

  • site dropdown – selectInput()
  • number of widgets input – numericInput()
  • air temperature input – numericInput()
  • add data button – actionButton()
  • map with sites – leaflet
  • graph with number v. temperature – plotOutput()
  • table with date, site, number, Temperature – tableOutput() or dataTableOutput()
  • download .csv button – downloadButton()

In the Server

We want to include:

Our development steps

  1. Set up basic interface, spit out inputs in a table – test
  2. Set up our database interface – test
  3. Set up leaflet interface – test
  4. Combine them – test

1. Basic interface

See above. To start, we’ll make it just output user input into a table – test

2. Database choose your own adventure

First, check out the Remote Data document

This will get you started on data storage

Then, we’ll run a test

In a new R file, try this:

source("global.R")
current_time <- as.character(Sys.time())
test_data <- data.frame(addtime=c(current_time,current_time), 
  site=c("Site1","Site2"), 
  weather=c(2,3), 
  nwidgets=c(10,12.3))
save_db(test_data)
xx <- load_db()
print(xx)

Does it work?

3. leaflet interface

A basic map – test

library(leaflet)
m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m  # Print the map

(from RStudio)

Then, we can add a leaflet map to Shiny

4. Put it together

Some pseudocode – Actions and reactions

  • User clicks add data button
    • server adds data to database
    • redraw data table
    • redraw graph
  • User clicks on a site on the map
    • filter table data
    • filter graph data
  • User clicks on download data button
    • grab data in data table
    • download to file