R-Shiny使用Reactive renderUI值

时间:2014-11-21 03:10:09

标签: r shiny rstudio

如何在反应式包装器中使用从renderUI元素获取的值?

即。我的代码:

CompanyNames <- sqlQuery(connection, "SELECT Companynm FROM RiskMgm_Company")

output$CompNameSelector <- renderUI({ 
selectInput("compName","Company Name:",as.vector(CompanyNames[,1]))
})

 CompID <- reactive({
CompID <<- sqlQuery(paste("SELECT CompanyID FROM RiskMgm_Company WHERE Companynm = '",compName,"'"))
})

output$MotorSelector <- renderUI({
selectInput("MachSer","Machine:",sqlQuery(connection,paste("SELECT Motor_func FROM RiskMgm_Motor WHERE Company_ID='",CompID,"'")))
})

我的错误:

Successfilly opened connection to db
Error in paste("SELECT CompanyID FROM RiskMgm_Company WHERE Companynm = '",  : 
could not find function "compName"

我做错了什么?基本上我想要的是SQL查询给出的公司列表。然后,根据所选择的公司,它将在下一个下拉框中显示属于该公司的电机

由于

1 个答案:

答案 0 :(得分:7)

您可以通过ID来引用元素,例如input$compName。作为一个人为的例子 是一个简单的闪亮应用程序,有两个selectInput&#39; s。第二个selectInput选项取决于第一个renderUI的值。引用由library(shiny) myDF <- data.frame(A = 1:4, B = 3:6, C = 6:9, D = 10:13) runApp( list( ui = fluidPage( uiOutput("myList"), uiOutput("myNumbers") ) , server = function(input, output, session){ output$myList <- renderUI({ selectInput("compName", "Company Name:", LETTERS[1:4]) }) output$myNumbers <- renderUI({ selectInput("compNo", "Product Line:", myDF[, input$compName]) }) } ) ) 创建的窗口小部件的输出与引用相同的窗口小部件(如果它们从头开始在UI.R中)没有什么不同:

{{1}}