闪亮的动作按钮不会触发observeEvent

时间:2017-05-27 06:23:43

标签: r shiny shinyjs action-button

我试图创建一个简单的闪亮应用程序,按顺序提出两个问题。我为每个问题创建了一个div,第二个隐藏了。我正在尝试使用observeEvent和一个动作按钮来触发第一个div的隐藏,并在点击时触发第二个div的显示。但是,单击操作按钮不会触发observeEvent。关于我错过了什么的想法?

我的ui.R是:

library(shiny)
library(shinyjs)


shinyUI(fluidPage(useShinyjs(),

  # Application title
  titlePanel("My Survey App"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(

    ),

    # Show a plot of the generated distribution
    mainPanel(
      h2("survey"),
      wellPanel(
          h6('Placeholder for Placeholder for question'),
          br(),
          div(id = 'questionseq1div',
              h5('Text Question Here',align = 'center'),
              br(),br(),
              textInput('sequence-question-1', '', value = "", width = '100%', 
                        placeholder = 'Give a detailed explanation of your position on the question.'),
              br(),br(),
              fluidRow(
                column(6, align="center", offset = 3,
                       actionButton(inputId = 'completedquestion1',label = 'Next '),
                       tags$style(type='text/css', "#button { vertical-align: middle; height: 50px; width: 100%; font-size: 30px;}")
                )
              )
          ),
          shinyjs::hidden(div(id = 'questionseq2div',
                              h5('Slider Input Question Here',align = 'center'),
                              br(),br(),
                              sliderInput('sequence-question-2','Min: 1  Max: 5',min = 1,max = 5,step = 1,ticks = T,value = 3),
                              br(),br(),
                              fluidRow(
                                column(6, align="center", offset = 3,
                                       submitButton('completed-question-2','Submit '),
                                       tags$style(type='text/css', "#button { vertical-align: middle; height: 50px; width: 100%; font-size: 30px;}")
                                )
                              )
          )))
    )
  )
))

我的服务器.R是:

library(shiny)
library(shinyjs)

shinyServer(function(input, output,session) {

  observeEvent(input$completedquestion1,{
    print('Triggered')
    shinyjs::show('questionseq2div')
    shinyjs::hide('questionseq1div')

  })

})

2 个答案:

答案 0 :(得分:1)

我不得不重新构建一些东西以使其发挥作用 - 我没有看到你如何能够在没有问题列表的情况下来回切换来迭代。一旦那样,我就添加了一些显而易见的东西,因为它很有趣......

这里的任何地方都是做你想做的事情的基本结构。

library(shiny)
library(shinyjs)


qlist <- list(
"q1" = list(type="qtext","text"="first text question"), 
"q2" = list(type="qslid","text"="first slider question"), 
"q3" = list(type="qslid","text"="second slider question"), 
"q4" = list(type="qtext","text"="second text question")
)
u <- shinyUI(fluidPage(useShinyjs(),

  # Application title
  titlePanel("My Survey App"),

  sidebarLayout(
    sidebarPanel(  ),

    mainPanel(
       h2("survey"),
       wellPanel( h6(textOutput("curstatus")), 
                  br(),
                  h4(textOutput("curquestion")), 
       div(id = 'questionseq1div',
           h5('div1 - Text Div',align = 'center'), 
           br(),br(),
           textInput('sequence-question-1', '', value = "", width = '100%', 
                     placeholder = 'Give a detailed explanation of your position on the question.'), 
            br(),br()
        ),
        div(id = 'questionseq2div',
               h5('div2 - Slider Div',align = 'center'),
               br(),br(),
               sliderInput('sequence-question-2','Min: 1  Max: 5',min = 1,max = 5,step = 1,ticks = T,value = 3),
               br(),br()
         )
        ),
        div(id='nextbuttondiv',fluidRow(
          column(6, align="center", offset = 3,
                 actionButton(inputId = 'completedquestion',label = 'Next '),
                 tags$style(type='text/css', "#button { vertical-align: middle; height: 50px; width: 100%; font-size: 30px;}")
            )
           )
        )

))))
s <- shinyServer(function(input, output,session) {

       rv <- reactiveValues(qlist=qlist,curqnum=1,inited=FALSE,finished=FALSE)

       setupForCurQuestion <- function(){
         qcur <- rv$qlist[[rv$curqnum]]

         if (qcur$type=="qtext"){
           shinyjs::show('questionseq1div')
           shinyjs::hide('questionseq2div')
         } else {
           shinyjs::hide('questionseq1div')
           shinyjs::show('questionseq2div')
         }
       }

       advanceQuestionAndCheckFinish <- function(){
         if (rv$curqnum==length(rv$qlist)) return(TRUE)
         rv$curqnum <- rv$curqnum+1
         print(rv$curqnum)
         setupForCurQuestion()
         return(FALSE)
       }

       isolate(setupForCurQuestion()) 

       observeEvent(input$completedquestion,{
         if (!rv$finished){
           rv$finished <- advanceQuestionAndCheckFinish()
           if (rv$finished){
             shinyjs::hide('nextbuttondiv')
           }
         }
      })
      output$curquestion <- renderText({
        qcur <- rv$qlist[[rv$curqnum]]
        qcur$text
      })
      output$curstatus <- renderText({
        if (rv$finished) return("Finished")
        sprintf("Question %d of %d",rv$curqnum,length(rv$qlist))
      })
})
shinyApp(u,s)

这是一个截屏: enter image description here

答案 1 :(得分:1)

所以,阅读@BigDataScientist的评论和Mike的解决方案,

  

你有一个隐藏的submitButton(),因为带有submitButtons()的应用只会在点击该按钮后更新你隐藏按钮你处于棘手的情况;)如果你用submitButton()注释掉这个部分,你将会看它运作良好,... - BigDataScientist

一个submitButton和一个actionButton之间的区别在于我,以及这个问题的简单解决方案 - 用submitButton()交换actionButton()