闪亮 - 更新选择输入的输入文本

时间:2016-04-21 11:52:47

标签: select shiny textinput

我想为selectInput函数设置文本值,并为每个选择选项保存值。我的尝试不想工作,我无法理解原因。

有没有人有想法?

library(shiny)

runApp(list(
  ui = bootstrapPage(
    sidebarPanel(
      selectInput('SELoption', label = "Select option", 
                  choices = c(
                    "Option 1" = 'f1',
                    "Option 2" = 'f2',
                    "Option 3" = 'f3'),
                  selected = 'f1')
  ),

  mainPanel(
    textInput("text", label = strong("Text"),value = 0)
  )

),

server = function(input, output, session) {
  userEnv <- new.env()
  userEnv$text <- NULL

  optionID <- reactive({
    if(is.null(input$SELoption)){return()}
    return(input$SELoption)
  })

  observe({
    fID <- optionID()

    if(!is.null(userEnv$text[[fID]]))
      updateTextInput(session, "text", value = userEnv$text[[fID]])
  })

}
))

1 个答案:

答案 0 :(得分:0)

library(shiny)
library(shinyjs)

runApp(list(
  ui = tagList(useShinyjs(),bootstrapPage(
    sidebarPanel(
      selectInput('SELoption', label = "Select option", 
                  choices = c(
                    "Option 1" = 'f1',
                    "Option 2" = 'f2',
                    "Option 3" = 'f3'),
                  selected = 'f1')
    ),

    mainPanel(
      disabled(textInput("text", label = strong("Text"),value = "f1"))
    )

  )),

  server = function(input, output, session) {


    optionID <- reactive({
      if(is.null(input$SELoption)){return(NULL)}
      return(input$SELoption)
    })

    observe({
      fID <- optionID()

      if(!is.null(fID))
        updateTextInput(session, "text", value = fID)
    })


  }
))