In sync sliderInput and textInput

前端 未结 2 703
南笙
南笙 2020-12-30 11:36

Consider the following shiny app:

library(\'shiny\')

# User Interface/UI

ui <- fluidPage(

  titlePanel(
    \'Slider and Text input update\'
          


        
相关标签:
2条回答
  • 2020-12-30 12:00

    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)
    

    Hope it helps!

    0 讨论(0)
  • 2020-12-30 12:00

    The above code can be modified a bit to fix the issue of application getting closed when the input in the test box is empty

       library('shiny')
       ui <- fluidPage(titlePanel('Slider and Text input update'),
    
                        mainPanel(
                          sliderInput(
                            inputId = 'sliderValue',
                            label = 'Slider value',
                            min = 0,
                            max = 1000,
                            value = 500
                          ),
    
    
                          textInput(
                            inputId = 'textValue',
                            value = 500,
                            label = NULL
                          )
    
                        ))
    
    
        # Server logic
    
        server <- function(input, output, session)
        {
          observeEvent(input$textValue, {
            print(input$textValue)
            if ((as.numeric(input$textValue) != input$sliderValue) &
                input$textValue != "" &  input$sliderValue != "")
            {
              updateSliderInput(
                session = session,
                inputId = 'sliderValue',
                value = input$textValue
              )
            } else {
              if (input$textValue == "") {
                updateSliderInput(session = session,
                                  inputId = 'sliderValue',
                                  value = 0)
    
              }
            }
    
    
          })
    
          observeEvent(input$sliderValue, {
            if ((as.numeric(input$textValue) != input$sliderValue) &
                input$sliderValue != "" & input$textValue != "")
            {
              updateTextInput(
                session = session,
                inputId = 'textValue',
                value = input$sliderValue
              )
    
            }
    
          })
    
    
        }
    
        # Run the application
        shinyApp(ui = ui, server = server)
    
    0 讨论(0)
提交回复
热议问题