January
January

Reputation: 17090

How to implement a reset button in R shiny?

In my shiny output, I have a DataTable. I have a button ("Compute") and a list. I want the following behavior:

Somehow I can't wrap my mind around shiny to achieve this. Here is a rudimentary code to illustrate my problem:

server.R

server.fun <- function(input, output) {

  output$results <- renderDataTable({
    mytab <- data.frame( ID=1:10, blah=letters[1:10] ) 
    input$compute
    print("computing")
    mytab
  })

  output$results <- renderDataTable({
    input$reset
    print("resetting")
    NULL
  })
}

ui.R

shinyUI({
  basicPage(
        actionButton("compute", "Compute"),
        selectInput( "reset", "",
          list("------"="",
            "Do nothing"="nothing",
            "Reset"="reset")
        ),

        dataTableOutput( "results" )
    )
})

Clearly, it does not do what I want it to do. I don't think I am allowed to hook up two reactive functions to the same object, output$results (am I?). When I start the shiny app, both functions are called. When I then click on "compute", nothing happens. I am not sure why.

Here is another take at the problem, which partly a solution -- it works, but has unnecessary output. I tried to use reactiveValue. I define two additional UI elements, "foo" and "bar".

server.R

server.fun <- function(input, output) {

  rv <- reactiveValues()
  rv$mytab <- NULL

  output$foo <- renderText({
    input$compute
    rv$mytab <- data.frame( ID=1:10, blah=letters[1:10] )
    "computing"
  })    

  output$bar<- renderText({
    input$reset
    rv$mytab <- NULL
    "resetting"
  })

  output$results <- renderDataTable({
    rv$mytab
  })

}

ui.R

shinyUI({
  basicPage(
        actionButton("compute", "Compute"),
        selectInput( "reset", "",
          list("------"="",
            "Do nothing"="nothing",
            "Reset"="reset")
        ),

        dataTableOutput( "results" ),
        textOutput("foo"),
        textOutput("bar")

    )
})

Any better solution?

Upvotes: 2

Views: 3708

Answers (2)

hedgedandlevered
hedgedandlevered

Reputation: 2384

You may have stumbled upon some reserved variables? not sure, I saw a bunch of buggy behavior, but this works, just change the UI variable names too

shinyServer(function(input, output,session) {

  output$dummyoutput <- renderText({NULL})
  output$results1 <- renderDataTable({NULL})
  observe({
    input$compute1
    output$results1 <- renderDataTable({
      mytab <- data.frame( ID=1:10, blah=letters[1:10] ) 
      print("computing")
      return(mytab)
    })
  })

  observe({
    input$reset1
    print("resetting")
    output$results1 <- renderDataTable({NULL})
  })
})

Upvotes: 2

Quinn Weber
Quinn Weber

Reputation: 927

You could use a radioButton with two options, "compute" and "reset". Depending on the value of that input, the data.table is either shown or not shown.

output$results <- renderDataTable({
    if ( input$radio == "compute" ) {
        print('computing')
        return(data.frame( ID=1:10, blah=letters[1:10] ) )
    } else {
        print("resetting")
        return(NULL)
    }
})

Upvotes: 0

Related Questions