library(tidyverse)
library(shiny)
# Loading datasets
<- mtcars
cars
# Define UI for dataset viewer app ----
<- fluidPage(
ui
# App title ----
titlePanel("R Shiny Example"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Text for providing a caption ----
# Note: Changes made to the caption in the textInput control
# are updated in the output area immediately as you type
textInput(inputId = "caption",
label = "Caption",
value = "Datasets"),
# Input: Selector for choosing dataset ----
selectInput(inputId = "dataset",
label = "Choose a dataset:",
choices = c("Cars")),
# Input: Numeric entry for number of obs to view ----
numericInput(inputId = "obs",
label = "Number of observations to view:",
min=0,
value = 10),
# Input: Selector for x variable
selectInput(inputId = "xvariable",
label = "Choose the x variable:",
choices = names(cars)),
# Input: Selector for y variable
selectInput(inputId = "yvariable",
label = "Choose the y variable:",
choices = names(cars))
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Formatted text for caption ----
h3(textOutput("caption", container = span)),
# Output: Verbatim text for data summary ----
verbatimTextOutput("summary"),
# Output: HTML table with requested number of observations ----
tableOutput("view"),
# Output: Graph with selected variables
plotOutput("plot")
)
)
)
# Define server logic to summarize and view selected dataset ----
<- function(input, output) {
server
# Return the requested dataset ----
# By declaring datasetInput as a reactive expression we ensure
# that:
#
# 1. It is only called when the inputs it depends on changes
# 2. The computation and result are shared by all the callers,
# i.e. it only executes a single time
<- reactive({
datasetInput switch(input$dataset,
"Cars" =cars)
})
# Create caption ----
# The output$caption is computed based on a reactive expression
# that returns input$caption. When the user changes the
# "caption" field:
#
# 1. This function is automatically called to recompute the output
# 2. New caption is pushed back to the browser for re-display
#
# Note that because the data-oriented reactive expressions
# below don't depend on input$caption, those expressions are
# NOT called when input$caption changes
$caption <- renderText({
output$caption
input
})
# Generate a summary of the dataset ----
# The output$summary depends on the datasetInput reactive
# expression, so will be re-executed whenever datasetInput is
# invalidated, i.e. whenever the input$dataset changes
$summary <- renderPrint({
output<- datasetInput()
dataset summary(dataset)
})
# Show the first "n" observations ----
# The output$view depends on both the databaseInput reactive
# expression and input$obs, so it will be re-executed whenever
# input$dataset or input$obs is changed
$view <- renderTable({
outputhead(datasetInput(), n = input$obs)
})
$plot <- renderPlot({
outputggplot(data = datasetInput(),
aes_string(x = input$xvariable, y = input$yvariable)) +
geom_point() +
geom_smooth(method = "loess")
})
}
# Create Shiny app ----
shinyApp(ui, server)
Shiny applications not supported in static R Markdown documents
# NOTE: CAN'T RUN SHINY APP DIRECTLY ON MARKDOWN
# NEED TO PUBLISH (FOR EXAMPLE TO SHINYAPPS.IO) AND THEN EMBEDD WITH IFRAME