0
votes

I'm trying to figure out how to make a ggplot output reactive to the varSelectInput function in the UI of my shiny app. For example, I would like to be able to select two different variables and see them plotted as a function of each other. I've used the flights dataset for this example, ie., select arr_time and dep_delay and see arr_time on the x axis and dep_delay on the y axis.

The DateRangeInput has no function for now, but I would eventually like to be able to filter out the results that are plotted in the ggplot output by month.

library(tidyverse)
library(shiny)
flights <- nycflights13::flights

# Define UI for application
ui <- navbarPage(
    "NYC Flights",

    tabPanel(
        "Flights",
        sidebarPanel(
            h4("Flight Inputs"),
            selectInput(
                "Airline_Select",
                label = "Select Airline",
                choices = flights$Carrier,
                selected = TRUE
            ),
            dateRangeInput(
                "dates",
                label = "Dates",
                start = min(flights$Month),
                end = max(flights$Month),
                min = min(flights$Month),
                max = max(flights$Month)
            ),
            varSelectInput(
                "X_Axis",
                label = "Select Variable 1",
                data = flights
            ),
            varSelectInput(
                "Y_Axis",
                label = "Select Variable 2",
                data = flights
            ),
        ),
    )
)

mainPanel(plotOutput("flights_plot"))


# Define server logic
server <- function(input, output) {

    output$flights_plot <- renderPlot({
        ggplot(data = flights, aes(x = input$X_Axis, y = input$Y_Axis)) + geom_point()
    })
}
# Run the application 
shinyApp(ui = ui, server = server)
1

1 Answers

3
votes

Welcome to stackoverlfow.

There are a few things you have to change in order to have a functional app. I put here a summary of the things I saw, and details within the code as comments.

  1. Prepare your data, you should consider creating variables
  2. Do not put a whole vector with duplicated values as choices argument in a selectInput, you should pass the distinct options.
  3. It is a good idea to select a value whenever is possible. that way your app will launch with something to show as default.
  4. Use the inputs to filter the data.
  5. selectInput create a string value, there for, your should use aes_string in your ggplot mapping argument.
library(shiny)
library(tidyverse)


# You have to Adequate your data: You have to create a dete variable
# in order to make the `dateRangeInput` work. You can do that using
# `year`, `month` and `day` variables as follow.
flights <- nycflights13::flights %>% 
    mutate(
        date = as.Date(paste(year, month, day, sep = "-"))
    )

ui <- navbarPage(
    title = "NYC Flights",
    tabPanel(
        title = "Flights",
        sidebarPanel(
            h4("Flight Inputs"),
            # The choices argument should be the unique
            # list of arilines, not the whole vector with
            # duplicates
            selectInput(
                "airline",
                label = "Select Airline",
                choices = unique(flights$carrier), 
                selected = 'UA' # It is a good idea to select a value
                # visible when you launch the app
            ),
            dateRangeInput(
                "dates",
                label = "Dates",
                start = min(flights$date),
                end = max(flights$date)
                ),
            varSelectInput(
                "X_Axis",
                label = "Select Variable 1",
                data = flights,
                selected = "date" # selecting one
            ),
            varSelectInput(
                "Y_Axis",
                label = "Select Variable 2",
                data = flights,
                selected = "dep_delay" # selecting one
            )
        ),

        mainPanel(
            plotOutput("plot")
        )

    )

)

server <- function(input, output, session) {

    output$plot <- renderPlot({
        flights %>% 
            # Use your inputs to filter the data
            filter(date >= input$dates[1], date <= input$dates[2], carrier == input$airline) %>% 
            # since the selectImput create a character element, you should use 
            # ase_string() to map the x an y variables
            ggplot(aes_string(x = input$X_Axis, y = input$Y_Axis)) +
            geom_point()
    })

}

shinyApp(ui, server)