首页 > 解决方案 > R Shiny ggplot 对 varSelectInput 反应

问题描述

我试图弄清楚如何使 ggplot 输出对我闪亮的应用程序 UI 中的 varSelectInput 函数产生反应。例如,我希望能够选择两个不同的变量并将它们绘制为彼此的函数。我在此示例中使用了航班数据集,即选择 arr_time 和 dep_delay,然后在 x 轴上查看 arr_time,在 y 轴上查看 dep_delay。

DateRangeInput 目前没有功能,但我最终希望能够过滤掉按月绘制在 ggplot 输出中的结果。

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)

标签: rggplot2shiny

解决方案


欢迎来到stackoverfow。

为了拥有一个功能强大的应用程序,您必须更改一些内容。我把我看到的东西的总结和代码中的细节作为注释放在这里。

  1. 准备你的数据,你应该考虑创建变量
  2. 不要将具有重复值的整个向量作为choices参数放在 a 中selectInput,您应该传递不同的选项。
  3. 尽可能选择一个值是个好主意。这样,您的应用程序将启动并显示为默认值。
  4. 使用输入过滤数据。
  5. selectInput创建一个字符串值,您应该aes_string在 ggplotmapping参数中使用。
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)


推荐阅读