r - 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)
解决方案
欢迎来到stackoverfow。
为了拥有一个功能强大的应用程序,您必须更改一些内容。我把我看到的东西的总结和代码中的细节作为注释放在这里。
- 准备你的数据,你应该考虑创建变量
- 不要将具有重复值的整个向量作为
choices
参数放在 a 中selectInput
,您应该传递不同的选项。 - 尽可能选择一个值是个好主意。这样,您的应用程序将启动并显示为默认值。
- 使用输入过滤数据。
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)
推荐阅读
- python - 如何将基类的实例设置为子类的属性?
- assembly - 如何仅使用 64 位寄存器将 1 字节值存储在 64 位寄存器中?
- php - PHP-将输入保存到具有日期名称的单独文件中
- android - 如何从android devide获取纬度和经度?
- javascript - Lodash Js 用数组和对象创建树结构
- python - 卡在 Qt Designer - 连接按钮
- c++ - 我想在屏幕上看到球体的运动(OpenGL)
- java - Spring 从 Retrofit 获取 Null 值
- swift - 我何时以及为什么要在 Swift 中使用协议?
- php - 如果类别不活跃,则和产品设置不活跃 Laravel