Counting button clicks

994 views
Skip to first unread message

Dennis Andersen

unread,
Jan 14, 2015, 6:46:56 PM1/14/15
to shiny-...@googlegroups.com
I am new to Shiny and playing around with the interface. I thought I would try and make a simple click counter app, where a textOutput displays the number of clicks on an actionButton.

I have the following in the ui.R file
textOutput("clickCount")
actionButton
("clickBtn", "Click")


In the server.R file I have
library(shiny)
clickCounter
<- 0
shinyServer
(function(input, output) {
 
  output$clickCount
<- renderText({
    input$clickBtn
    clickCounter
<- isolate(clickCounter + 1)
    clickCounter
   
})
 
})


First of all, when the app loads the textOutput displays 1 and not 0. Secondly, nothing happens to the clickCounter when I click the button. I wanted to increase the number in textOutput("clickCount") by 1 every time the button is clicked.. is it possible or am I chasing windmills? :)

Thanks,
Dennis

Oliver Gjoneski

unread,
Jan 14, 2015, 7:01:13 PM1/14/15
to shiny-...@googlegroups.com
Hi Denis!

Not chasing windmills at all!  You just need to use the fact that the actionButton variable is already a counter that's initialized at zero.   The following should do what you want:

shinyApp(
  ui = fluidPage(responsive = FALSE
    ,fluidRow(style = "padding-bottom: 20px;"
      ,column(width=6,
        textOutput("clickCount")
      )
      ,column(width=6,
        actionButton("clickBtn", "Click")
      )
    )
  )
  ,server = function(input, output, session) {
    output$clickCount <- renderText({
        input$clickBtn
    })
  }
  ,options = list(height = 500)
)
Reply all
Reply to author
Forward
0 new messages