rshinyshiny-servershiny-reactivity

Error in calling a variable written in a reactive expression


I'd like to call pcrPositiveFailedDetails, which is currently a tibble that is reactive. The tibble will change values whether the input$sPP is greater/less than highCT. The tibble provides what I want when I use the print() and make a selection for input$sPP. In addition to the RTable, I want to display the tibble (pcrPositiveFailedDetails) as an output called failedPcrTable. The solutions I tried below didn't work, and what I get is no table with the warning: Unknown or uninitialised column: pcrPositiveFailedDetails.

I've tried several ways, including:

1)

fPCR <- reactive({RTable()$pcrPositiveFailedDetails})

output$failedPcrTable <- renderTable({fPCR()})

2)

output$failedPcrTable <- renderTable({RTable()$pcrPositiveFailedDetails})

3)

output$failedPcrTable <- renderTable({RTable$pcrPositiveFailedDetails})

4)

output$failedPcrTable <- renderTable({pcrPositiveFailedDetails})

Here's the relevant code:

`RTable <- reactive({
highCT <- input$highCT
pcrTable <- fullTable() %>% select(all_of(input$sPP))

#checks all values of pcrTable pass
pcrPositiveFull <- pcrTable[input$sPP] < highCT & !(pcrTable[input$sPP] == "" |   
is.na(pcrTable[input$sPP])) 

# identify which rows have failed values
pcrPositiveFailedRows <- apply(pcrPositiveFull,1, FUN=function(x){all(x)})

# create summary table with only failed samples
pcrPositiveSamples <- fullTable() %>% select(SampleID, all_of(input$sPP))
pcrPositiveFailedDetails <- pcrPositiveSamples[!pcrPositiveFailedRows,]

pcrResult <- if (all(pcrPositiveFull)) "PASS" else "FAIL" 

print(pcrTable)
print(pcrPositiveFull)
print(all(pcrPositiveFull))
print(pcrPositiveFailedDetails)

tibble(
  "Control Type" = c("PCR Positive Control", 
                     "Reverse Transcription Control", 
                     "No Template Control", 
                     "Genomic Contamination Control"),
  
  "Purpose" = c("To test if your PCR reactions worked",
                "To test if your RT reactions worked", 
                "Checks for RNA Contamination",
                "Checks for DNA Contamination"),
  
  "Pass Criteria" = c("Ct < High Ct Cutoff",
                      "Ct < High Ct Cutoff",
                      "Ct > High Ct Cutoff or No Ct",
                      "Ct > High Ct Cutoff or No Ct"),
  
  "Result" = c(pcrResult, rtcResult, ntcResult, gcResult) 
)
})

Solution

  • A reactive is basically a function, so when you call it you can only access what the function returns. To give a basic example, if I have

    table <- reactive({
       x <- 1
       tibble(a = 1, b = 2)
    })
    

    then I can't access x outside of this reactive.

    In your case, pcrPositiveFailedDetails is not returned by the reactive so can't be accessed outside of it. To assign this to a value outside of the reactive, you can create a blank reactive value in the server and then update this from your RTable reactive. This would look like

    server <- function(input, output, session) {
       fPCR <- reactiveVal()
    
       RTable <- reactive({
          # ...
          pcrPositiveFailedDetails <- pcrPositiveSamples[!pcrPositiveFailedRows,]
          fPCR(pcrPositiveFailedDetails) # this updates the value of fPCR
          # ...
       })
    
       output$failedPcrTable <- renderTable({fPCR()})   
    }