【问题标题】:Filter dataframe and create a plot based on start and end date过滤数据框并根据开始和结束日期创建图
【发布时间】:2019-03-20 14:53:15
【问题描述】:

我有数据集,我已经转换成以下格式并根据它绘制图表。

    structure(list(Date = structure(c(17833, 17830, 17829, 17828, 
NA), class = "Date"), stocks = structure(c(1L, 1L, 1L, 1L, 1L
), .Label = c("DBS SP Equity", "OCBC SP Equity", "ST SP Equity"
), class = "factor"), cumulative = c(22.99, 23.1, 23.71, 24.1, 
NA), Industry = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("Banks", 
"Telecommunications"), class = "factor")), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))

我有 2 个输入字段:Industry 和 DateRange。

我的意见

selectInput(inputId = "industry2",
                  label = "Industry",
                  choices = input_selection[input_selection !='MarketIndex'],
                  selected = NULL, 
                  multiple = TRUE),
      dateRangeInput('dateRange',
      label = 'Date range input: yyyy-mm-dd',
      start = min(sharesdata_gather$Date), end = max(sharesdata_gather$Date))

我能够为我的原始数据结构中的所有数据绘制 2 个图表 - 行业与日期和库存与日期。
但不能仅针对用户指定的日期绘制图表。我尝试使用子集函数来过滤图形,但得到错误“没有活动的反应上下文不允许操作。(你试图做一些只能从反应表达式或观察者内部完成的事情。)”

我的服务器功能是:

#filtering the data for input start and end date                 
dailyprice_gather <- subset(dailyprice_gather, Date>=input$dateRange[1] )
             dailyprice_gather <- subset(dailyprice_gather, Date<=input$dateRange[2] )
           #grap for Date vs Cumulative for each industry
             output$ind=renderPlot({
            ggplot(data = dailyprice_gather[dailyprice_gather$Industry == input$industry2,]) +
          geom_line(aes(x= Date , y= cumulative, color=Industry) , size=0.25) +

               ggtitle(paste0("Simple Cumulative Return over Years - Industry Wise"))

             })
             #graph for Date vs Stock 
              output$stk =renderPlot({
              ggplot(data = dailyprice_gather[dailyprice_gather$Industry == input$industry2  & dailyprice_gather$stocks == input$equities,])+
          geom_line(aes(x= Date , y= cumulative, color=stocks) , size=0.25) +
              ggtitle(paste0("Simple Cumulative Return over Years - Stock Wise"))

             })

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您正在尝试在任何活动的反应上下文之外使用 input$dateRange[1] - 因此,如果用户更改该值,则不会对其做出任何反应并且 dailyprice_gather不会更新。

    尝试使用

    dailyprice_gather <- reactive({
        d <- subset(<original data name>, Date>=input$dateRange[1] )
        d <- subset(d, Date<=input$dateRange[2] )
        d
    )}
    

    并将其称为 dailyprice_gather()。因此,每当更改日期范围时,上述反应式将失效,并且依赖于它的任何内容都将重新运行。请注意,您需要替换“原始数据名称”。

    【讨论】:

    • @radmuzon,感谢您回答这个问题。但我不是很清楚,我如何在我的代码中实现它。我添加了dailyprice_gather &lt;- reactive({ d &lt;- subset(dailyprice_gather, Date&gt;=input$dateRange[1] ) d &lt;- subset(d, Date&lt;=input$dateRange[2] ) d }) 但我得到'closure'类型的对象不是子集[没有可用的堆栈跟踪]错误。我想我必须在某个地方调用 d() 但不确定在哪里
    • 您想要子集化的原始数据的名称是什么?是dailyprice_gather吗?然后将响应式命名为其​​他名称 - 您可以将响应式视为一个函数,每次日期范围更改时都会返回不同的数据集。您想要子集的数据集应该在原始 subset 中使用,并且应该像函数一样调用响应式。
    猜你喜欢
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2020-03-04
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 2021-07-14
    相关资源
    最近更新 更多