0
votes

I am trying to make an app that would take product ID from input field and then download data from server and use that data to create a selectInput. My UI part:

ui <- fluidPage(

  titlePanel("Alternatives"),

  sidebarLayout(

    sidebarPanel(

      textInput("text1",
                label = h3("Enter product ID:"),
                value = "ID"),

      actionButton('run', 'Run'),

      hr(),
      selectInput("selectinput",
                  label = "Alternatives:",
                  choices = choices_for_input
                  )
    ),

    mainPanel(

    )
  )

My server part:

server <- function(input, output, session) {

  observeEvent(input$run, {
    alldata <- data_request(as.numeric(input$text1)) #getting data from server
    })

     ### putting data into correspoding datatables
  fields <- as.data.table(do.call(rbind, alldata$fields))
  setnames(fields,
           c("V1", "V2", "V3", "V4", "V5"),
           c('prod_id', 'field', 'item1', 'alternative', 'score'))
  scores <- as.data.table(do.call(rbind, alldata$scores))
  setnames(scores,
           c("V1", "V2"),
           c("prod_id", "score"))

    ### list for selectInput choices
  choices_for_input <- scores$prod_id
}

but I get an error:

object 'choices_for_input' not found

I could use some default value like NO VALUES inside my choices to begin with and later update the choice list, but I want the selectInput only appear after all the data is prepared with all the right selections. But I am not sure how...

1

1 Answers

0
votes

You cannot pass variables from your server to your ui and visa/versa (well... there are ways). What you can do is update your selectInput choices after you have loaded your data.

In your ui you can set your choices to an empty vector:

selectInput("selectinput", label = "Alternatives:", choices = c())

and in your server you can updated your choices (after you loaded the data) with:

updateSelectInput(session, 'selectinput', choices = choices_for_input)

If you really want to hide the selectInput before you loaded your data, you can use the shinyjs library:

  1. Add the useShinyjs() function somewhere in your ui.
  2. Wrap selectInput('selectinput', ...) in the hidden() function of shinyjs.
  3. Add shinyjs::show('selectinput') in your server after you updated the choices.

Working example

library(shinyjs)

ui <- fluidPage(
  useShinyjs(), #add shinyjs functionalty
  titlePanel("Alternatives"),
  sidebarLayout(
    sidebarPanel(
      textInput("text1",
                label = h3("Enter product ID:"),
                value = "ID"),
      actionButton('run', 'Run'),
      hr(),
      hidden(selectInput("selectinput", label = "Alternatives:", choices = c()))# hide selectinput on load
    ),
    mainPanel(
    )
  )
)

server <- function(input, output, session) {
  observeEvent(input$run, {
    Sys.sleep(2) # sleep some time
    scores = data.frame(prod_id=1:10) # create random data
    updateSelectInput(session, 'selectinput', choices = scores$prod_id) # update choices
    shinyjs::show('selectinput') # show selectinput
  })
}

shinyApp(ui, server)