Rshiny: How to pass renderUI output to chocies parameter in Selectinput

时间:2015-09-01 22:27:08

标签: r shiny

I am trying to input renderUI output in server.UI as an input for choices= parameter in selectInput. The reason being, i need to use the values selected by user as an input for a function. I see names, ID, attr instead of values when I pass these arguments. Below is my code. Appreciate any help. Thank you

The server.r, lists the searchable fields in pubmed ( Eg:title, ALl fields, # UID etc). These list need to pop up for user UI, and when he selects his fields # of interest. The selection need to be passed to other function.

At this time, the code runs but does not give me the list of searchable fields and unable to pass any selection.

server.R

shinyServer(
function(input, output) {
output$Choose_Feild <- renderUI({
                      a = data.frame(entrez_db_searchable(db = "pubmed",
                      config = NULL))
                      unlist(a$FullName, use.names = FALSE)
                      }))

ui.R

shinyUI(
 fluidPage(
  titlePanel(title = "My data "),
 sidebarLayout(
  sidebarPanel(
    h3("Enter values"),
     selectInput( inputId = "var_y",label = "Field" , choices =
                uiOutput("Choose_Feild") )))

1 个答案:

答案 0 :(得分:2)

您无法以这种方式使用renderUI

由于选择不依赖于任何输入,您可以直接传递选择:

library(shiny)

shinyUI(bootstrapPage(
    selectInput(
        inputId = "var_y", label = "Field" ,
        choices = sapply(
            rentrez::entrez_db_searchable(db = input$db, config = NULL), 
            function(x) x$FullName, USE.NAMES=FALSE
        )
    )
))

如果您想动态生成选项,例如根据其他输入,您可以使用updateSelectInput

  • server.R

    shinyServer(function(input, output, session) {
        observe({
            choices <- sapply(
                rentrez::entrez_db_searchable(db = input$db, config = NULL), 
                function(x) x$FullName, USE.NAMES=FALSE
            )
            updateSelectInput(session, "var_y", choices=choices)
        })
    })
    
  • ui.R

    shinyUI(bootstrapPage(
        selectInput(
            inputId = "db", label="DB",
            choices=c("pmc"="pmc", "pubmed"="pubmed")
        ),
        selectInput(
            inputId = "var_y", label = "Field" ,
            choices = c()
        )
    ))