jeudi 1 août 2019

Check in R if reactive dataframe exists in if statement

I am making a dashboard using R Shiny that has an overview page with a number of charts. All charts are created using the same module. Users can select one subject they want all charts to show, but not all subjects exist for all charts.

Therefore, which certain selections not all charts will show data. In those cases I would like to show a text that tells the user that the subject is not available for that specific chart.

The input for my chart is a dataframe that is only created if the subject exists:

  data_indicator <- reactive({
   req(input$choice %in% data$subject)
    data_indicator <- data %>%
      filter(subject ==  input$choice)
  })

I have made two outputs: one that creates a chart ("plot"), and one that shows a text ("text").

Now I want to check if the dataframe has been created to choose which one to use. I've tried doing this using the following statement:

  output$plot_text <- renderUI({
    if (exists("data_indicator()")){
      output <- highchartOutput(ns("plot"))
     } else {
      output <- div(htmlOutput(ns("text")))
    }
    tagList(output)
  })

But it doesn't work, all charts now show the text, even the ones that should be available.

Does the exists command not work with a reactive dataset? Or is something else wrong?

Aucun commentaire:

Enregistrer un commentaire