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.