从一列中选择并传递另一列的值

问题描述

我正在尝试从MysqL和Shiny上的列中选择多个值。 从MysqL数据库查询时,某些带有特殊字符的名称不返回值。 如何选择名称,但将地理名称而不是名称传递给查询

      library(shiny)
      library(dplyr)
      library(sqldf)
      library(DT)
      library(stringr)

       survey <- data.frame("name" = c("Oberösterreich","Северозападен","Κύπρος","Strední Cechy","Severovýchod","Praha"),"geo" = c("AT31","BG31","CY00","    CZ02","CZ05","CZ01"),"population" =c(100409314,54086980,30961705,164741605,156857074,93166890))



          shinyApp(
             ui = fluidPage(
            fluidRow(
              wellPanel(
                  selectizeInput(
                                'Region',label = "Region Select",choices = NULL,options = list(
                                 placeholder = 'Select Region',maxOptions = 1000,maxItems = 10,searchConjunction = 'and'
                                   )
                                    ))),fluidRow(DT::dataTableOutput('table')),),server = function(input,output,session){

                updateSelectizeInput(session,"Region",server = TRUE,choices = survey$`name`)

           geonamesdata <- reactive({
            SelectedRegion <-
          stringr::str_c(stringr::str_c("'",input$Region,"'"),collapse = ',')

        sqldf(paste0("
            SELECT disTINCT c.name,c.geo 
            FROM survey c
                   WHERE c.name IN (",SelectedRegion,")
     "))
})           
             
output$table <- DT::renderDataTable(geonamesdata(),selection = 'single',options = list(searching = FALSE,pageLength = 10),server = FALSE,escape = FALSE,rownames= FALSE)      
             

  })

编辑

我想出了另一个演示来说明我想要的东西。

我有一个MysqL数据库,但出于这个问题的目的,我将使用在Shiny环境中语法相似的sqlDF。

    library(shiny)
    library(dplyr)
    library(sqldf)
    library(DT)
    library(stringr)

        df <- data.frame(empName = c("Jon","Bill","Maria"),empID = c("J111","B222","M333"),empAge = c(23,41,32),empSalary = c(21000,23400,26800)
                        )


       shinyApp(

          ui = fluidPage(

                        selectizeInput("Search",label = p("Select name"),choices = as.character(df$empName),multiple = TRUE),hr(),fluidRow(
                  column(6,DT::dataTableOutput("table1")),column(6,DT::dataTableOutput("table2"))),fluidRow(
                    column(6,DT::dataTableOutput("table3")),DT::dataTableOutput("table4"))
                     )),session) {  
               output$table1 = DT::renderDataTable({ df },options = list(dom = 't'))
                  

            df2 <-  reactive ({ 
                (df %>% filter(empName %in% input$Search)%>% select(empID))  
                       })

          output$table2 = DT::renderDataTable({
                       req(input$Search)
                   df2()},options = list(dom = 't'))

          df3 <- reactive({
             if (input$Search != "") {     
                   sqldf(paste0("SELECT  *  
                        FROM df WHERE  empName  LIKE '%",input$Search,"%'"))  
               }})                

    output$table3 = DT::renderDataTable({ 
                req(input$Search)
                  df3()},options = list(dom = 't'))


          df4 <- reactive ({
                   Selectednames <-stringr::str_c(stringr::str_c("'",')
  

sqldf(paste0("SELECT  empAge,empSalary  
                 FROM df  WHERE  empName IN (",Selectednames,")  "))
          })     



             output$table4 = DT::renderDataTable({ 
                  req(input$Search)
                 df4()},options = list(dom = 't')) 
   })

我正在使用MySQL查询。 在表1中,显示的数据是整个员工数据框,我无法在数千行中做到这一点。

在表2中,我从selectize中选择员工姓名,但显示相应的ID。

在表3中,它仅显示selectize中的一个选定值。

在表4中,该代码允许从selectizeInput的多个选择中查询其他详细信息。

我正在寻找的是能够从selectizeInput中选择多个名称,但是将相应的多个员工ID传递到MySQL查询以获得如表4所示的结果。

因此,基本上可以结合使用选择名称功能,但可以传递Id列的值以允许在查询中进行多重选择。

解决方法

我简化了您的反应式表达式,添加了Geo作为初始选择,并将selectizeInput移到了服务器端。试试这个。

编辑:我已经更新了答案,以删除初始选择。

  ###### updated answer on 8Sep2020
  survey <- data.frame(name = c("Oberösterreich","Северозападен","Κύπρος","Strední Cechy","Severovýchod","Praha"),geo = c("AT31","BG31","CY00","CZ02","CZ05","CZ01"),population =c(100409314,54086980,30961705,164741605,156857074,93166890))
  
  
  ui = fluidPage(
    fluidRow(
      wellPanel(
        uiOutput("regionorgeo")
      )),fluidRow(DTOutput('table')),)
  
  server = function(input,output,session){
    
    output$regionorgeo <- renderUI({
  
      selectizeInput(
        'Geo',label = "Geo Select",choices = survey$geo,selected=1,options = list(
          placeholder = 'Geo Region',maxOptions = 1000,maxItems = 10,searchConjunction = 'and'
        )
      )
      
    })
    
    geonamesdata <- reactive({
      req(input$Geo)
      data <- filter(survey,geo %in% as.character(input$Geo))
      data
    })
    
    output$table <- DT::renderDataTable(geonamesdata(),selection = 'single',options = list(searching = FALSE,pageLength = 10),server = FALSE,escape = FALSE,rownames= FALSE)
    
  }
  
  shinyApp(ui,server)

output

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...