Mostafa90
Mostafa90

Reputation: 1706

Remove reactive expression in shiny app

My application works well with reactive expressions, but it's very slow. I have two functions :

What I want to do first is importing my files and fixing my data whithout "reactive({})", for example my data will be named "mydata". Secondly, I filter and represent "my.data" with reactive expressions. What I want is to keep my data in memory and don't change it like do my current program.

shinyServer(function(input, output) {

#   observe({


  cp <- reactive({

    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    read.delim(inFile$datapath, header = TRUE,
               sep = "\t",stringsAsFactors=FALSE)
  })

  up <- reactive({

    inFile <- input$file2

    if (is.null(inFile))
      return(NULL)

    read.delim(inFile$datapath, header = TRUE,
               sep = "\t",stringsAsFactors=FALSE)
  })

  tac <- reactive({

    inFile <- input$file3

    if (is.null(inFile))
      return(NULL)

    read.delim(inFile$datapath, header = TRUE,
               sep = "\t",stringsAsFactors=FALSE)
  })




    GetData <- reactive({
      my.data <- FixData(data.cp = cp(), data.up = up(), data.tac = tac())

      ifelse(input$case1 == TRUE & input$case2 == FALSE,
             my.data <- my.data[my.data$CONSTRUCTEUR %in% manufacturer &
                                  my.data$TYPE_DE_TERMINAL %in% input$terminal.type, ],
             ifelse(input$case2 == TRUE & input$case1 == FALSE,
                    my.data <- my.data[my.data$TYPE_DE_TERMINAL %in% terminal &
                                         my.data$CONSTRUCTEUR %in% input$manufacturer, ],
                    ifelse(input$case1 == TRUE & input$case2 == TRUE,
                           my.data <- my.data[my.data$CONSTRUCTEUR %in% manufacturer &
                                              my.data$TYPE_DE_TERMINAL %in% terminal, ],
                           my.data <- my.data[my.data$CONSTRUCTEUR %in% input$manufacturer & 
                                                my.data$TYPE_DE_TERMINAL %in% input$terminal.type, ]
                           )))
      if(input$lte == TRUE){
        my.data <- my.data[my.data$LTE == "Oui", ]
      }

      ifelse(input$case3 == TRUE,
             my.data <- my.data[my.data$Cause %in% cause, ],
             my.data <- my.data[my.data$Cause %in% input$pb, ]                   
             )

      my.data

      })



    GetData2 <- reactive({


      if (input$compare == "yes"){

        cp2 <- reactive({

          inFile <- input$file4

          if (is.null(inFile))
            return(NULL)

          read.delim(inFile$datapath, header = TRUE,
                     sep = "\t",stringsAsFactors=FALSE)
        })

        up2 <- reactive({

          inFile <- input$file5

          if (is.null(inFile))
            return(NULL)

          read.delim(inFile$datapath, header = TRUE,
                     sep = "\t",stringsAsFactors=FALSE)
        })




      my.data <- FixData(data.cp = cp2(), data.up = up2(), data.tac = tac())

      ifelse(input$case1 == TRUE & input$case2 == FALSE,
             my.data <- my.data[my.data$CONSTRUCTEUR %in% manufacturer &
                                  my.data$TYPE_DE_TERMINAL %in% input$terminal.type, ],
             ifelse(input$case2 == TRUE & input$case1 == FALSE,
                    my.data <- my.data[my.data$TYPE_DE_TERMINAL %in% terminal &
                                         my.data$CONSTRUCTEUR %in% input$manufacturer, ],
                    ifelse(input$case1 == TRUE & input$case2 == TRUE,
                           my.data <- my.data[my.data$CONSTRUCTEUR %in% manufacturer &
                                                my.data$TYPE_DE_TERMINAL %in% terminal, ],
                           my.data <- my.data[my.data$CONSTRUCTEUR %in% input$manufacturer & 
                                                my.data$TYPE_DE_TERMINAL %in% input$terminal.type, ]
                    )))
      if(input$lte == TRUE){
        my.data <- my.data[my.data$LTE == "Oui", ]
      }

      ifelse(input$case3 == TRUE,
             my.data <- my.data[my.data$Cause %in% cause, ],
             my.data <- my.data[my.data$Cause %in% input$pb, ]                   
      )

      my.data
      }  
    })

   observe({

   if(input$compare == "no"){

     output$myChart1 <- renderPlot({

       my.plot <- PlotDataManBis(GetData(), 
                              var = input$variable)
       my.plot

     })

     output$myChart2 <- renderPlot({

       my.plot <- PlotDataTypeBis(GetData(), 
                               var = input$variable)
       my.plot

     })

     output$myChart3 <- renderPlot({

       my.plot <- PlotDataCauseBis(GetData(), 
                                var = input$variable)
       my.plot

     })

     output$resume <- renderDataTable({

       my.data2 <- GetData()
       my.data2
     },
     options = list(bSortClasses = TRUE, iDisplayLength = 10,
                    aLengthMenu = list(c(5, 10, -1), c('5', '10', 'All'),
                                       aoColumnDefs = list(list(sWidth=c("100px"),
                                                                aTargets=list(0)))

                    ))
     )

   }else{


    output$myChart1 <- renderPlot({

      my.plot <- PlotDataMan(GetData(), GetData2(),
                             var = input$variable)
      my.plot

    })

  output$myChart2 <- renderPlot({

    my.plot <- PlotDataType(GetData(), GetData2(),
                            var = input$variable)
    my.plot

  })

  output$myChart3 <- renderPlot({

    my.plot <- PlotDataCause(GetData(), GetData2(),
                           var = input$variable)
    my.plot

  })

  output$resume <- renderDataTable({

    my.data2 <- GetData()
    my.data2
  },
  options = list(bSortClasses = TRUE, iDisplayLength = 10,
                 aLengthMenu = list(c(5, 10, -1), c('5', '10', 'All'),
                                    aoColumnDefs = list(list(sWidth=c("100px"),
                                                             aTargets=list(0)))

                 ))
  )}})
  #   })  
})

Thank you, Mos

Upvotes: 0

Views: 1123

Answers (1)

Colin Robinson
Colin Robinson

Reputation: 144

This page should help you https://gist.github.com/wch/9606002. Basically, if you put an actionButton in your file, you can put an isolate bracket around your file input, so that the data won't changed each time an input is changed. Look at that page for more of the intricacies, but essentially you would have an actionButton next to where the user inputs the file and indicates that the button reruns the app with that new data. Then in your server.R, would place input$nameofyouractionButton as the first line within the reactive expressions where you import and fix you data, and then isolate() around the rest of the reactive expression. Hope that helps!

Upvotes: 1

Related Questions