Building an Interactive Pie Chart with SelectInput in Shiny Applications

Building a Shiny Application with SelectInput for Interactive Pie Charts

======================================================

In this article, we will explore how to create an interactive pie chart using the plotlyOutput and renderPlotly functions in Shiny. We will use the selectInput function to allow users to select a state from a list of options.

Introduction to Shiny Applications

Shiny is a powerful R framework for building web applications. It allows developers to create interactive and dynamic user interfaces that can be easily shared and reused. In this article, we will focus on building an application that displays a pie chart based on the selected state.

Setting Up the Project

To start building our Shiny application, we need to install the required packages. We will use plotly, RColorBrewer, and shiny.

# Install required packages
install.packages("plotly")
install.packages("RColorBrewer")
install.packages("shiny")

Creating the Data

We need to create a dataset that we can use to generate our pie chart. Let’s create a simple dataset with four states: USA, Belgium, France, and Russia.

# Create a dataset
total.death <- c(48, 24, 12, 22)
total.recovered <- c(12, 22, 78, 21)
total.cases <- c(553, 226, 742, 370)

State <- c('USA', 'Belgium', 'France', 'Russia')

df2 <- data.frame(State = State, total.death = total.death, total.recovered = total.recovered, total.cases = total.cases)

Building the User Interface

We need to create a user interface that allows users to select a state from a list of options. We will use the selectInput function to achieve this.

# Create a user interface
ui <- fluidPage(
  selectInput("select", label = h3("Select box"), 
              choices = df2$State, 
              selected = "USA"),
  
  hr(),
  fluidRow(column(3, verbatimTextOutput("value"))),
  mainPanel(
    textOutput("text1"),
    plotlyOutput("mypie")
  )
)

Creating the Server

We need to create a server that will handle user input and generate our pie chart. We will use the renderPlotly function to achieve this.

# Create a server
s <- function(input, output) {
  
  # Render text output
  output$text1 <- renderText({
    txt = as.character(input$select)
    paste("text is", txt)
  })

  # Render plotly output
  output$mypie <- renderPlotly({
    labels <- c("unrecovered", "recovered", "death")
    S <- filter(df2, df2$State == as.character(input$select))
    
    # Unlist the values vector
    S = unlist(S[-1])
    
    plot_ly(labels = ~labels,
            values = ~S, type = 'pie',
            marker = list(colors = brewer.pal(7, "Spectral")))
  })
}

Creating the Shiny Application

We need to create a Shiny application that will run our server. We will use the shinyApp function to achieve this.

# Create a shiny application
shinyApp(ui = ui, server = s)

Conclusion

In this article, we built an interactive pie chart using the plotlyOutput and renderPlotly functions in Shiny. We used the selectInput function to allow users to select a state from a list of options. By following these steps, you can build your own interactive applications with Shiny.

Additional Tips

  • Make sure to check the documentation for each package to learn more about its functions and features.
  • Use meaningful variable names and comments to make your code easier to understand.
  • Test your application thoroughly before sharing it with others.

Last modified on 2024-05-13