可以稍微修改上述代码,以解决当测试框中的输入为空时关闭应用程序的问题
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)