【问题标题】:Hide/show results with submit button in Shiny with shinyjs使用 Shiny 的 Shiny 中的提交按钮隐藏/显示结果
【发布时间】:2017-05-09 01:49:57
【问题描述】:

我正在尝试创建一个闪亮的应用程序,其中包括一个用于输入的提交按钮和一个用于隐藏/显示结果的复选框。我的问题是勾选或取消勾选隐藏/显示复选框无效,除非我再次点击提交按钮。

如何在用户选中复选框后立即显示结果并在取消选中时将其隐藏而不依赖于提交按钮?它类似于this question,但我使用的是 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")
                        )
                    )
))

服务器.R

server <- shinyServer(function(input, output) {
        observe(toggle("full_phrase", condition=(input$checkbox==T)))
        output$full_phrase <- renderText({paste(input$phrase1, input$phrase2)})
})

非常感谢任何帮助!

【问题讨论】:

    标签: r shiny shinyjs


    【解决方案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()})
        })
    )
    

    【讨论】:

      猜你喜欢
      • 2018-01-04
      • 2021-11-01
      • 2016-05-10
      • 1970-01-01
      • 2018-05-12
      • 2021-06-08
      • 1970-01-01
      • 2021-10-06
      • 2017-11-15
      相关资源
      最近更新 更多