library(shiny)
library(DT)
library(tidyverse)
library(TidyDensity)
Introduction
Shiny is an R package that allows you to build interactive web applications using R code. TidyDensity is an R package that provides a tidyverse-style interface for working with probability density functions. In this tutorial, we’ll use these two packages to build a Shiny app that allows users to interact with TidyDensity functions.
Example
Required Packages
Before we dive into the code, let’s go over the packages that we’ll be using in this app:
Shiny: As mentioned earlier, Shiny is an R package for building interactive web applications. It provides a variety of input controls and output elements that allow you to create user interfaces for your R code.
TidyDensity: TidyDensity is an R package that provides a tidyverse-style interface for working with probability density functions. It provides a set of functions for generating density functions, as well as a tidy_autoplot() function for creating visualizations.
tidyverse: Tidyverse is a collection of R packages designed for data science. It includes many popular packages such as ggplot2, dplyr, and tidyr. We’ll be using some functions from the tidyverse packages in our Shiny app.
DT: DT is an R package for creating interactive tables in RMarkdown documents, Shiny apps, and RStudio. We’ll be using the DT::datatable() function to create a table of output data in our app.
Load them up!
The UI Object
The UI object is the first argument of the shinyApp() function, and it defines the layout and appearance of the app. In our TidyDensity Shiny app, we’ll use a sidebar layout with two input controls and two output elements:
Select Function Input: A selectInput() control that allows users to select one of four TidyDensity functions: tidy_normal(), tidy_bernoulli(), tidy_beta(), and tidy_gamma().
Number of Simulations Input: A numericInput() control that allows users to specify the number of simulations to use in the TidyDensity function.
Sample Size Input: A numericInput() control that allows users to specify the sample size to use in the TidyDensity function.
Density Plot Output: A plotOutput() element that displays the density plot generated by tidy_autoplot().
Data Table Output: A dataTableOutput() element that displays the output data from the TidyDensity function.
Here’s the code for the UI object:
# Define UI
<- fluidPage(
ui titlePanel("TidyDensity App"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "function",
label = "Select Function",
choices = c(
"tidy_normal",
"tidy_bernoulli",
"tidy_beta",
"tidy_gamma"
)
),numericInput(inputId = "num_sims",
label = "Number of simulations:",
value = 1,
min = 1,
max = 15),
numericInput(inputId = "n",
label = "Sample size:",
value = 50,
min = 30,
max = 200)
),mainPanel(
plotOutput("density_plot"),
dataTableOutput("data_table")
)
) )
The Server Object
The server object is the second argument of the shinyApp() function, and it defines the behavior and output of the app. In our TidyDensity Shiny app, the server object consists of two reactive expressions that generate the output elements based on the user inputs:
Data Reactive Expression: A reactive expression that generates the output data for the selected TidyDensity function based on the user inputs. We use match.fun() to convert the selected function name into an R function, and we pass the num_sims and n arguments from the input controls.
Density Plot Reactive Expression: A reactive expression that generates the density plot using tidy_autoplot() and the output data from the data reactive expression.
Data Table Output: We use DT::renderDataTable() to generate the data table output element based on the output data from the data reactive expression.
Here’s the code for the server object:
# Define server
<- function(input, output) {
server
# Create reactive data
<- reactive({
data # Call selected function with user input
match.fun(input$function)(.num_sims = input$num_sims, .n = input$n)
})
# Create density plot
$density_plot <- renderPlot({
output# Call autoplot on reactive data
<- data() %>%
p tidy_autoplot()
print(p)
})
# Create data table
$data_table <- DT::renderDataTable({
output# Return reactive data as a data table
::datatable(data())
DT
})
}
Conclusion
In this tutorial, we used Shiny and TidyDensity to build an interactive web application that allows users to generate and visualize probability density functions. We learned how to use the selectInput() and numericInput() controls to allow users to specify the function and input parameters, and we used the plotOutput() and dataTableOutput() elements to display the output data and visualizations. We also used the reactive() function to create reactive expressions that automatically update the output elements based on the user inputs.
Most importantly, steal the code below and see what you can do with it!
library(shiny)
library(TidyDensity)
library(tidyverse)
library(DT)
# Define UI
<- fluidPage(
ui titlePanel("TidyDensity App"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "functions",
label = "Select Function",
choices = c(
"tidy_normal",
"tidy_bernoulli",
"tidy_beta",
"tidy_gamma"
)
),numericInput(inputId = "num_sims",
label = "Number of simulations:",
value = 1,
min = 1,
max = 15),
numericInput(inputId = "n",
label = "Sample size:",
value = 50,
min = 30,
max = 200)
),mainPanel(
plotOutput("density_plot"),
::dataTableOutput("data_table")
DT
)
)
)
# Define server
<- function(input, output) {
server
# Create reactive data
<- reactive({
data # Call selected function with user input
match.fun(input$functions)(.num_sims = input$num_sims, .n = input$n)
})
# Create density plot
$density_plot <- renderPlot({
output# Call autoplot on reactive data
<- data() |>
p tidy_autoplot()
print(p)
})
# Create data table
$data_table <- DT::renderDataTable({
output# Return reactive data as a data table
::datatable(data())
DT
})
}
# Run the app
shinyApp(ui = ui, server = server)
Voila!