Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

actionButton in RShiny : alternative to reset value

Tags:

r

shiny

I've read on topics that it's not possible to reset value of an actionButton with Shiny Package, but I couldn't find any trick to solve my problem.

I'd like to delete the text and the button in the main panel with this code :

library(shiny)

shinyUI(fluidPage(

    titlePanel("Trying to reset text !"),

    sidebarLayout(
        sidebarPanel(
            actionButton("button1","Print text")
        ),

        mainPanel(
          textOutput("textToPrint"),
          br(),
          uiOutput("uiButton2")
        )
    )
))

shinyServer(function(input, output) {

    output$textToPrint <- renderText({ 
        if(input$button1==0) (return("")) 
        else (return("Button clicked"))
    })

    output$uiButton2 <- renderUI({
        if(input$button1==0) (return ())
        else (return(actionButton("button2","Reset text and this button")))
    })

})

What is the alternative to impossible input$button1 = 0 ?

Thanks in advance for your help,

Matt

like image 638
mbh86 Avatar asked Jun 12 '14 12:06

mbh86


1 Answers

Thanks to Joe Cheng, here is a nice way of doing it :

shinyServer(function(input, output) {
    values <- reactiveValues(shouldShow = FALSE)

    observe({
        if (input$button1 == 0) return()
        values$shouldShow = TRUE
    })

    observe({
      if (is.null(input$button2) || input$button2 == 0)
          return()
      values$shouldShow = FALSE
    })

    output$textToPrint <- renderText({ 
        if (values$shouldShow)
          "Button clicked"
    })
    output$uiButton2 <- renderUI({
        if (values$shouldShow)
            actionButton("button2","Reset text and this button")

    })
})
like image 185
mbh86 Avatar answered Nov 04 '22 09:11

mbh86