Hide/show 使用 shinyjs 在 Shiny 中提交按钮的结果
Hide/show results with submit button in Shiny with shinyjs
我正在尝试创建一个 Shiny 应用程序,其中包含用于输入的提交按钮和用于 hiding/showing 结果的复选框。我的问题是勾选或取消勾选 hide/show 复选框没有任何效果,除非我再次点击提交按钮。
如何在用户选中复选框后立即显示结果并在取消选中时隐藏它而不依赖于提交按钮?它类似于 问题,但我使用的是 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)})
})
非常感谢任何帮助!
您的 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()})
})
)
我正在尝试创建一个 Shiny 应用程序,其中包含用于输入的提交按钮和用于 hiding/showing 结果的复选框。我的问题是勾选或取消勾选 hide/show 复选框没有任何效果,除非我再次点击提交按钮。
如何在用户选中复选框后立即显示结果并在取消选中时隐藏它而不依赖于提交按钮?它类似于
下面是一些示例代码来说明这个问题:
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)})
})
非常感谢任何帮助!
您的 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()})
})
)