Consider the following shiny app:
library(\'shiny\')
# User Interface/UI
ui <- fluidPage(
titlePanel(
\'Slider and Text input update\'
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!
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)