How can I set NULL to reactive?

Hi, I need to set NULL to reactive when a button is clicked. In my example below 'data' (as module parameter) is passed as reactive from other module. Now, I want to click button and set NULL to reactiveExpr . The way I'm doing it here is not working. I don't need set NULL to reactive base on any condition, just by clicking the button.

mod_ui <- function(id){ 
  ns <- NS(id)

  actionButton(ns("btn"), "Click me")

}

mod_server <- function(id, data){ 
  moduleServer( id, function(input, output, session) { 

  ns <- NS(id)

  reactiveExpr <- reactive(data())

  observeEvent(input$btn, {
    reactiveExpr(NULL) 
})

# some more code...

How can I do this?

Look into the use of reactiveValue

I know, I use it in some other cases but here I need reactive

Do you want the 0bject to contain data at first and then clear from button press and never again ? Or is there supposed to be some condition to refill?.

In general to do one or another thing based on conditions if-else logic can be used.

library(shiny)
mod_ui <- function(id) {
  ns <- NS(id)

  tagList(
    actionButton(ns("btn"), "Click me"),
    tableOutput(ns("modtable"))
  )}

mod_server <- function(id, data) {
  moduleServer(id, function(input, output, session) {
    ns <- NS(id)

    reactiveExpr <- reactive({
      switchvar <- as.numeric(input$btn) %% 2
      switch(switchvar,
        data(),
        NULL
      ) })

    output$modtable <- renderTable({
      reactiveExpr()
    })
  })
}

ui <- fluidPage(
  mod_ui("a1")
)

server <- function(input, output, session) {
  mod_server("a1",reactive(head(iris)))
}

shinyApp(ui, server)

Thank you for your answer:)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.