R-Shiny中的IN运算符

时间:2014-06-01 01:42:00

标签: r shiny sqldf

我正在按照以下代码对组件进行多项选择。

selectInput("cmpnt",
            "Choose Component:",
            choices = as.character(levels(Material_Data()$CMPNT_NM)),multiple = TRUE)

但我正在尝试编写一个如下所示的sql语句,然后它无效。它既没有抛出任何错误信息。当我一次选择一个选项时(没有多个= TRUE)然后它正在工作(因为我正在使用" ="运算符)。但在使用" multiple = TRUE"我需要使用IN运算符,它不起作用。

Input_Data2 <- fn$sqldf( paste0( "select * from Input_Data1 where MTRL_NBR = '$mtrl1' and CMPNT_NM in ('$cmpnt1')") )

提前感谢您提供任何帮助。

谢谢jdharrison!请查看详细代码:

# server.R
library(RODBC)
library(shiny)
library(sqldf)

Input_Data <- readRDS("InputSource.rds")

Mtrl <- factor(Input_Data$MTRL_NBR)
Mtrl_List <- levels(Mtrl)

shinyServer(function(input, output) {

# First UI input (Service column) filter clientData
output$Choose_Material <- renderUI({
if (is.null(clientData()))
  return("No client selected")
selectInput("mtrl",
            "Choose Material:",
            choices = as.character(levels(clientData()$MTRL_NBR)),
            selected = input$mtrl

)
})

# Second UI input (Rounds column) filter service-filtered clientData
output$Choose_Component <- renderUI({
if(is.null(input$mtrl))
return()

if (is.null(Material_Data()))
  return("No service selected")

selectInput("cmpnt",
            "Choose Component:",
            choices = as.character(levels(Material_Data()$CMPNT_NM)),multiple = TRUE)

})

# First data load (client data)
clientData <- reactive({
#    get(input$Input_Data)
return(Input_Data)
})

# Second data load (filter by service column)
Material_Data <- reactive({
dat <- clientData()
if (is.null(dat))
  return(NULL)
if (!is.null(input$mtrl)) # !
  dat <- dat[dat$MTRL_NBR %in% input$mtrl,]
  dat <- droplevels(dat)
  return(dat)
})

output$Choose_Columns <- renderUI({

if(is.null(input$mtrl))
return()
if(is.null(input$cmpnt))
return()

colnames <- names(Input_Data)

checkboxGroupInput("columns", "Choose Columns To Display The Data:",
                choices  = colnames,
                selected = colnames)
})

output$text <- renderText({ print(input$cmpnt)   })

output$data_table <- renderTable({
 if(is.null(input$mtrl))
  return()

if (is.null(input$columns) || !(input$columns %in% names(Input_Data)))
  return()

Input_Data1 <- Input_Data[, input$columns, drop = FALSE]
cmpnt1 <- input$cmpnt
mtrl1 <- input$mtrl

Input_Data2 <- fn$sqldf( paste0( "select * from Input_Data1 where MTRL_NBR = '$mtrl1'      and CMPNT_NM in ('$cmpnt1')") )
head(Input_Data2, 10)
})
})

其实我正在寻找表中的字符输入检查。这就是我想检查&#34; IN&#34; sql staement中的运算符。以下陈述是否有任何问题:

  CMPNT_NM in ('$cmpnt1')

1 个答案:

答案 0 :(得分:3)

library(shiny)
library(sqldf)
myDf <- data.frame(a = 1:10, b = letters[1:10])

runApp(list(
  ui = bootstrapPage(
    selectInput('let', 'Pick some letters', letters[1:10], multiple = TRUE),
    tableOutput('table')
  ),
  server = function(input, output) {
    output$table <- renderTable({
      dumLET <- input$let
      print(fn$paste("select * from myDf where b IN ('$dumLET')"))
      fn$sqldf("select * from myDf where b IN ('$dumLET')")
    }
    )
  }
)
)

运行这个简单的例子,选择一个有效的字母。超过一个字母

[1] "select * from myDf where b IN ('a')"
[1] "select * from myDf where b IN ('c(\"a\", \"e\")')"
Warning in matrix(align.tmp[(2 - pos):(ncol(x) + 1)], nrow = nrow(x), ncol = ncol(x) +  :
  data length exceeds size of matrix
[1] "select * from myDf where b IN ('c(\"a\", \"e\", \"c\")')"
Warning in matrix(align.tmp[(2 - pos):(ncol(x) + 1)], nrow = nrow(x), ncol = ncol(x) +  :
  data length exceeds size of matrix
[1] "select * from myDf where b IN ('c(\"a\", \"e\", \"c\", \"d\")')"

注意

select * from myDf where b IN ('c(\"a\", \"e\", \"c\")')

对此的一个简单修复是替换

dumLET <- input$let
# with      
dumLET <- paste(input$let, collapse = '\',\'')

所以尝试替换

cmpnt1 <- input$cmpnt
#with
cmpnt1 <- paste(input$cmpnt, collapse = '\',\'')