0

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

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

服务器.R

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

非常感谢任何帮助!

4

1 回答 1

2

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()})
    })
)
于 2016-12-23T12:58:59.900 回答