将输入存储为数值以在 Shiny 中生成三个表

store input as numeric value to generate three tables in Shiny

我想创建一个大 table 以在 Shiny 应用程序中创建一些 table 之后 table。

这是我的一部分 server.R:

function(input, output) {
    output$year <- renderText(input$year)

    ################################
    # CONFLICTING PART OF THE CODE
    year <- reactive({
      as.character(input$year)
    })

    matrix = tbl_df(dbReadTable(rca_matrices_db, reactive(paste0("table_",year))))
    ################################

    my_table = matrix %>% ... BLA BLA BLA

    output$more_than_10 <- DT::renderDataTable(DT::datatable({
      mytable %>% select(X1,X2) %>% filter(X1 > 10)
    }))

    output$less_than_10 <- DT::renderDataTable(DT::datatable({
      mytable %>% select(X1,X2) %>% filter(X1 < 10)
    }))    
  }
)

year来自ui.R

的这一部分
sidebarPanel(
    selectInput('year', 'Year', c("Select year",1999:2015), selected = 1999)
  )

如果我在 server.R 的冲突部分替换

year 变量
year <- 2000

然后就可以了

有什么想法吗?

问题是

    matrix = tbl_df(dbReadTable(rca_matrices_db, reactive(paste0("table_",year))))

没有反应。每当反应年份发生变化时,它都不会更新。此外,正如评论中已经指出的那样,要调用反应式 year 的值,您需要使用 year()。因此,您也需要使 my_table 成为反应式,可能如下所示:

my_table <- reactive({ 
    my_matrix = tbl_df(dbReadTable(rca_matrices_db, reactive(paste0("table_",year()))))
    my_table = my_matrix %>% ... BLA BLA BLA
    return (my_table)
})

现在,my_table() 的值将在 year() 更改时更新,随时 input$year 更改。 (请注意,您也可以直接将 input$year 放在这里,而不是将 year() 设为单独的反应式)。

所以你现在可以这样做:

output$more_than_10 <- DT::renderDataTable(DT::datatable({
  mytable() %>% select(X1,X2) %>% filter(X1 > 10)
}))

这将在反应性 mytable() 变化的任何时候更新,正如我们刚刚注意到的那样,随着“input$year”的变化而变化。希望这对您有所帮助!