I'm using a postgres db for a shiny app, and I'm having trouble getting a dplyr query to work.
I have the following reactive. si.division is a dataframe, and input$si_division is a select input:
si_division_selected <- reactive({
si.division %>%
filter(division_name %in% input$si_division) %>%
select(division_code) %>%
unlist(use.names = FALSE)
})
I'm trying to pass this into a dplyr query using src_pool
industry_division_code <- src_pool(pool) %>% tbl("si_alldata") %>%
translate_sql(division_code %in% si_division_selected()) %>%
select(industry_code)
I'm getting the following error:
Error in UseMethod: no applicable method for 'select_' applied to an object of class "c('sql', 'character')
I have also tried:
industry_division_code <- src_pool(pool) %>% tbl("si_alldata") %>%
filter(division_code %in% si_division_selected()) %>%
select(industry_code)
Which returns:
Error in postgresqlExecStatement(conn, statement, ...) : RS-DBI driver: (could not Retrieve the result : ERROR: syntax error at or near "SI_DIVISION_SELECTED" LINE 5: WHERE ("division_code" IN SI_DIVISION_SELECTED()))
If I load the file into R instead of using the database I have no issues:
industry_division_code <- si_alldata %>%
filter(division_code %in% si_division_selected()) %>%
select(industry_code)
translate_sql(and why with two sets of parenthesis) instead offilter?%>% explainon it? Also, how many elements are insi_division_selected?%in%won't work with less than 2 elements.<SQL> SELECT "industry_code" AS "industry_code" FROM (SELECT * FROM "si_alldata" WHERE ("division_code" = SI_DIVISION_SELECTED()))si_division_selected <- si_division_selected()Then pass:industry_division_code <- collect(src_pool(pool) %>% tbl("si_alldata") %>% filter(division_code == si_division_selected) %>% select(industry_code))