Site icon R-bloggers

Exploring Distributions with {shiny} and {TidyDensity}

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< section id="introduction" class="level1">

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.

< section id="example" class="level1">

Example

< section id="required-packages" class="level2">

Required Packages

Before we dive into the code, let’s go over the packages that we’ll be using in this app:

Load them up!

library(shiny)
library(DT)
library(tidyverse)
library(TidyDensity)
< section id="the-ui-object" class="level2">

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:

Here’s the code for the UI object:

# Define UI
ui <- fluidPage(
  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")
    )
  )
)
< section id="the-server-object" class="level2">

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:

Here’s the code for the server object:

# Define server
server <- function(input, output) {
  
  # Create reactive data
  data <- reactive({
    # Call selected function with user input
    match.fun(input$function)(.num_sims = input$num_sims, .n = input$n)
  })
  
  # Create density plot
  output$density_plot <- renderPlot({
    # Call autoplot on reactive data
    p <- data() %>%
      tidy_autoplot()
    
    print(p)
  })
  
  # Create data table
  output$data_table <- DT::renderDataTable({
    # Return reactive data as a data table
    DT::datatable(data())
  })
  
}
< section id="conclusion" class="level1">

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
ui <- fluidPage(
  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"),
      DT::dataTableOutput("data_table")
    )
  )
)

# Define server
server <- function(input, output) {
  
  # Create reactive data
  data <- reactive({
    # Call selected function with user input
    match.fun(input$functions)(.num_sims = input$num_sims, .n = input$n)
  })
  
  # Create density plot
  output$density_plot <- renderPlot({
    # Call autoplot on reactive data
    p <- data() |>
      tidy_autoplot()
    
    print(p)
  })
  
  # Create data table
  output$data_table <- DT::renderDataTable({
    # Return reactive data as a data table
    DT::datatable(data())
  })
  
}

# Run the app
shinyApp(ui = ui, server = server)

Voila!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version