library(shiny)
library(shinythemes)
<- fluidPage(
ui theme = shinythemes::shinytheme("spacelab"),
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "y",
label = "Y-axis:",
choices = c(
"IMDB rating" = "imdb_rating",
"IMDB number of votes" = "imdb_num_votes",
"Critics Score" = "critics_score",
"Audience Score" = "audience_score",
"Runtime" = "runtime"
),selected = "audience_score"
),selectInput(
inputId = "x",
label = "X-axis:",
choices = c(
"IMDB rating" = "imdb_rating",
"IMDB number of votes" = "imdb_num_votes",
"Critics Score" = "critics_score",
"Audience Score" = "audience_score",
"Runtime" = "runtime"
),selected = "critics_score"
),selectInput(
inputId = "z",
label = "Color by:",
choices = c(
"Title Type" = "title_type",
"Genre" = "genre",
"MPAA Rating" = "mpaa_rating",
"Critics Rating" = "critics_rating",
"Audience Rating" = "audience_rating"
),selected = "mpaa_rating"
),sliderInput(
inputId = "alpha",
label = "Alpha:",
min = 0, max = 1,
value = 0.4
),sliderInput(
inputId = "size",
label = "Size:",
min = 0, max = 5,
value = 3
),textInput(
inputId = "plot_title",
label = "Plot title",
placeholder = "Enter text to be used as plot title"
),actionButton(
inputId = "update_plot_title",
label = "Update plot title"
)
),mainPanel(
br(),
p(
"These data were obtained from",
a("IMBD", href = "http://www.imbd.com/"), "and",
a("Rotten Tomatoes", href = "https://www.rottentomatoes.com/"), "."
),p(
"The data represent",
nrow(movies),
"randomly sampled movies released between 1972 to 2014 in the United States."
),plotOutput(outputId = "scatterplot"),
hr(),
p(em(
"The code for this Shiny application comes from",
a("Building Web Applications with shiny",
href = "https://rstudio-education.github.io/shiny-course/"
)
))
)
)
)
<- function(input, output, session) {
server
<- reactive({
new_plot_title ::toTitleCase(input$plot_title)
tools|>
}) bindEvent(input$update_plot_title,
ignoreNULL = FALSE,
ignoreInit = FALSE
)
$scatterplot <- renderPlot({
outputscatter_plot(
# load movies data
df = movies,
x_var = input$x,
y_var = input$y,
col_var = input$z,
alpha_var = input$alpha,
size_var = input$size
+
) ::labs(title = new_plot_title()) +
ggplot2::theme_minimal() +
ggplot2::theme(legend.position = "bottom")
ggplot2
})
}
shinyApp(ui = ui, server = server)
shiny-app-frameworks
Shiny frameworks
This website is a resource for developers to get an ‘under the hood’ view of four popular Shiny frameworks.
gap
: a golem
app-package (download gap)
lap
: a leprechaun
app-package (download lap)
cap
: a charpente
app-package (download cap)
rap
: a rhino
app (pseudo-package)1 (download rap)
Source application
The original data and code for each application comes from Building Web Applications With Shiny,2 but has been adapted to provide a basic (but realistic) example with modules, utility function, tests, etc.
scatter_plot()
gives us an example utility function to build with our applications.
<- function(df, x_var, y_var, col_var, alpha_var, size_var) {
scatter_plot ::ggplot(data = df,
ggplot2::aes(x = .data[[x_var]],
ggplot2y = .data[[y_var]],
color = .data[[col_var]])) +
::geom_point(alpha = alpha_var, size = size_var)
ggplot2
}
The application also uses an external dataset.3