Ketty
Ketty

Reputation: 851

Dataset (a reactiveValue) can not be found inside regular R functions

I have a dataset (called mydata$data) that I would like to keep updated at all time. First, this dataset is read in as 'rawdata'. Then it is updated by several regular R functions.

Below are my codes. When I run the app, I always get this error.

Warning: Error in <-: object 'mydata' not found
Stack trace (innermost first):
    106: PrepareData [./functions-sample.R#2]

Can anybody help? How should I define that dataset? How can I update it using regular R functions?

Rawdata:

colA <- c('A','B','C','D','E')
colB <- c(1,2,3,4,5)
rawdata <- as.data.frame(cbind(colA,colB))
View(rawdata)

ui.R:

fluidPage(
  sidebarPanel(
    actionButton("runButton", strong("Run!"))
  ),
  mainPanel(
    tabsetPanel(id = "allResults",
              tabPanel(value='inputVars',title='Variable', DT::dataTableOutput('inputVars')),
              tabPanel(value='result',title='Result', DT::dataTableOutput('result')),
              tabPanel(value='temp',title="TEMP", verbatimTextOutput("temp"))
    )
  )
)

server.R:

source("./functions-sample.R")

function(input, output, session) {

  # Activate tab 'Result' when users click 'Run'
  observeEvent(input$runButton, {
    updateTabsetPanel(session, "allResults", 'result')
  })

  mydata <- reactiveValues()

  ### RESULT TAB ###

  # mydata$data is the active dataset we want to keep updating
  result <- eventReactive (input$runButton, {
    mydata$data <- rawdata
    PrepareData(mydata$data)
    head(mydata$data,5)
  })

  ### RENDERING FUNCTIONS ###

  # Default SummaryTable
  output$inputVars <- DT::renderDataTable({
      DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE))
  })

  # Display results
  output$result <- DT::renderDataTable({
    DT::datatable(result(), options = list(paging = FALSE, searching = FALSE))
  })

  output$temp <- renderPrint({
    print(head(mydata$data))
  })
}

functions-sample.R:

PrepareData<-function(datax) {
  mydata$data <- RankData(datax)
  CodeMissingData(mydata$data)
}

RankData<-function(datax) {
  return(datax[order(datax[,1],datax[,2]),])
}

CodeMissingData<-function(datax) {
  for (v in 4:ncol(datax)) {
    if (!("*NA*" %in% levels(datax[,v]))) {levels(datax[,v])<<-c(levels(datax[,v]),"*NA*")}
  }
  datax[,-(1:3)][is.na(datax[,-(1:3)]) | datax[,-(1:3)]=="NA"]<<-"*NA*"
  mydata$data <- datax
}

Upvotes: 0

Views: 62

Answers (1)

HubertL
HubertL

Reputation: 19544

Try returning the data from your function instead of modify it:

CodeMissingData<-function(datax) {
  for (v in 4:ncol(datax)) {
    if (!("*NA*" %in% levels(datax[,v]))) {levels(datax[,v])<-c(levels(datax[,v]),"*NA*")}
  }
  datax[,-(1:3)][is.na(datax[,-(1:3)]) | datax[,-(1:3)]=="NA"]<-"*NA*"
  return(datax)
}

PrepareData<-function(datax) {
  CodeMissingData(RankData(datax))
}

Then in server.R

 result <- eventReactive (input$runButton, {
    mydata$data <- PrepareData(rawdata)
    head(mydata$data,5)
  })

Upvotes: 2

Related Questions