How to save an output to use it later in another output in Shiny R

I want to use the results of 2 outputs in shiny to another output for plotting : The first output is soq1 below:

output$soq <- renderTable({

     if (input$selectedLevels == "Technical Junior")
     {soq1<-sum(simplegap1[,input$Candidate]>0)}

     else if (input$selectedLevels == "Entry Level")
     {soq1<-sum(simplegap2[,input$Candidate]>0)}

     else if (input$selectedLevels == "Product Owner")
     {soq1<-sum(simplegap3[,input$Candidate]>0)}

     else if (input$selectedLevels == "Technical Leader")
     {soq1<-sum(simplegap4[,input$Candidate]>0)}

     else if (input$selectedLevels == "Senior Manager") 
     {soq1<-sum(simplegap5[,input$Candidate]>0)} 
     }, include.rownames = TRUE ,  bordered = TRUE ,hover = TRUE, align = "c" ) 

the second output i want to use later is :

output$suq <- renderTable({

     if (input$selectedLevels == "Technical Junior")
     {suq1<-sum(simplegap1[,input$Candidate]<0)}

     else if (input$selectedLevels == "Entry Level")
     {suq1<-sum(simplegap2[,input$Candidate]<0)}

     else if (input$selectedLevels == "Product Owner")
     {suq1<-sum(simplegap3[,input$Candidate]<0)}

     else if (input$selectedLevels == "Technical Leader")
     {suq1<-sum(simplegap4[,input$Candidate]<0)}

     else if (input$selectedLevels == "Senior Manager") 
     {suq1<-sum(simplegap5[,input$Candidate]<0)} 
     }, include.rownames = TRUE ,  bordered = TRUE ,hover = TRUE, align = "c" )

And later i want to use the result soq1 and suq1 to another output for calculations :

output$qsplot <- renderPlot({

      if (input$selectedLevels == "Technical Junior")
      { plot(x,-x,type = "p",main = "Qualification Space",xlim = c(0,1),ylim = c(-1,0), xlab = "SOQ",ylab = "SUQ")
       points(soq1,suq1,type="o",pch=20) }

    })

Upvotes: 3

Views: 1382

Answers (1)

Tonio Liebrand
Tonio Liebrand

Reputation: 17689

I am not sure, but I think you are either looking for: reactive() or reactiveValues(). I think the cleaner version would be to use reactive(), but to stay a bit closer to your code snippet, I will use the latter one and perform the data modification with the the renderTable() function.

mat <- matrix(1:9, ncol = 3)

ui <- fluidPage(
  tableOutput("table1"),
  tableOutput("table2")
)

server <- function(input, output, session){
  global <- reactiveValues(mat = NULL)
  
  output$table1 <- renderTable({
    matNew <- mat
    # edit the table
    matNew[, 1] <- 1
    global$mat <- matNew
  })

  output$table2 <- renderTable({
    if(!is.null(global$mat)){
      return(global$mat)
    }
  })
}

shinyApp(ui = ui, server = server)

Upvotes: 4

Related Questions