0

I am not a programmer and have less than a month of experience with R but have been writing simple scripts that read from external CSV files for the past week. The function below, which reads data from a CSV file, was originally more complex but I repeatedly shortened it during troubleshooting until I was left with this:

newfunction <- function(input1, input2) {
  processingobject <- read.csv("processing-file.csv")
  print(head(processingobject))
}

I can print both the head and the entire processingobject within the script without a problem, but after the script ends processingobject no longer exists. It never appears in the RStudio global environment pane. Shouldn't processingobject still exist after the script terminates?

The script runs without displaying any error or warning messages. I tried assigning processingobject to a second variable: processingobject2 <- processingobject but the second variable doesn't exist after the script ends either. I also tried clearing the global environment and restarting RStudio, but that did not work either. If at the prompt after the script I type processingobject I get the message "Error: object 'processingobject' not found". The CSV file itself is perfectly normal as far as I can tell.

Obviously, I must be doing something very stupid. Please help me. Thanks.

  • That's right: `processingobject` no longer exists. It was a local object and after exit from the function, it will be garbage collected. The only objects whose values will persist are ones that were assigned to a name in the global environment via one of three mechanisms: 1) assignment of the last evaluation to a token in the global environment as is demonstrated for newcsv token in the answer below, 2) assignment to a token with `<<-`, or 3) assignment with `assign` using an argument to the 'env' parameter. – IRTFM Jan 15 '15 at 01:20

1 Answers1

0

You need to use return in your function and reproducible examples are always a good idea. It makes it easier for people to help you out.

ncol<- 10
nrow<- 100
x <- matrix(runif(nrow*ncol),nrow,ncol)
write.csv(x,file="example_data.csv")

newfunction <- function(input1) {
  processingobject <- read.csv("example_data.csv")
  result <- apply(processingobject,2,function(x)x*input1) #doing something to csv with input
  print(head(result))
  return(result)
}

newcsv <-newfunction(3)
Skiptoniam
  • 91
  • 1
  • 7
  • Thanks for your response. The problem is still present after adding the extra 'return(result)' line. The only change is that now after I run the function it returns NULL but the object is still not found. How can I make my example more reproducible - do I need to include the actual CSV file or some other information? – MGruden Jan 15 '15 at 00:31
  • Have a look at this post: [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) and edit your post so we can see what is happening. You might have an issue with reading in the csv? – Skiptoniam Jan 15 '15 at 00:44