我正在尝试创建一个Shiny应用,其中包含用于输入的提交按钮和用于隐藏/显示结果的复选框。我的问题是,除非我再次点击提交按钮,否则勾选或取消勾选隐藏/显示复选框无效。
如果用户选中复选框并在取消选中时隐藏它而不依赖于提交按钮,我该如何显示结果?它与this问题类似,但我使用的是shinyjs软件包。
以下是一些示例代码来说明问题:
UI.R
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.R
server <- shinyServer(function(input, output) {
observe(toggle("full_phrase", condition=(input$checkbox==T)))
output$full_phrase <- renderText({paste(input$phrase1, input$phrase2)})
})
任何帮助都非常感谢!
答案 0 :(得分:1)
你的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()})
})
)