1

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?

pachadotdev
  • 3,345
  • 6
  • 33
  • 60
  • 1
    hi try to use `year()` instead of `year` in `paste0...` – s.brunel Aug 10 '17 at 07:07
  • hi, thanks, I tried and the problem persists :S its complicated because I want to add more user selectable parameters – pachadotdev Aug 10 '17 at 07:09
  • 1
    maybe this will help there is a reactive query https://stackoverflow.com/questions/31446500/acessing-sql-database-using-shiny-with-reactive-query – s.brunel Aug 10 '17 at 07:25

1 Answers1

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!

Florian
  • 24,425
  • 4
  • 49
  • 80
  • thanks !! that unlocked creating a general table dynamically, now I cannot create smaller tables from that because of error `cannot coerce type 'closure' to vector of type 'character', and that's a new question – pachadotdev Aug 10 '17 at 16:42
  • 1
    Could you post that as a new question on Stack Overflow? I can take a look at it when I'm home, if no one else has. – Florian Aug 10 '17 at 16:58
  • thanks a lot !! I already did ... so now I'll start posting replies to pure R questions, that is what I do know, the question is here https://stackoverflow.com/questions/45619547/shiny-dynamic-table-error-cannot-coerce-type-closure-to-vector-of-type-chara – pachadotdev Aug 10 '17 at 17:03