将用户输入向量传递给采用字符串

时间:2018-05-02 15:43:44

标签: r postgresql shiny

我有一个闪亮的应用程序,它接受用户输入: input$libraries是由用户输入

生成的反应性字符向量
output$libraries <- renderUI({
checkboxGroupInput(inputId = "libraries", 
                   label = strong("Select the libraries for which you would like to see part counts"), 
                   choiceValues = LibraryIDs$libraryid,
                   choiceNames = LibraryNames$name, 
                   selected = LibraryIDs$libraryid[1],
                   inline = T)}})
})

我想从我的postgreSQL数据库中选择,我有一个如下设置的功能:

get_query <- function(querystring){
  # create a connection

  # loads the PostgreSQL driver
  drv <- dbDriver("PostgreSQL")
  con <- dbConnect(drv, dbname = "RosettaRelational",
                   host = "localhost", port = 5432,
                   user = "postgres", password = rstudioapi::askForPassword("Database password"))
  on.exit(dbDisconnect(con))

  # check for the existance of tables, must be created in pgAdmin4
  #dbExistsTable(con, "libraries")

  query <- eval(parse(text = querystring))
  return(query)
}

它接受一个字符串并解析它以评估查询

现在当我尝试查询数据库时:

Names <- get_query(paste0("con %>% tbl('libraries') %>% 
filter(libraryid %in% input$libraries) %>% select(name) %>% collect()"))

我收到错误:找不到对象'输入'。我知道它没有正确解析反应特征向量。我应该如何更改它以使其工作?

我试过了:

Names <- get_query(paste0("con %>% tbl('libraries') %>% 
filter(libraryid %in% '",input$libraries,"') %>% select(name) %>% collect()"))

但即使用户选择了多个库,它也只选择向量中的第一个库..当输入$只有一个字符时,这是有效的,例如当输入是操作按钮而不是复选框时

如果用户选择111a,111b,211和input$libraries,当我将var name = "kenny"; function nameLogger() { console.log(this); //is logging the global object console.log(this.name); //undefined } nameLogger(); 传递给字符串时,基本上我需要看起来像c('111a,'111b','211','311a')。 311a,而不仅仅是'111a',这是它目前正在传递的。

1 个答案:

答案 0 :(得分:1)

从我身边的一些测试中可以看出你的代码

        if (count==1) {
        my.stores[[count]]$amount.supply.birth <-  amount.supply.birth
        my.stores[[count]]$price.supply.birth <- price.supply.birth
    }
    else {
            my.stores[[count]]$amount.supply.birth <- c(my.stores[[count-1]]$amount.supply.birth, amount.supply.birth)
            my.stores[[count]]$price.supply.birth <- c(my.stores[[count-1]]$price.supply.birth, price.supply.birth)
    }
对于Names <- get_query(paste0("con %>% tbl('libraries') %>% filter(libraryid %in% '",input$libraries,"') %>% select(name) %>% collect()")) 中的多个库,

将以其当前形式“矢量化”。这将为输入$ libraries中的每个库创建一个单独的字符串,而不是包含所有库的一个字符串。 e.g。

input$libraries

使用我自己的数据和您的建议“> Names [1] "con %>% tbl('libraries') %>% filter(libraryid %in% '111a') %>% select(name) %>% collect()" [2] "con %>% tbl('libraries') %>% filter(libraryid %in% '111b') %>% select(name) %>% collect()" 看起来像c('111a','111b','211','311a')”我将您的代码改编为

input$libraries

这应该给你所需的c('111a','111b','211','311a')。

这不是最优雅但它应该有效。如果内部Names <- get_query(paste0("con %>% tbl('libraries') %>% filter(libraryid %in% c(", paste0("'", input$libraries, "'", collapse = ", "), ")) %>% select(name) %>% collect()")) 看起来很乱,你也可以这样做,如下所示

paste0()

这会给你'111a','111b','211','311a',然后再做

libraries_comma_separated <- paste0("'", input$libraries, "'", collapse = ", ")