Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R load script objects to workspace

This is a rookie question that I cannot seem to figure out. Say you've built an R script that manipulates a few data frames. You run the script, it prints out the result. All is well. How is it possible to load objects created within the script to be used in the workspace? For example, say the script creates data frame df1. How can we access that in the workspace? Thanks.

Here is the script...simple function just reads a csv file and computes diff between columns 2 and 3...basically I would like to access spdat in workspace

mspreaddata<-function(filename){

# read csv file
rdat<-read.csv(filename,header=T,sep=",")


# compute spread value column 2-3
spdat$sp<-rdat[,2]-rdat[,3]
}
like image 445
user2238328 Avatar asked Jan 08 '14 19:01

user2238328


1 Answers

You should use the source function.

i.e. use source("script.R")

EDIT:

Check the documentation for further details. It'll run the script you call. The objects will then be in your workspace.

Alternatively you can save those objects using save and then load them using load.

like image 92
stanekam Avatar answered Oct 08 '22 22:10

stanekam