我想制作一个表单类型的Shiny应用程序,当用户单击和操作按钮时,会显示输出,但如果输入值发生变化,则会隐藏输出。
这里有一个例子:
library(shiny)
library(shinyjs)
ui <- fluidPage(
radioButtons("myRadioButton", label = h4("Radio Input"),
choices = list("A" = 0,
"B" = 1,
"C" = 2),
selected = character(0)),
numericInput("myNumericInput", label = h4("Numeric Input"),
value = NA, min = 0, max = 50, step = 1),
actionButton("submit", "Submit"),
textOutput("myOutput")
)
server <- function(input, output, session){
score <- reactive({
scoreOut <- paste(input$myRadioButton, input$myNumericInput)
})
observeEvent(input$myRadioButton, {
hide("myOutput")
})
observeEvent(input$myNumericInput, {
hide("myOutput")
})
observeEvent(input$submit, {
show("myOutput")
output$myOutput <- renderText({
paste("This is your value:", score())
})
})
}
shinyApp(ui, server)
因此在上面的例子中,输出显示在"0"之后;提交";单击。我想要的是,如果你回去改变,比如说无线电或数字输入,输出就会消失,直到";提交";再次单击。
您错过了UI中加载执行隐藏函数所需的JavaScript的useShinyjs()
:
library(shiny)
library(shinyjs)
ui <- fluidPage(
# load required Java Script
useShinyjs(),
radioButtons("myRadioButton",
label = h4("Radio Input"),
choices = list(
"A" = 0,
"B" = 1,
"C" = 2
),
selected = character(0)
),
numericInput("myNumericInput",
label = h4("Numeric Input"),
value = NA, min = 0, max = 50, step = 1
),
actionButton("submit", "Submit"),
textOutput("myOutput")
)
server <- function(input, output, session) {
score <- reactive({
scoreOut <- paste(input$myRadioButton, input$myNumericInput)
})
observeEvent(input$myRadioButton, {
hide("myOutput")
})
observeEvent(input$myNumericInput, {
hide("myOutput")
})
observeEvent(input$submit, {
show("myOutput")
output$myOutput <- renderText({
paste("This is your value:", score())
})
})
}
shinyApp(ui, server)