【问题标题】:Creating a reactive data frame for shiny: object 'input' not found为闪亮创建反应数据框:找不到对象“输入”
【发布时间】:2022-01-12 21:06:04
【问题描述】:

我正在构建一个闪亮的应用程序,它使用单个数据框来填充一组带有下拉菜单的图,以进行简单的过滤。原始设置调用定义的绘图生成函数,该函数将数据帧作为参数传递,执行过滤 -> 聚合 -> 绘图输出。我希望通过将反应数据框传递到我的绘图函数来提高性能,这样过滤/聚合只需要发生一次,而不是每个单独的绘图发生 6 次。这是我尝试解决此问题的一个示例:

library(dplyr)
library(shiny)

### Data ###

colA <- c("A", "A", "B", "B")
colB <- c("w", "x", "y", "z")
colC <- c(1, 2, 3, 4)

testDF <- data.frame(colA, colB, colC)

reactiveTestDF <- reactive({
    testDF %>%
        filter(colA == input$filterDropDown)
})

### Application UI ###

ui <- fluidPage(

    titlePanel("test"),

    # Sidebar filter dropdown
    sidebarLayout(
        sidebarPanel(
            selectInput("filterDropDown", "filters", choices = c("A", "B"))
            )
        ,

        # Show data frame
        mainPanel(
           tableOutput("testTable")
        )
    )
)

### App Server ###

server <- function(input, output) {

    output$testTable <- renderTable(reactiveTestDF())
    #output$testTable <- renderTable(testDF)
}

# Run the application 
shinyApp(ui = ui, server = server)

我在运行上面的应用程序时收到此错误(注释掉的行运行良好):

Warning: Error in : Problem with `filter()` input `..1`.
i Input `..1` is `colA == input$filterDropDown`.
x object 'input' not found
  126: <Anonymous>

这是创建基于输入参数(在本例中为下拉列表)过滤自身的数据框的正确方法吗?

【问题讨论】:

    标签: r dplyr shiny


    【解决方案1】:

    您只需将reactive 部分移动到server

    library(dplyr)
    library(shiny)
    
    ### Data ###
    
    colA <- c("A", "A", "B", "B")
    colB <- c("w", "x", "y", "z")
    colC <- c(1, 2, 3, 4)
    
    testDF <- data.frame(colA, colB, colC)
    
    
    ### Application UI ###
    
    ui <- fluidPage(
      
      titlePanel("test"),
      
      # Sidebar filter dropdown
      sidebarLayout(
        sidebarPanel(
          selectInput("filterDropDown", "filters", choices = c("A", "B"))
        )
        ,
        
        # Show data frame
        mainPanel(
          tableOutput("testTable")
        )
      )
    )
    
    ### App Server ###
    
    server <- function(input, output) {
      reactiveTestDF <- reactive({
        testDF %>%
          filter(colA == input$filterDropDown)
      })
      
      output$testTable <- renderTable(reactiveTestDF())
     
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    
    

    【讨论】:

      猜你喜欢
      • 2015-04-13
      • 1970-01-01
      • 2021-02-09
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      • 2016-02-10
      相关资源
      最近更新 更多