2
votes

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
Could you post a reproducible example, perhaps explicitly define a data frame like one that gets pulled down?Patrick McCarthy

1 Answers

2
votes

You simply cannot use renderUI in such a way.

Since choices don't depend on any input you can pass choices directly:

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

If you want to generate choices dynamically, for example based on another input you can use 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()
        )
    ))