Shiny - Update input text for select input

自古美人都是妖i 提交于 2019-12-12 03:48:00

问题


I'd like to set a text value for a selectInput function and save the value for every choice of the selection. My try does not want to work and I can't understand the reason.

does anyone have an idea?

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:


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


  }
))


来源:https://stackoverflow.com/questions/36768785/shiny-update-input-text-for-select-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!