1

I have the following in server.R

shinyServer(function(input, output) {

# builds a reactive expression that only invalidates 
# when the value of input$goButton becomes out of date 
# (i.e., when the button is pressed)
ntext <- eventReactive(input$goButton, {
input$n
})

output$nText <- renderText({
ntext()
})
})

and the following in ui.R

shinyUI(pageWithSidebar(
headerPanel("actionButton test"),
sidebarPanel(
numericInput("n", "N:", min = 0, max = 100, value = 50),
br(),
actionButton("goButton", "Go!"),
p("Click the button to update the value displayed in the main panel.")
),
mainPanel(
verbatimTextOutput("nText")
)
))

My goal is to make the go action button disappear once it is clicked five times and give a pop up window warning if clicked less than five times.

DeanAttali
  • 25,268
  • 10
  • 92
  • 118
Sam Kingston
  • 817
  • 1
  • 10
  • 19
  • take a look at [shinyjs](https://github.com/daattali/shinyjs), it can do this (change the text on a button or make it disappear) – DeanAttali Nov 08 '15 at 04:24

2 Answers2

1

As @daattali is saying, shinyjs make this really easy, you can do it like this:

library(shiny)
library(shinyjs)

ui <- shinyUI(
  fluidPage(
    useShinyjs(),
    sidebarPanel(
      actionButton('btn','Click me')
    ),
    mainPanel(
      verbatimTextOutput("nText")
    )
  )
)

server <- shinyServer(function(input,output,session){
  n <- 0
  makeReactiveBinding('n')

  observeEvent(input$btn, { 
    if(n < 5){
      info('Msg')
    } else if(n > 5){
      hide('btn')
    }
    n <<- n + 1 
  })

  output$nText <- renderText({
    n
  })

})

shinyApp(ui=ui,server=server)

Here's how you would hide the button without using shinyjs:

library(shiny)

ui <- shinyUI(
  fluidPage(
    tags$head(
      tags$style(
        HTML('#num{display: none;}')
      )
    ),
    useShinyjs(),
    sidebarPanel(
      conditionalPanel(
        condition = "input.num < 5",
        actionButton('btn','Click me')
      ),
      numericInput('num','',0)
    ),
    mainPanel(
      verbatimTextOutput("nText")
    )
  )
)

server <- shinyServer(function(input,output,session){
  n <- 0
  makeReactiveBinding('n')

  observeEvent(input$btn, { 
    n <<- n + 1
    updateNumericInput(session,'num',value=n)
  })

  output$nText <- renderText({
    n
  })

})

shinyApp(ui=ui,server=server)

And finally without using observeEvent:

library(shiny)

ui <- shinyUI(
  fluidPage(
    tags$head(
      tags$style(
        HTML('#num{display: none;}')
      )
    ),
    useShinyjs(),
    sidebarPanel(
      conditionalPanel(
        condition = "input.num < 5",
        actionButton('btn','Click me')
      ),
      numericInput('num','',0)
    ),
    mainPanel(
      verbatimTextOutput("nText")
    )
  )
)

server <- shinyServer(function(input,output,session){
  n <- 0
  makeReactiveBinding('n')

  observe({
    input$btn
    isolate({
      n <<- n + 1
      updateNumericInput(session,'num',value=n)
    })
  })

  output$nText <- renderText({
    n
  })

})

shinyApp(ui=ui,server=server)
RmIu
  • 4,357
  • 1
  • 21
  • 24
  • Is there any way to hide it without shinyjs? I have been trying to hide the div which the button is placed in once it is clicked, but that forces nothing to show up. Why is that? – Sam Kingston Nov 08 '15 at 13:26
  • Yes, what shinyjs basically does is call javascript so you can always write that yourself otherwise you can use CSS and a `conditionalPanel` but I really do think that shinyjs is the best alternative. – RmIu Nov 08 '15 at 13:59
  • Thanks for the response. Could not find function observeEvent is what I am getting. – Sam Kingston Nov 08 '15 at 14:09
  • You probably have a old version of shiny, try updating, otherwise use `observe` instead – RmIu Nov 08 '15 at 14:42
  • Is there any way you could provide the solution with observe? I am really struggling in making it work as I keep getting errors. I could start a new question if you want so I could give you another point. Thank you. – Sam Kingston Nov 08 '15 at 16:21
  • That's fine I don't mind not getting another answer. I posted how you would do it with just observe and this should work for you. I do recommend that you update shiny, `observeEvent` is a really handy function. – RmIu Nov 08 '15 at 17:51
  • Hi Oskar. Thank you for all your help. I tried to give you an extra point. I couldn't figure out how. I do have one last question that I posted(I hope this is last). I am not sure if someone knows the answer, but I am sure you will. Is there any way you can provide some guidance? – Sam Kingston Nov 09 '15 at 02:15
1

You don't need to define a reactive n. It is already the value of input$btn.

library(shiny)
library(shinyjs)

ui <- shinyUI(
  fluidPage(
    useShinyjs(),
    sidebarPanel(
      actionButton('btn','Click me')
    ),
    mainPanel(
      verbatimTextOutput("nText")
    )
  )
)

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

  observe({ 
    if(input$btn < 5){
      info('Msg')
    } else {
      hide('btn')
    }
  })

  output$nText <- renderText({
    input$btn
  })

})

shinyApp(ui=ui,server=server)
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225