store input as numeric value to generate three tables in Shiny

江枫思渺然 提交于 2019-12-11 06:25:28

问题


I want to create a large table to create some tables after that table in a Shiny app.

This is a part of my 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)
    }))    
  }
)

And the year comes from this part of ui.R

sidebarPanel(
    selectInput('year', 'Year', c("Select year",1999:2015), selected = 1999)
  )

If I replace, in the conflicting part of server.R, the year variable for

year <- 2000

then it works

any ideas?


回答1:


The problem is that

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

is not reactive. It will not update whenever the reactive year changes. Also, as already pointed out in the comments, to call the value of the reactive year, you need to use year(). So you need to make my_table a reactive too, maybe as follows:

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)
})

Now, the value of my_table() will update anytime year() changes, which changes anytime input$year changes. (Note, you could also directly put input$year here instead of making year() a separate reactive).

So you can now do:

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

and this will update anytime the reactive mytable() changes, which as we just noticed changes as `input$year' changes. Hope this helps!



来源:https://stackoverflow.com/questions/45606255/store-input-as-numeric-value-to-generate-three-tables-in-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!