【问题标题】:can I use updateTextInput() within shiny modules?我可以在闪亮的模块中使用 updateTextInput() 吗?
【发布时间】:2016-06-05 15:08:09
【问题描述】:

我编写了一个相当简单的模块,由客户端上的 textInput 和更新该 textInput 值的服务器函数组成。为此,我在服务器函数中调用 updateTextInput()。但是,客户端上的 textInput 没有更新。

我应该怎么做才能让我的模块服务器函数更新客户端上的 textInput?

这是我的代码的简化版本:

带有模块定义的global.R

# Client side. 
specifyInput <- function(id, points){  
    ns <- NS(id)
    tagList(textInput(ns('points'), label='Total', value=points))
}

# Server side. 
specify <- function(input, output, session){
    ns <- session$ns

    observe({
        new.value = 2 * as.numeric(input$points)
        #this line does not seem to work
        updateTextInput(session, ns('points'), value = new.value)
    })

    # create dataframe with entered value
    df <- reactive(data.frame(points = as.numeric(input$points)))

    # return the dataframe
    return(df())
}

ui.R

    specifyInput("ttlEntry", 10)

服务器.R

        function(input, output, session){
          test <- reactive(callModule(specify, "ttlEntry"))

          #somewhere in the code, call test()
        }

实际上,当用户输入句点时,我想在输入的值后面附加一个 0.5,例如,如果用户输入“10”。然后将 textInput 更新为显示“10.5” 但是出于测试目的,我已将该代码更改为 new.value = 2 * ...

非常感谢任何帮助。

【问题讨论】:

    标签: r module shiny textinput


    【解决方案1】:

    有几点需要注意,都是连接到模块服务器组件的:

    1. 我认为您不需要 ns &lt;- session$ns - 我认为这是为在模块中使用 renderUI 保留的(请参阅 this article)。
    2. 您不需要将 inputId 包装在模块服务器中的 ns() 中 - 因此应将 ns('points') 更改为仅 'points'
    3. 您不应返回 df(),而应仅返回不带括号的 df

    一旦您进行了这些更改,应用程序就会运行,但请注意,系统会创建一个反馈循环,因此输入的值会不断加倍。您需要重新编写代码逻辑,以便将输入的内容加倍(或按照您的描述添加 .5)并将其保留。

    一个工作的单文件应用程序版本发布如下:

    # Client side. 
    specifyInput <- function(id, points){  
      ns <- NS(id)
      textInput(ns('points'), label = 'Total', value = points)
    }
    
    # Server side. 
    specify <- function(input, output, session, ...){
    
      observe({
        new.value = 2 * as.numeric(input$points)
        updateTextInput(session, 'points', value = new.value)
      })
    
      # create dataframe with entered value
      df <- reactive(data.frame(points = as.numeric(input$points)))
    
      # return the dataframe
      return(df)
    }
    
    # ui
    ui <- specifyInput("ttlEntry", 10)
    
    # server
    server <- function(input, output, session){
      test <- callModule(specify, "ttlEntry")
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2021-01-10
      • 2017-12-09
      • 1970-01-01
      • 2019-10-20
      • 2020-01-18
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      • 2019-01-18
      相关资源
      最近更新 更多