多个Reactives与ggplot,复选框和日期范围

时间:2017-07-30 02:11:09

标签: r dataframe input ggplot2 shiny

我正在使用Shiny和ShinyDashboard,并且我试图绘制一个geom_bar,其中x值对日期范围做出反应,并且填充是使用checkboxGroupInput对所选项目做出反应。当我在ggplot data =中运行date_data()函数时,我的日期范围工作正常,但是一旦我尝试使用project_data()进行子集化,我的代码开始返回一些奇怪的东西:取决于我如何选中/取消选中框,随机条和填充将会出现,就像在提供的图片(链接下面),而不是我想要的...这里是我的代码

ui <- dashboardPage(
skin = "purple",
dashboardHeader(title = '[enter image description here][1]Dashboard'),
dashboardSidebar(
sidebarMenu(
  menuItem('Dashboard', tabName = 'dashboard', icon = icon('dashboard')),

  menuItem(
    dateRangeInput('date_range', label = "Date Range",format = "mm/dd/yyyy", start = Sys.Date()-17, end = Sys.Date()+17, startview = "month", weekstart = 0, separator = " to ", width = 200)
  ),

  menuItem(
    checkboxGroupInput('project_name', label = "Project", choices = c(unique(allocated$Project)), selected = allocated$Project)
  )
)
),
dashboardBody(
tags$head(
  tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
),

fluidRow(
  box(plotOutput("plot1", width = 1000, height = 500))
),

fluidRow(
  box(plotOutput("plot2", width = 1000, height = 500))
)
)
)



server <- function(input, output) {

output$plot1 <- renderPlot({
date_data <- reactive({
  subset(allocated, variable >= input$date_range[1] & variable <= input$date_range[2])})
project_data <- reactive({subset(date_data(), Project == input$project_name)})

ggplot(data = project_data(), aes(x = variable, y = value, alpha = time, fill = Project)) + 
geom_bar(stat = 'identity') + scale_x_date(name = 'Load Date', breaks = allocated$variable, date_labels =  "%m/%d")

1 个答案:

答案 0 :(得分:0)

问题在于如何在date_data()内对project_data()进行子集化。使用Project == input$project_name,在{。{}完全等于的data.frame中搜索来自ProjectcheckboxGroupInput的输入向量。

相反,请使用input$project_name返回match()中各个值与input$project_name向量中的各个值匹配的索引。

具体而言,将当前定义Project的代码替换为以下行:

project_data()