1

I've coded a shiny app. It reads any input data in csv format. I've also introduced a column selector to let the user select which columns he/she wants to analyze. My problem is that I want let the user also erase missing values but when the user click 'Erase missings' it reads the data again and don't respect variable selection user has done before.

So I don't know hot to separate both operations read input data and erase missings values.

Thanks in advance!!

data1<-reactive({ fichero<-input$fichero
#Si el fichero que leemos no tiene nada devolvemos un Nulo
if (is.null(fichero))
  {return()}
# Leemos el fichero
salida<-read.csv(fichero$datapath, header=input$header,              sep=input$sep,quote=input$quote,stringsAsFactors=input$stringAsFactors)
# Si marcamos la opción de no missings entonces solo se tienen en cuenta los      registros que no tienen ningun NA
# if (input$miss==TRUE) {salida<-salida[complete.cases(salida),]}
return(salida)
})

data<-reactive({ 
data1f<-data1()
salida<-data1f
# Si marcamos la opción de no missings entonces solo se tienen en cuenta los registros que no tienen ningun NA
if (input$miss==TRUE) {salida<-data1f[complete.cases(data1f),]}
return(salida)
})

1 Answers1

0

You'll have to forgive me for not being able to read your comments, so I may not fully understand what you're trying to do. You also haven't provided a reproducible example so I've put together a skeleton server.R as a guide, not a fully functioning example

Your app reads the data each time because of this line

data1f<-data1()      ## this is a call to the reactive `data1` function every time `input$miss` is changed

One way you can preserve the data is to use reactiveValues() and only overwrite it when the button is pressed.

So in server.R you would have something like

function(input, output, session) {

    reactive_values <- reactiveValues()
    reactive_values$salida_original <- NULL
    reactive_values$salida_changed <- NULL

    ##  ...

    ## Observe the button press
    observe({ 

        if(input$fichero){

            isolate({  ## To stop a dependancy on the other inputs$ you are calling
                salida<-read.csv(fichero$datapath, 
                                 header=input$header,              
                                 sep=input$sep,
                                 quote=input$quote,
                                 stringsAsFactors=input$stringAsFactors)    

                reactive_values$salida_original <- salida
                reactive_values$salida_changed <- salida

            })
        }
    })

    ## Now we have the data stored in a reactive_value, and can make changes to it as and when required
    ## without calling the 'read.csv' each time
    data <- reactive({
        salida_original <- reactive_values$salida_original   ## the untouched, original data
        salida_changed <- reactive_values$salida_changed     ## the data that gets changed each time
        if(input$miss) {
            ## Code to change the data here
            ## salida <- salida_changed[complete.cases(salida_changed), ]
        }

        ## update salida_changed 
        reactive_values$salida_changed <- salida             ## update the changed data as and when a change i srequired
        return(salida)

    })

}
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • Thanks a lot!!! It totally works now!! I hadn't heard of reactive_values() till now. – Manuel Fco Aviles Mar 09 '16 at 12:58
  • @ManuelFcoAviles - no problem. reactive values are extremely useful. If this answers your question you should mark it as answered by pressing the 'tick' at the top-left of this answer – SymbolixAU Mar 09 '16 at 19:33
  • I can't press like button. it says I vae no enough reputation or something like that. – Manuel Fco Aviles Mar 14 '16 at 16:15
  • Sorry but I can't make your solution work. I've changed a few things because in the reactive piece of code it needs to know what happen if input$miss==0 so i write else {salida<-salida_original}. Also I've written fichero<-input$fichero in the observe code.At the end I keep having same problem. Onse i chek input$miss field it reloads the file even if I've replaced some variables using an output$choose_columns object which it uses data(). Do you know what is happening? – Manuel Fco Aviles Mar 14 '16 at 18:49
  • @ManuelFcoAviles - are you able to post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that I/someone else can run to see your issue? Otherwise it's hard to understand your problem – SymbolixAU Mar 15 '16 at 09:32
  • The code that works. data <- reactive({if (is.null(input$fichero)|| is.null(input$columns)) {salida_original <- reactive_values$salida_original salida_changed <-reactive_values$salida_changed } else {salida_original <- reactive_values$salida_original salida_changed <- reactive_values$salida_changed salida_original<-salida_original[, input$columns, drop = FALSE] salida_changed<-salida_changed[, input$columns, drop = FALSE]} if(input$miss) {salida<-salida_changed[complete.cases(salida_changed),]} else {salida<-salida_original} reactive_values$salida_changed <- salida return(salida)}) – Manuel Fco Aviles Mar 15 '16 at 11:17