0

I am trying to automate editing a csv. I want the user of the Shiny app to be able to choose which options they want to to clean in their csv. To clarify, I want them to be able to click one button, then update the csv, then click another button and update again while maintaining the original updates. Currently, I am looking at the case of just two buttons, first I would click the 'Load' button to load and view the csv; then I want to click the "Email Cleaned" button to append a new column of cleaned emails. Ultimately, I want to add more buttons.

My code is currently crashing with the error:


Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)


My code is as follows:

ui

    ui = fluidPage(
       sidebarPanel(
           fileInput('file1', 'Choose file to upload',accept = c('text/csv','text/comma-separated-values','text/tab-separated-values','text/plain','.csv','.tsv')),
           checkboxInput('header', 'Header', TRUE),
           radioButtons('sep', 'Separator',c(Comma=',',Semicolon=';',Tab='\t'),'Comma'),
           radioButtons('quote', 'Quote',c(None='','Double Quote'='"','Single Quote'="'"),'Double Quote'),
           actionButton("Load", "Load File"),
           actionButton("Email", "Clean Email")),
           mainPanel(tableOutput("my_output_data"))
       )

server

           server = function(input, output) {

             ###Load CSV File
             data1 <- reactive({
               if(input$Load == 0){return()}
               inFile <- input$file1
               if (is.null(inFile)){return(NULL)}

               isolate({ 
                 input$Load
                 my_data <- read.csv(inFile$datapath, header = input$header,sep = input$sep, quote = input$quote,stringsAsFactors =FALSE)
               })
               my_data
             })
               output$my_output_data <- renderTable({data1()},include.rownames=FALSE) 

             data2 <- reactive({
               if(input$Email == 0){return()}
               inFile <- input$file1
               if (is.null(inFile)){return(NULL)}

               isolate({ 
                 input$Email
                 my_data <- read.csv(inFile$datapath, header = input$header,sep = input$sep, quote = input$quote,stringsAsFactors =FALSE)

                 ###CLEAN EMAIL BUTTON FORMULA
                 email_clean <- function(email, invalid = NA)
                 {
                   email <- trimws(email) 
                   email[(nchar(email) %in% c(1,2)) ] <- invalid
                   email[!grepl("@", email)] <- invalid
                   bad_email <- c("\\@no.com", "\\@na.com","\\@none.com","\\@email.com",
                   "\\@noemail.com", "\\@directcapital.com", "\\@test.com", 
                   "noemail")
                   pattern = paste0("(?i)\\b",paste0(bad_email,collapse="\\b|\\b"),"\\b")
                   email <-gsub(pattern, invalid, sapply(email,as.character))
                   unname(email)
                 }
                 Cleaned_Email <- email_clean(my_data$Email)
                 my_data<-cbind(my_data,Cleaned_Email)
               })
               my_data},

               {
               if(input$Load == 0){return()}
               inFile <- input$file1
               if (is.null(inFile)){return(NULL)}

               isolate({ 
                 input$Load
                 my_data <- read.csv(inFile$datapath, header = input$header,sep = input$sep, quote = input$quote,stringsAsFactors =FALSE)
               })
               my_data}
             )
             output$my_output_data <- renderTable({data2()},include.rownames=FALSE)  

           }



     shinyApp(ui = ui, server = server)

Thank you!

Maddie
  • 87
  • 1
  • 6
  • 1
    something is wrong in your `data2`, what are you trying to do after the `isolate`, you close the curly brackets for the `reactive` block and add more code. Probably just a typo, if you remove the `my_data},{` before the `if(input$Load == 0)` it works fine. – NicE Jan 14 '16 at 14:32
  • I have done that but it doesn't seem to be fixing the issue? – Maddie Jan 15 '16 at 15:36

0 Answers0