应用过滤器后,shiny plotly 未能主动绘制系列线图

shiny plotly failed to plot the series line plot actively after applying the filter

我有一个 dafaram,看起来像这样:

df <- data.frame(Date = rev(seq(as.Date("2020-01-01"),
                                as.Date("2021-08-15"),"day")),
                 Var1 = sample(10:100, 593, replace = TRUE), 
                 Var2 = sample(10:100, 593, replace = TRUE), 
                 Var3 = sample(10:100, 593, replace = TRUE), 
                 Var4 = sample(10:100, 593, replace = TRUE)
)

df$CovidPeriod <- ifelse((df$Date>=as.Date("2020-03-01", format="%Y-%m-%d") & 
                            df$Date<=as.Date("2020-09-30", format="%Y-%m-%d")), 
                          "Covid", "NonCovid")

现在我想做的是在闪亮的 flexdashboard 中创建 plotly 时间序列图表。

我试过这个:

library(tidyverse)
library(plotly)
library(flexdashboard)
library(shiny)

selectInput("Series1", label = "Select Series:", choices = c("Select", "Var1", "Var2", "Var3", "Var4",), selected="Select")
 
checkboxInput("covid", "Exclude covid period?", FALSE)
output$value <- renderText({ input$covid })

output$df2 <- reactive({
    if(input$covid == 'Covid'){ 
    filter(df, CovidPeriod == "NonCovid")
  }
})


renderPlotly({
  plot_ly(df2(), x = ~df2$Date, y = ~df2[[input$Series1]], type='scatter', mode = 'lines', name = 'Actual',
          line=list(color='rgb(56, 98, 249)', width=4)
          ) %>%
      layout(title = "Series",
             xaxis = list(title = "Year"),
             yaxis = list(title = "Series excluding Covid time")
             )
})

我遇到错误:

  Warning: Error in df2: could not find function "df2"
  106: is.data.frame
  105: plot_ly
  102: renderPlotly [<text>#38]
  101: func
   98: shinyRenderWidget
   97: func
   84: renderFunc
   83: output$out7e4e6b71a6155385
    3: <Anonymous>
    1: rmarkdown::run

有人能帮忙画出选择用于过滤 covid 时期或整个系列(未应用任何过滤)的系列基础吗?

我稍微编辑了你的代码,你可以很容易地用flexdashboard

申请
library(shiny)
library(plotly)
library(tidyverse)


df <- data.frame(Date = rev(seq(as.Date("2020-01-01"),
                                as.Date("2021-08-15"),"day")),
                 Var1 = sample(10:100, 593, replace = TRUE), 
                 Var2 = sample(10:100, 593, replace = TRUE), 
                 Var3 = sample(10:100, 593, replace = TRUE), 
                 Var4 = sample(10:100, 593, replace = TRUE)
)

df$CovidPeriod <- ifelse((df$Date>=as.Date("2020-03-01", format="%Y-%m-%d") & 
                              df$Date<=as.Date("2020-09-30", format="%Y-%m-%d")), 
                         "Covid", "NonCovid")

ui <- fluidPage(
    titlePanel("Old Faithful Geyser Data"),
    sidebarLayout(
        sidebarPanel(
            selectInput("Series1", label = "Select Series:", choices = c("Select", "Var1", "Var2", "Var3", "Var4"), selected="Var1"),
            checkboxInput("covid", "Exclude covid period?",value = F)
        ),
        mainPanel(
           plotlyOutput("outplot")
        )
    )
)

server <- function(input, output) {

    df2 <- reactive({
        
        newdf <- df
        if(input$covid){ 
            newdf <- filter(df, CovidPeriod == "NonCovid")
        }
        newdf
        
        
    })
    
    output$outplot <- renderPlotly({
        
        filtered_data <- df2()
        plot_ly(filtered_data, x = ~filtered_data$Date, y = ~filtered_data[[input$Series1]], type='scatter', mode = 'lines', name = 'Actual',
                line=list(color='rgb(56, 98, 249)', width=4)
        ) %>%
            layout(title = "Series",
                   xaxis = list(title = "Year"),
                   yaxis = list(title = "Series excluding Covid time")
            )
    })
}
shinyApp(ui = ui, server = server)

请注意,您不应直接调用反应对象。您最好将它们分配给一个带有括号的新对象(我通过“过滤数据”完成)。