简体   繁体   English

使用ShinyJS在Shiny中使用提交按钮隐藏/显示结果

[英]Hide/show results with submit button in Shiny with shinyjs

I'm trying to create a Shiny app that includes a submit button for inputs and a checkbox for hiding/showing the results. 我正在尝试创建一个Shiny应用程序,其中包括用于输入的提交按钮和用于隐藏/显示结果的复选框。 My issue is that ticking or unticking the hide/show checkbox has no effect unless I hit the submit button again. 我的问题是,除非再次按下“提交”按钮,否则选中或取消选中“隐藏/显示”复选框均无效。

How can I show results as soon as the user checks the checkbox and hide it on uncheck without any dependency on submit button? 用户一旦选中复选框,如何立即显示结果并在不选中的情况下将其隐藏,而不依赖于提交按钮? It's similar to this question, but I'm using the shinyjs package instead. 这类似于问题,但我使用的是Shinyjs包。

Here is some sample code to illustrate the issue: 以下是一些示例代码来说明此问题:

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)})
})

Any help greatly appreciated! 任何帮助,不胜感激!

Your submitButton control his halting all reactivity until it is clicked. 您的submitButton控件将停止所有反应,直到单击为止。 If you want any elements of your UI to be reactive independent of your button, you should use actionButton instead, and use an event observer for the actions that you want performed when the button is clicked. 如果要让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()})
    })
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM