我正在尝试创建一个Shiny应用程序,该应用程序包括一个用于输入的提交按钮和一个用于隐藏/显示结果的复选框。我的问题是,除非再次按下“提交”按钮,否则选中或取消选中“隐藏/显示”复选框均无效。
用户一旦选中复选框,如何在不选中“提交”按钮的情况下立即显示结果,而在未选中的情况下将其隐藏呢?类似于这个问题,但是我使用的是Shinyjs包。
以下是一些示例代码来说明此问题:
用户界面
ui <- shinyUI(fluidPage(
# Initiate shinyjs package
useShinyjs(),
# Select layout type
sidebarLayout(
# Sidebar content
sidebarPanel(
# Input phrase1
textInput("phrase1", "Enter a word or phrase here", "It’s not rocket"),
# Input phrase2
textInput("phrase2", "Enter a word or phrase here", "science"),
# Submit button
submitButton("Paste phrases")
),
# Main panel content
mainPanel(
# Checkbox to show/hide results
checkboxInput("checkbox", "Show results?", TRUE),
# Results
textOutput("full_phrase")
)
)
))
服务器
server <- shinyServer(function(input, output) {
observe(toggle("full_phrase", condition=(input$checkbox==T)))
output$full_phrase <- renderText({paste(input$phrase1, input$phrase2)})
})
任何帮助,不胜感激!
您可以submitButton
控制他停止所有反应,直到单击为止。如果您希望UI的任何元素独立于按钮而具有响应性,则应actionButton
改为使用事件,并为单击按钮时要执行的操作使用事件观察器。
library(shiny)
library(shinyjs)
shinyApp(
ui =
shinyUI(fluidPage(
# Initiate shinyjs package
useShinyjs(),
# Select layout type
sidebarLayout(
# Sidebar content
sidebarPanel(
# Input phrase1
textInput("phrase1", "Enter a word or phrase here", "It's not rocket"),
# Input phrase2
textInput("phrase2", "Enter a word or phrase here", "science"),
# Submit button
actionButton("paste_phrase",
label = "Paste phrases")
),
# Main panel content
mainPanel(
# Checkbox to show/hide results
checkboxInput("checkbox", "Show results?", TRUE),
# Results
textOutput("full_phrase")
)
)
)),
server =
shinyServer(function(input, output, session) {
observe({
toggle("full_phrase",
condition=input$checkbox)
})
pasted_phrases <-
eventReactive(
input$paste_phrase,
{
paste(input$phrase1, input$phrase2)
}
)
output$full_phrase <-
renderText({pasted_phrases()})
})
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句