简体   繁体   English

下拉列表R中的列名称来自数据库的Shiny SelectInput

[英]Column Names in Dropdown R Shiny SelectInput from data base

I am using the below code to populate the dropdown with database values:- 我正在使用以下代码用数据库值填充下拉列表:-

Server.R 服务器

sqlOutputAssetClass <- reactive({
    sqlInputAssetClass<- paste("select distinct ASSET_CLASS from DUMMY_TABLE",sep="")
    dbGetQuery(con, sqlInputAssetClass)
   })

sqlOutputFeedSrcSys <- eventReactive(input$pick_assetclass, {
    sqlInputFeedSrcSys<- paste("select distinct FEED_SRC_SYS from DUMMY_TABLE where ASSET_CLASS=",
                               "'",
                               input$pick_assetclass,"'",sep="")
    dbGetQuery(con,sqlInputFeedSrcSys)
    })        

observe ({
    updateSelectInput(session,"pick_assetclass","ASSET CLASS",
                      choices = sqlOutputAssetClass())
   })

observe ({
    updateSelectInput(session,"pick_feedsrcsys","FEED SOURCE SYSTEM",
                      choices = sqlOutputFeedSrcSys())
   })

UI.R 用户界面

selectInput('pick_assetclass',label ='Asset Class',
            choices=NULL,selected = NULL, multiple = FALSE,width="450px"),

selectInput('pick_feedsrcsys',label ='Feed Src Sys',
            choices=NULL,selected = NULL, multiple = FALSE,width="450px"),

I see the dropdowns are filled with values but also the column names in the database table. 我看到下拉列表中填充了值,但也填充了数据库表中的列名。 Another strange thing is that when there is just one value in the dropdown then the value doesn't display but the column name displays. 另一个奇怪的事情是,当下拉列表中只有一个值时,该值不会显示,但会显示列名称。

Could you please let me know how to resolve these issues? 您能否让我知道如何解决这些问题?

在此处输入图片说明

在此处输入图片说明

Thanks, 谢谢,

This is not an answer but an extended comment. 这不是答案,而是扩展的评论。

In order to see what values are passed to updateSelectInput , you can put a call to browser() just before the update like so 为了查看传递给updateSelectInput值是什么,您可以像这样在更新之前对browser()进行调用

observe ({
  choices = sqlOutputAssetClass()
  browser()
  updateSelectInput(session,"pick_assetclass","ASSET CLASS",
                   choices = sqlOutputAssetClass()
  )
})

this will open an interactive console where you can print your choices as well as all other inputs/reactives and see which changes are necessary to convert it to a "bare" character sting without labels, attributes or names. 这将打开一个交互式控制台,您可以在其中打印您的选择以及所有其他输入/反应,并查看需要进行哪些更改才能将其转换为没有标签,属性或名称的“裸”字符字符串。 This console will block the console until you click "next" and will be reopened whenever the observe block is run. 该控制台将阻塞该控制台,直到您单击“下一步”为止,并且在运行observe块时将重新打开该控制台。

The object you eventually want to pass to the update function should look like this 您最终要传递给更新功能的对象应如下所示

choices <- c("FX", "CS", "AT", "FUTURES")

From ?selectInput : ?selectInput

choices: ... This can also be a named list whose elements are (either named or unnamed) lists or vectors. 选择:...这也可以是一个命名列表,其元素是(命名或未命名的)列表或向量。 If this is the case, the outermost names will be used as the "optgroup" label for the elements in the respective sublist. 在这种情况下,最外面的名称将用作相应子列表中元素的“ optgroup”标签。 This allows you to group and label similar choices. 这使您可以对相似的选项进行分组和标记。 ... ...

So in your case, dbGetQuery is likely returning a named vector. 因此,在您的情况下, dbGetQuery可能返回命名的向量。 Just run it through unname to strip those names before returning from sqlOutputAssetClass and the category names should go away: 只需通过unname运行它以unname那些名称,然后再从sqlOutputAssetClass返回,类别名称应消失:

sqlOutputAssetClass <- reactive({
    sqlInputAssetClass<- paste("select distinct ASSET_CLASS from DUMMY_TABLE",sep="")
    unname(dbGetQuery(con, sqlInputAssetClass))
   })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM