test if reactive value does not exist or is empty

I am trying to build an app that joins together different data sources conditional on user input. But I need to test the existence of reactives to do that, and can't figure it out.

I have one main dataset1 that I want to always pass back to the user (for a download, but here, a renderPrint) and then another dataset2 that I join to it, if it exists.

Here's a shiny reprex, key line at isTruthy:

library(shiny)
library(tibble)
library(dplyr)
library(magrittr)

app_ui <- function() {
  tagList(fluidPage(
    actionButton("get1", "Get dataset1"),
    actionButton("get2", "Get dataset2"),
    verbatimTextOutput("view")
  ))
}

app_server <- function(input, output) {
  dataset1 <-
    eventReactive(input$get1, {
      mtcars %>% head() %>% rowid_to_column()
    })
  
  dataset2 <-
    eventReactive(input$get2, {
      chickwts %>% head() %>% rowid_to_column()
    })
  
  dataset_joint <- reactive({
    if (isTruthy(dataset2())) {
      dataset1() %>%
        left_join(dataset2())
    } else {
      dataset1()
    }
  })
  
  output$view <-
    renderPrint({
      dataset_joint()
    })
}

shinyApp(app_ui, app_server)

I get the behavior I want if I replace isTruthy(dataset2()) with isTruthy(input$get2). But in my actual use-case, I am passing datasets between modules and testing the existence of a dataset is easier than passing around both the input and dataset and then testing the input.

This topic was automatically closed 54 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.