PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

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

 August 18, 2022     assign, output, r, variables     No comments   

Issue

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) }

    })

Solution

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)


Answered By - Tonio Liebrand
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing