Shiny Web Applications
May 5, 2025
shiny::runExample()
shiny-00-old-faithful
.shiny-00-old-faithful
)app.R
app.R
.app.R
, click the Run App button at the top-right corner of the Script Pane. This previews your application locally.rsconnect::writeManifest()
in R Console.
manifest.json
that will tell Connect Cloud (1) what version of R to use and (2) what packages and versions are required.library(shiny)
library(tidyverse)
library(viridis)
faithful <- as_tibble(datasets::faithful[, 2])
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 60,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
v_colors <- viridis(input$bins,
alpha = 1,
begin = 0,
end = 1,
direction = 1, option = "B")
ggplot(faithful, aes(x = value)) +
geom_histogram(bins = input$bins,
fill = v_colors) +
labs(x = 'Waiting time to next eruption (in mins)',
y = "Count",
title = 'Histogram of waiting times') +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5,
size = rel(1.5),
face = 'bold',
margin =
margin(0,0,30,0)),
axis.title.x = element_text(size = rel(1.5),
margin =
margin(30,0,0,0)),
axis.title.y = element_text(size = rel(1.5),
margin =
margin(0,30,0,0)) ,
axis.text.x = element_text(size = rel(1.5)),
axis.text.y = element_text(size = rel(1.5)) )
})
}
# Run the application
shinyApp(ui = ui, server = server)
main
)app.R
as the primary filestorm
data.frame as an example.app.R
file or in separate ui.R
and server.R
files.There are two convenient ways to create a new shiny app.
shinyapp
and select the shinyapp {snippet}
Let’s do Shiny Exercise 1!
We will be using the storms
data set distributed with the dplyr
package as an example.
ui
specifies the elements of your application and their arrangement.
textOutput()
plotOutput()
tableOutput()
*Output()
function has a corresponding render*()
function. For example:
textOutput()
→ renderText()
plotOutput()
→ renderPlot()
tableOutput()
→ renderTable()
Let’s do Shiny Exercise 2!
Let’s do Shiny Exercise 3!
textInput()
selectInput()
fileInput()
server
function via the input argument.str
to examine inputs;
str(reactiveValuesToList(input))
will show the current input names and values.Let’s do Shiny Exercise 4!
Let’s do Shiny Exercise 5!
Javascript is the language of the web, and many of the most popular javascript libraries can be used directly from R.
DT
The DataTables
javascript library can be used to create interactive tables directly from R. Features include:
Interacting with tables updates input, enabling integration with Shiny.
See https://shiny.rstudio.com/articles/datatables.html for more.
plotly
event_data
function for retrieving values produced by interacting with the plot.
plotly::plotly_example("shiny", "event_data")
in R Console.Let’s do Shiny Exercise 6!
DataTables
, interacting with leaflet maps updates input, enabling interacting with shiny.Let’s do Shiny Exercise 7!
ggplot
charts: lines, dots, bars, maps, and more