闪亮-更新输入文本以选择输入 [英] Shiny - Update input text for select input

查看:74
本文介绍了闪亮-更新输入文本以选择输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为selectInput函数设置一个文本值,并为每个选择项保存该值.我的尝试不想工作,我不明白原因.

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.

有人有主意吗?

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

}
))

推荐答案

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


  }
))

这篇关于闪亮-更新输入文本以选择输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆