帮我解决此错误:
Error in switch(code, ic9 = { : EXPR must be a length 1 vector
当我从一个单选按钮切换到另一个单选按钮时,它应该开始显示所选单选按钮的结果以及与之关联的结果。这就是为什么我使用了开关盒,但它有错误。
CODE:
library(shiny)
library(RPostgreSQL)
shinyServer(function(input, output){
# Return the requested dataset
inputCode <- reactive({
input$code
code <<- {switch(input$code,
i9= ic9,
i10= ic10)}
input$icd
})
dbConn <- function(inputCode,out){
con <- dbConnect(dbDriver("PostgreSQL"), user="postgres", password="xyz", dbname="ICD_9_10_Mapping", host = "localhost", port = 5432)
on.exit(dbDisconnect(con), add=TRUE)
query1 <- "SELECT icd_10_codes, description, flags FROM icd_9_10_dia WHERE icd_9_codes ='"
query2 <- "SELECT icd_9_codes, description, flags FROM icd_10_9_dia WHERE icd_10_codes ='"
switch(code,
ic9 = {X <<- query1},
ic10 = {X <<- query2})
invert <- "'"
stmt <- paste(X,toString(input$icd, width = 10),invert, sep = "")
res <- dbSendQuery(con, statement = stmt)
out <<- fetch(res, n = -1)
}
output$out1 <- renderTable(dbConn(inputCode,out))})
答案 0 :(得分:2)
为了简化这一点,只需使用if语句直接使用输入$代码而不是切换。例如:
if(input$code == 'ic9') {
query <- "SELECT icd_10_codes, description, flags FROM icd_9_10_dia WHERE icd_9_codes ='"
} else {
query <- "SELECT icd_9_codes, description, flags FROM icd_10_9_dia WHERE icd_10_codes ='"
}
您可以在函数中使用input $ code或将其放入对dbConn的调用中(即dbConn(输入$ code))。我没有看到'out'来自哪里。它在dbConn中定义,但您在调用dbConn时使用它,这将无法正常工作。此外,对被动的调用应该是inputCode(),但是使用上面建议的if语句,你不需要它。