【发布时间】:2020-05-07 22:40:38
【问题描述】:
我正在尝试创建一个闪亮的应用程序,其中 ggplot 是主要的绘图输出,并且我已经达到了用户可以使用 dateRangeInput 按日期过滤,以及使用 selectVarInput 选择 X 和 Y 轴的地步。但是,在主要的 ggplot 输出中,我希望该图具有颜色美感,可以根据日期分离不同的响应。这是我到目前为止所拥有的,但返回一条错误消息说“美学必须是长度 1 或与数据 (10) 相同:颜色”。任何帮助将不胜感激!
library(tidyverse)
library(shiny)
sampledf <- tibble(Name = c("John Smith"),
`Test 1` = c("Test 1"),
`Date` = lubridate::as_date(c("2020-04-22","2020-04-22", "2020-04-22", "2020-04-24", "2020-04-24", "2020-04-24", "2020-04-24", "2020-04-26", "2020-04-26", "2020-04-26")),
`Result 1` = rnorm(1:10),
`Result 2` = rnorm(1:10),
`Result 3` = rnorm(1:10))
# Define UI for application
ui <- navbarPage(
"Title",
tabPanel(
"Tab 1",
sidebarPanel(
h4("Inputs"),
selectInput(
"Name_Select",
label = "Select Name",
choices = sampledf$Name,
selected = TRUE
),
dateRangeInput(
"dates",
label = "Dates",
start = min(sampledf$Date),
end = max(sampledf$Date)
),
varSelectInput("X_Axis",
label = "Select Variable 1",
data = sampledf,
),
varSelectInput("Y_Axis",
label = "Select Variable 2",
data = sampledf,
),
)
),
mainPanel(plotOutput("plot")),
tabPanel("Tab2")
)
# Define server logic
server <- function(input, output) {
output$plot <- renderPlot({
Data %>%
filter(sampledf$Date >= input$dates[1], sampledf$Date <= input$dates[2]) %>%
ggplot(mapping = (aes_string(x = input$X_Axis, y = input$Y_Axis))) +
geom_line(color = input$dates) +
geom_point(color = input$dates)
})
}
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】: