MRF
MRF

Reputation: 377

Making persistent changes to Shiny reactive dataframe

I have reactive dataframe which I would like to change variable names based on two user inputs. Firstly the user selects from a drop down list the name of the variable to change. Secondly the user inputs the new name of the variable in a text input then clicks a button to make the change. I have achieved replacing the text input with the original variable however when I go to change another variable the dataframe isn't 'updated' so the previous change is lost. Essentially I want to make persistent changes to the dataframe. An example code follows.

server.R

#### Initiate shinyServer
shinyServer(function(input, output) {

### This reactive function will take the input "inFile" from the UI.R and store it as data
  inFile<-reactive({

    file1<-input$rawdata

    if(is.null(file1)) stop ("Please Upload a Valid .csv file")
    datas<-fread(file1$datapath, skip="Block") #### <<- set as datas as a variable outside the function

    datas<-as.data.frame(datas)

  assign('datas', datas, envir=parent.env(environment()) )
    return(datas)

  })

## Set the UI for selecting the chamber name to change

output$Chamber_select<-renderUI({
  if(is.null(inFile())) 
    return()
  ## Get the data set with the unique chamber Names
  isolate(named_Chamber<-unique(datas2$Chamber)

  ## Fill the selection box with the available chambers
  selectInput("Chamber_to_rename", 'Chambers to rename', c(Choose=0, named_Chamber), multiple=FALSE)
})


#### Another dataframe which is reactive, allow it to be subsetted and variable (chamber) renamed based upon user input

datas_edit<-reactive({

  datas2=inFile()

  if (input$change>0){


 isolate(c1<-input$Chamber_to_rename)
 isolate(c2<-input$Chamber_change)

 isolate(datas2$Chamber[datas2$Chamber==c1]<-c2)   ## The text input replaces the chamber which is selected ### HEREIN THE PROBLEM LIES, DOES NOT UPDATE

  }
  isolate(assign('datas2', datas2, envir=parent.env(environment()) ))

})

})

ui.R

shinyUI(fluidPage(


  tabsetPanel(
    id = 'dataset',
    tabPanel('Tabular', dataTableOutput('table')),
    tabPanel('Box Plot', plotOutput('box'))),


  fluidRow(
    column(4,
           h4("1.File Options:"),
           br(),

           fileInput("rawdata", "Enter your .csv file")
           ),

      column(4,
             uiOutput("Chamber_select"),
             textInput("Chamber_change", "Change Chamber name"),
             actionButton("change","Make Change")

      )


    ) #end fluidrow
  )  ### Fluid page end 
)    #### Shiny UI end

Upvotes: 1

Views: 813

Answers (1)

MRF
MRF

Reputation: 377

The problem was fixed with guidance from @NicE. There appears to be an issue with !exists so is.null was used. The following code has been corrected.

#### Initiate shinyServer
shinyServer(function(input, output) {

### This reactive function will take the input "inFile" from the UI.R and store it as data
  inFile<-reactive({

    file1<-input$rawdata

    if(is.null(file1)) stop ("Please Upload a Valid .csv file")
    datas<-fread(file1$datapath, skip="Block") #### <<- set as datas as a variable outside the function

    datas<-as.data.frame(datas)

  assign('datas', datas, envir=parent.env(environment()) )
    return(datas)

  })

## Set the UI for selecting the chamber name to change

output$Chamber_select<-renderUI({
  if(is.null(inFile())) 
    return()
  ## Get the data set with the unique chamber Names
  isolate(named_Chamber<-unique(datas2$Chamber)

  ## Fill the selection box with the available chambers
  selectInput("Chamber_to_rename", 'Chambers to rename', c(Choose=0, named_Chamber), multiple=FALSE)
})


#### Another dataframe which is reactive, allow it to be subsetted and variable (chamber) renamed based upon user input

datas_edit<-reactive({

  if(is.null(datas2)){
       datas2<-inFile()
       return(datas2)
  }

  if (input$change>0){


 isolate(c1<-input$Chamber_to_rename)
 isolate(c2<-input$Chamber_change)

 isolate(datas2$Chamber[datas2$Chamber==c1]<-c2)   ## The text input replaces the chamber which is selected ### HEREIN THE PROBLEM LIES, DOES NOT UPDATE

  }
  isolate(assign('datas2', datas2, envir=parent.env(environment()) ))

})

})

Upvotes: 1

Related Questions