0

I made a peculiar observation in my ShinyApp but I am not sure why it is happening or how to fix it. When I first launch the App, selecting inputs and clicking on the action button (Go) shows me output values in the table, which are wrong output values. When I click on the action button again, without changing the inputs, the output values change to the correct values. The problem seems to happen only when I first launch the App. Afterwards the App works fine and correct values are shown when I single-click. I am copying the server.R code below. Thank you!

server <- function(input, output,session) {


  one_or_duration <- eventReactive(input$go,ignoreNULL = FALSE,{
              if(input$one_or_duration == "A single measurement"){
              "single"}
              else if(input$one_or_duration == "Multiple measurements"){
              "multiple"}})

  nnrti <- eventReactive(input$go,ignoreNULL = FALSE,{
    if(input$nnrti == "Yes, include participants on NNRTIs"){
      "yes"}
    else if(input$nnrti == "No, exclude participants on NNRTIs"){
      "no"}})

  arm <- eventReactive(input$go,ignoreNULL = FALSE,{
    if(input$arm == "Yes, include participants in intervention arms"){
      "yes"}
    else if(input$arm == "No, exclude participants in intervention arms"){
      "no"}})

  freq <- eventReactive(input$go,ignoreNULL = FALSE,{
    if(input$freq == "Same frequency as observed by authors"){
      "same"}
    else if(input$freq == "Input expected frequency"){
      "diff"}})

  pt_class <- eventReactive(input$go,ignoreNULL = FALSE,{
    if(input$pt_class == "All study participants"){
      "all"}
    else if(input$pt_class == "Early treated participants"){
      "early"}
    else if(input$pt_class == "Chronic treated participants"){
      "chronic"}
  })


  ptcs_plus_ncs <- eventReactive(input$go,ignoreNULL = FALSE,{
    if (freq() == "same"){
    validate(need(input$vl >= 50, "Please select viral load threshold equal to or greater than 50 copies/mL"))
      system(sprintf("python3 new_duration_ctrl.py %s %s %s %s %s %s", input$vl , input$duration, pt_class(), one_or_duration(), nnrti(),arm()))
      data <- read.csv("output_all.csv")
      data}

    else if (freq() == "diff"){
      validate(need(input$vl >= 50, "Please select viral load threshold equal to or greater than 50 copies/mL"))
      system(sprintf("python3 new_another_freq.py %s", input$expected_freq))
      data <- read.csv("output_all.csv")
      data}
    })



    ptc <- eventReactive(input$go,ignoreNULL = FALSE,{
      system(sprintf("python3 new_duration_ctrl_ptc.py %s %s %s %s %s %s", input$vl , input$duration, pt_class(), one_or_duration(), nnrti(), arm()))
      data <- read.csv("output_ptc.csv")
      data 
    })

    nc <- eventReactive(input$go,ignoreNULL = FALSE,{
      system(sprintf("python3 new_duration_ctrl_nc.py %s %s %s %s %s %s", input$vl , input$duration, pt_class(), one_or_duration(), nnrti(), arm()))
      data <- read.csv("output_nc.csv")
      data 
      })

    ptcs_plus_ncs_subset <- eventReactive(input$go,ignoreNULL = FALSE,
      {
      if(input$weekly == FALSE & freq() == "same")
      {
      data <- read.csv("output_all.csv")
      data_ptc <- read.csv("output_ptc.csv")
      data_nc <- read.csv("output_nc.csv")
      mytimes <- c("1", "5", "9", "13", "25", "49")
      subset1 <- data[mytimes,1:4]
      subset2 <- data_ptc[mytimes,2:4]
      subset3 <- data_nc[mytimes,2:4]
      subset <- cbind(subset1,subset2,subset3)
      colnames(subset) <- c("Weeks after treatment interruption", "Percentage with viral suppression (Total)", "Count with viral suppression (Total)", "Count under follow-up (Total)", "Percentage with viral suppression (PTCs)", "Count with viral suppression (PTCs)", "Count under follow-up (PTCs)","Percentage with viral suppression (NCs)", "Count with viral suppression (NCs)", "Count under follow-up (NCs)")
      subset
    }
        else if(input$weekly == TRUE & freq() == "same"){
          data <- read.csv("output_all.csv")
          data_ptc <- read.csv("output_ptc.csv")
          data_nc <- read.csv("output_nc.csv")
          mytimes <- c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49")
          subset1 <- data[mytimes,1:4]
          subset2 <- data_ptc[mytimes,2:4]
          subset3 <- data_nc[mytimes,2:4]
          subset <- cbind(subset1,subset2,subset3)
          colnames(subset) <- c("Weeks after treatment interruption", "Percentage with viral suppression (Total)", "Count with viral suppression (Total)", "Count under follow-up (Total)", "Percentage with viral suppression (PTCs)", "Count with viral suppression (PTCs)", "Count under follow-up (PTCs)","Percentage with viral suppression (NCs)", "Count with viral suppression (NCs)", "Count under follow-up (NCs)")
          subset          
        }
        else if(input$weekly == FALSE & freq() == "diff"){
          data <- read.csv("output_all.csv")
          data_ptc <- read.csv("output_ptc.csv")
          data_nc <- read.csv("output_nc.csv")
          mytimes <- c("1", "5", "9", "13", "25", "49")
          subset1 <- data[mytimes,1:2]
          subset2 <- data_ptc[mytimes,2:4]
          subset3 <- data_nc[mytimes,2:4]
          subset <- cbind(subset1,subset2,subset3)
          colnames(subset) <- c("Weeks after treatment interruption", "Percentage with viral suppression (Total)", "Percentage with viral suppression (PTCs)", "Count with viral suppression (PTCs)", "Count under follow-up (PTCs)","Percentage with viral suppression (NCs)", "Count with viral suppression (NCs)", "Count under follow-up (NCs)")
          subset         

        }

        else if(input$weekly == TRUE & freq() == "diff"){
          data <- read.csv("output_all.csv")
          data_ptc <- read.csv("output_ptc.csv")
          data_nc <- read.csv("output_nc.csv")
          mytimes <- c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49")
          subset1 <- data[mytimes,1:2]
          subset2 <- data_ptc[mytimes,2:4]
          subset3 <- data_nc[mytimes,2:4]
          subset <- cbind(subset1,subset2,subset3)
          colnames(subset) <- c("Weeks after treatment interruption", "Percentage with viral suppression (Total)", "Percentage with viral suppression (PTCs)", "Count with viral suppression (PTCs)", "Count under follow-up (PTCs)","Percentage with viral suppression (NCs)", "Count with viral suppression (NCs)", "Count under follow-up (NCs)")
          subset          
        }
        })

  output$table <- 
    DT::renderDataTable({

    if (freq()=="same"){
    datatable(ptcs_plus_ncs_subset(),
              container=
                withTags(table(
                class = 'display',
                thead(
                    tr(
                      th(rowspan=2 ,'Weeks after treatment interruption'),
                      th(colspan = 3, 'Total'),
                      th(colspan = 3, 'PTCs'),
                      th(colspan = 3, 'NCs')
                    ),
                    tr(
                      lapply(c('Percentage with viral suppression (Total)', 'Count with viral suppression (Total)', 'Count under follow-up (Total)','Percentage with viral suppression (PTCs)', 'Count with viral suppression (PTCs)', 'Count under follow-up (PTCs)','Percentage with viral suppression (NCs)', 'Count with viral suppression (NCs)', 'Count under follow-up (NCs)'), th)
                    )                  
                    )
                )),
      rownames=FALSE,options = list(paging=FALSE, bInfo=FALSE, lengthChange=FALSE, searching = FALSE,
      columnDefs = list(list(className = 'dt-center', targets = "_all")))) %>% formatStyle(colnames(ptcs_plus_ncs_subset())[2],backgroundColor = "gray", color = "#fff", fontWeight = 'bold') %>% formatStyle(colnames(ptcs_plus_ncs_subset())[5],backgroundColor = "red", color = "#fff", fontWeight = 'bold') %>% formatStyle(colnames(ptcs_plus_ncs_subset())[8],backgroundColor = "blue", color = "#fff", fontWeight = 'bold')
    }
    else if (freq()=="diff")
    {
      datatable(ptcs_plus_ncs_subset(),
                container=
                  withTags(table(
                    class = 'display',
                    thead(
                      tr(
                        th(rowspan=2 ,'Weeks after treatment interruption'),
                        th(colspan = 1, 'Total'),
                        th(colspan = 3, 'PTCs'),
                        th(colspan = 3, 'NCs')
                      ),
                      tr(
                        lapply(c('Percentage with viral suppression (Total)','Percentage with viral suppression (PTCs)', 'Count with viral suppression (PTCs)', 'Count under follow-up (PTCs)','Percentage with viral suppression (NCs)', 'Count with viral suppression (NCs)', 'Count under follow-up (NCs)'), th)
                      )
                    )
                  )),
                rownames=FALSE,options = list(paging=FALSE, bInfo=FALSE, lengthChange=FALSE, searching = FALSE,
                                              columnDefs = list(list(className = 'dt-center', targets = "_all")))) %>% formatStyle(colnames(ptcs_plus_ncs_subset())[2],backgroundColor = "gray", color = "#fff", fontWeight = 'bold') %>% formatStyle(colnames(ptcs_plus_ncs_subset())[3],backgroundColor = "red", color = "#fff", fontWeight = 'bold') %>% formatStyle(colnames(ptcs_plus_ncs_subset())[6],backgroundColor = "blue", color = "#fff", fontWeight = 'bold')

    }
    })
}
Radwa Sharaf
  • 159
  • 14
  • hard to say for sure without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) but my guess is since you are defining your `eventReactive()`s with `ignoreNULL = FALSE` all of those blocks are being triggered on app start up and generating the "wrong" data you are seeing until you trigger a build via the button, which gives your the expected output. – Nate Mar 18 '19 at 18:20
  • Thanks for your reply Nate. If I remember correctly, I added `ignoreNULL = FALSE` to ensure that there is a table/graph when users first launch the app, instead of a blank page. If I delete `ignoreNULL=FALSE`, is there a work-around that blank page that you would recommend? – Radwa Sharaf Mar 18 '19 at 18:25
  • one way is add another conditional to your `renderDataTable()` call to return an empty `data.table()` if one of the required inputs is not yet populated. Another way is use the `req()` function inside of the renderDataTable to pause rendering until the required value is ["truthy"](https://shiny.rstudio.com/reference/shiny/latest/req.html), so you would insert `req(freq())` before the first `if` statement, assuming you remove all of the `ignoreNULL`s first – Nate Mar 18 '19 at 18:36

0 Answers0