Select the input to fill the text box in Shiny

My goal is to fill the value from the selected input for text input. The text input must be changed by the user later. Unfortunately my application is not working (selection is not populated) but there are no errors.

ui.R

library(shiny)

shinyUI(fluidPage(

    sidebarLayout(
        sidebarPanel(
            selectInput("id", 
                        label = "Choose a number",
                        choices = list()
            ),

            textInput("txt1", "number", 0)

        ),



        mainPanel(

        )
    )
))

      

server.R

df <- data.frame(a=c(1,2),b=c(3,4))

shinyServer(function(input, output, session) {
    # fill the select input
    updateSelectInput(session, "id", choices = df$a)

    observe({
        # When I comment this line, the select is correctly filled
        updateTextInput(session, "txt1", value = df[df$a==input$id,'a'])
    })

})

      

Any ideas on what might be wrong?

+3


source to share


1 answer


Your code doesn't work for me for your example dataset, but it works for:

df <- data.frame(a=c("a","b"),b=c(3,4))

      

I assume updateSelectInput () requires a character. However, this doesn't work:

updateSelectInput(session, "id", choices = as.character(df$a))

      



But if you define df as:

df <- data.frame(a=c("1","2"),b=c(3,4))

      

It works. Full code for example:

library(shiny)

df <- data.frame(a=c("1","2"),b=c(3,4))

shinyApp(
  ui = shinyUI(fluidPage(
    sidebarLayout(
      sidebarPanel(
        selectInput("id", 
          label = "Choose a number",
          choices = list()
        ),
        textInput("txt1", "number", 0)
      ),
      mainPanel()
    )
  )),
  server = shinyServer(function(input, output, session) {
    updateSelectInput(session, "id", choices = df$a)

    observe({
      updateTextInput(session, "txt1", value = df[df$a==input$id,'a'])
    })
  })
)

      

+2


source







All Articles