R-Shiny 使用 Reactive renderUI 值 [英] R-Shiny using Reactive renderUI value

查看:32
本文介绍了R-Shiny 使用 Reactive renderUI 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do you use values obtained from a renderUI element in a reactive wrapper?

即我的代码:

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 查询给出的公司列表.然后根据选择的公司,它将在下一个下拉框中显示属于该公司的电机

What am I doing wrong? Essentially what I want is a list of companies given by the SQL query. Then depending on the Company selected it will show the motors that belong to that company in the next dropdown box

谢谢

推荐答案

您可以通过元素的 id 来引用元素,例如 input$compName.作为一个人为的例子在这里是一个简单的闪亮应用程序,带有两个 selectInput.第二个 selectInput 选项取决于第一个的值.引用由 renderUI 创建的小部件的输出与引用从一开始就在 UI.R 中的相同小部件没有什么不同:

You would refer to the elements by their id for example input$compName. As a contrived example here is a simple shiny app with two selectInput's. The second selectInput choices depend on the value of the first. Referencing the output of widgets created by renderUI is no different from referencing the same widgets if they had been in UI.R from the beginning:

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])
      })
    }
    )
  )

这篇关于R-Shiny 使用 Reactive renderUI 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆