Shiny applets are defined by two files, ui.R and server.R. These two files are enough to create a simple shiny applet.
- ui.R defines the page layout and user interface
- server.R contains the R code to create any output
Shiny applets are defined by two files, ui.R and server.R. These two files are enough to create a simple shiny applet.
library(shiny) # A simple/common user interface template shinyUI(fluidPage( # Application title titlePanel("Title"), sidebarPanel( # Define some inputs here ), mainPanel( # output (from the server) go here ) ))
Defines the shinyUI()
function, which contains elements that describe the HTML page
Typical applets are constructed using the pageWithSidebar()
function, which creates a side bar generally used for input and a main panel used for output
Elements are hierarchical functions: components in the sidebar go into the sidebarPanel()
function, separated by commas
Input variables are named in ui.R and then referenced in server.R
User interface code for creating Example_01.r:
ui <- fluidPage( # Application title titlePanel("Summary Statistics"), # Sidebar panel sidebarLayout( sidebarPanel( # Option to upload file fileInput('data', 'Please upload your data file', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')), # Numeric option to change n parameter in head() function numericInput('headcount', 'How many rows would you like to display', 6, min = 1, max = 100) ), # Main panel mainPanel( verbatimTextOutput("sum") # Displays the summary text ) ), tableOutput('contents') # Displays table )
library(shiny) # Server side logic shinyServer(function(input, output) { # do something })
shinyServer()
, which is a function with input and output variables.Server code for creating Example_01.r:
server <- function(input, output) { # Define dataframe to be accessible for later use dataframe <- reactive({ dataset <- input$data if (is.null(dataset)) return(NULL) read.csv(dataset$datapath, header=T) }) # Renders summary output output$sum <- renderPrint({ summary(dataframe()) }) # Renders table preview with input parameter output$contents <- renderTable({ head(dataframe(),input$headcount) }) }
Using Example_01.R:
# In user interface numericInput('headcount', 'How many rows would you like to display', 10, min = 1, max = 100)
Input
Output
There are many additional ways for users to interact with shiny!
Shiny applets work because of reactive expressions, which automatically update outputs when input values change.
input values => R code => output values
Reactive expressions keep track of what values they read and what values they change. If those values become "out of date", they know their return value is out of date and will automatically recalculate.
We can create a reactive expression by passing a normal expression into reactive.
dataInput <- reactive({ c("Nike","Reebok","Under Armor") })
This statement stores the corresponding data into the variable dataInput which we can then call later in our server code.
Reactive values can be turned into output objects that are passed to the shinyServer function.
This output depends on both the dataset and the number of observations.
output$view <- renderTable({ head(dataInput(), n = input$obs) })
Whenever either the dataset or the number of observations changes, this function will be re-executed and the output will change.
Explore the code for the Example_01.r applet.
ggplot2
dataframe
# In server dataframe <- reactive({ dataset <- input$data if (is.null(dataset)) return(NULL) read.csv(dataset$datapath, header=T) })
# In user interface plotOutput("histplot") # In server output$histplot <- renderPlot({ qplot(dataframe()[,8], main = "Histogram of At Bats for MLB Data Set")+ labs(x = "Number of At Bats Per Season", y = "Frequency") })