同步slideInput和textInput [英] In sync sliderInput and textInput

查看:78
本文介绍了同步slideInput和textInput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下闪亮的应用程序:

Consider the following shiny app:

library('shiny')

# User Interface/UI

ui <- fluidPage(

  titlePanel(
    'Slider and Text input update'
  ), # titlePanel

  mainPanel(

    # Slider input
    sliderInput(
      inputId = 'sliderValue',
      label = 'Slider value',
      min = 0,
      max = 1000,
      value = 500
    ), # sliderInput

    # Text input
    textInput(
      inputId = 'textValue',
      label = NULL
    ) # textInput

  ) # mainPanel

) # fluidPage


# Server logic

server <- function(input, output, session) {

  observe({
    # Update vertical depth text box with value of slider
    updateTextInput(
      session = session,
      inputId = 'textValue',
      value = input$sliderValue
    ) # updateTextInput

#    updateSliderInput(
#      session = session,
#      inputId = 'sliderValue',
#      value = input$textValue
#    ) # updateSliderInput

  }) # observe

}

# Run the application 
shinyApp(ui = ui, server = server)

它允许用户更改滑块的值( sliderInput ),它会更新文本框中的文本( textInput ):

It allows the user to change the values of a slider (sliderInput), which updates the text in the text box (textInput):

我希望它们可以同步工作。因此,我不仅要上面的滑块>文本框交互,还要相反:文本框>滑块。

I want these to work in sync. So, instead of just the above slider > text box interaction, I want the opposite as well: text box > slider.

如果取消注释 updateSliderInput 组件,这两个小部件相互竞争;一个更新导致另一个更新,另一个导致更新...

If you uncomment the updateSliderInput component, the two widgets compete against one another; an update of the one leads to an update of the other which leads to an update of the other, ...

如何在仍然使两者保持同步的同时避免这种情况?

How can this be avoided while still making the two be in sync?

推荐答案

一种方法是对每个输入使用 observeEvent 并添加条件 if(as.numeric(input $ textValue)! = input $ sliderValue)。这将帮助您从彼此调用的输入中递归更新功能。然后您的应用程序将如下所示:

One way to do it would be using observeEvent for each input and adding a condition if(as.numeric(input$textValue) != input$sliderValue). This will help you from the inputs calling each others update functions recursively. Then your app would look something like this:

library('shiny')

  # User Interface/UI

  ui <- fluidPage(

    titlePanel(
      'Slider and Text input update'
    ), # titlePanel

    mainPanel(

      # Slider input
      sliderInput(
        inputId = 'sliderValue',
        label = 'Slider value',
        min = 0,
        max = 1000,
        value = 500
      ), # sliderInput

      # Text input
      textInput(
        inputId = 'textValue',
        value = 500,
        label = NULL
      ) # textInput

    ) # mainPanel

  ) # fluidPage


  # Server logic

  server <- function(input, output, session)
  {
    observeEvent(input$textValue,{
      if(as.numeric(input$textValue) != input$sliderValue)
      {
        updateSliderInput(
          session = session,
          inputId = 'sliderValue',
          value = input$textValue
        ) # updateSliderInput
      }#if


    })

    observeEvent(input$sliderValue,{
      if(as.numeric(input$textValue) != input$sliderValue)
      {
        updateTextInput(
          session = session,
          inputId = 'textValue',
          value = input$sliderValue
        ) # updateTextInput

      }#if

    })


  }

  # Run the application 
  shinyApp(ui = ui, server = server)

希望有帮助!

这篇关于同步slideInput和textInput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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