1
votes

I would like to remove an actionButton once it has been clicked. I had previously used shinyjs::hide("init") successfully, but now I would like to remove the button altogether. I have tried the following code

The issue stems from modularization. It updates a vector GlobClass$Init based on a unique ID userPID. The non-modularized of the code version works.

Example.R

library(shiny)

### Modularized Code
#' UserSide Function to Create Button
InitializeUI <- compiler::cmpfun( function(id) {
    ns <- NS(id)    
    actionButton( ns("init"), h4("Initialize") )
})         
#' ServerSide Function to Update reactive Vector and Delete Button
Initialize <- compiler::cmpfun( function(input, output, session,
    GlobClass, userPID ) {

    observeEvent( input$init, {
        cat("click\n")
        removeUI(selector='#init', immediate=TRUE)
        GlobClass$Init[userPID] <- TRUE
    }, autoDestroy=TRUE)
})

### User.R
ui <- fluidPage( InitializeUI("init") )

### Server.R
GlobClass <- reactiveValues(Init=c(FALSE, FALSE))

server <- function(input,output) {
    userPID <-  sample( 1:2,1)
    cat(userPID, "\n")
    callModule( Initialize, "init", GlobClass, userPID )        
}

### Run Application
shinyApp(ui, server)
2

2 Answers

3
votes

The following seems to work fine for me. Are there any differences in implementation?

library(shiny)

ui <- fluidPage(
  actionButton('init','Click')
)

# The server function
server <- function(input,output) 
{
  observeEvent(input$init, {
    removeUI(selector='#init', immediate=TRUE)
  }, autoDestroy=TRUE)

}

shinyApp(ui, server)
0
votes

You can do:

  actionButton(
    ns("init"), h4("Initialize"), 
    onclick = "var $btn=$(this); setTimeout(function(){$btn.remove();},0);"
  )

(and don't use removeUI).