Reactive expression not triggered by certain inputs


#1

Below is a reproducible stripped down app that illustrates the problem I am having. Basically, the user has 3 choices to add some numbers. What I want to have happen is for the value of ‘flagRecords’ to get updated when the user provides data to one or more inputs. When ‘flagRecords’ has a value a UI should render to allow the user to press an action button.

However, ‘flagRecords’ only gets triggered when both ‘A()’ and ‘B()’ return values. I need the value of ‘flagRecords’ to get recalculated with any change in inputs and for any combination. Entering 1 for input A should suffice, as should selecting a csv file.

library(shiny)
library(shinyjs)
library(stringr)

if (interactive()) {
  
  ui <- fluidPage(
      br(),
      br(),
      useShinyjs(),
          mainPanel(
            fluidRow(column(12,
               textInput("A", "Input A - numbers separated by commas:", NULL),
               textOutput("A"),
               br(),
               br(),
               numericInput("B1", "Input B1 - Beginning of a range of numbers", NULL),
               numericInput("B2", "Input B2 - End of a range of numbers:", NULL),
               textOutput("B"),
               br(),
               br(),
               fileInput("C", "Choose CSV File with a single column of record numbers",
                         accept = c(
                           "text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")
               ),
               checkboxInput("header", "Header", FALSE)
              ),
               textOutput("C"),
                tags$hr(),
                br(),
                br(),
               uiOutput("processflags.UI"),
               textOutput("D")
            )
      )
  ) # End UI
  ######################################################################################################################
  server <- function(input, output, session) {
    

  A <- reactive({
    req(input$A)
    x <- str_split(input$A,",") %>% 
      lapply(function(x) as.numeric(x))
    A <- as.vector(as.integer(unlist(x))) 
    A
  })
  
  B <- reactive({
    req(input$B1, input$B2)
    B <- as.vector(as.integer(seq.int(input$B1, input$B2, 1)))
    B
  })
  
    C <- reactive({
    inFile <- input$C
    if (is.null(inFile)){
      NULL
    } else {
      C <- as.vector(read.csv(inFile$datapath, header = input$header)) %>%
        unlist() %>% 
        as.integer() 
      C
    }
  })

  flagRecords <- reactive({
    combined <- list(A(),B(),C())
    flagRecords <- unique(Reduce(c,combined))
  })
    
  # Preview each output
  output$A <- renderText({req(A())
    paste0(length(A()), " records have been marked for flagging: ", list(A()))
  })
  
  output$B <- renderText({req(B())
    paste0(length(B()), " records have been marked for flagging: A continuous range - ", list(B()))})
  
  output$C <- renderText({
    req(C())
    paste0(length(C()), " records have been marked for flagging: ", list(C()))
  })
  output$D <- renderText({
    req(flagRecords())
    paste0("The following records have been marked for flagging: ", list(flagRecords()))
  })

  output$processflags.UI <- renderUI({
    req(length(flagRecords()) > 0)
    actionButton(inputId = "processflags", 
                 label = "Prepare flags for import", 
                 width = '500px')
  })
  
  # Stop app when browser session window closes
  session$onSessionEnded(function() {
    stopApp()
  })
  }
}
shinyApp(ui, server)