Sebastian Zeki Sebastian Zeki - 20 days ago 5
R Question

How to pass values between R scripts

I have an R script that takes a xls spreadsheet, cleans it up and then does some analysis.

The source spreadsheet changes as does the analysis but the clean up is always the same so I would like to do the following in any new script I write
1. Read the xls file
2. Send the xls file to be cleaned up into a dataframe in a clean-up script
3. Send the cleaned-up dataframe back to the original script for further analysis

I know that I can get the dataframe back from the clean-up script and use it using source. What I don't understand is how to send the original file to the clean up script in the first place:

#How do I make sure that the file I want to be cleaned up is sent to the CleanUP script below
df<-as.data.frame(source("/Users/sebastianzeki/CleanUp.R"))
#Further analysis then performed on df


Do I have to do this via command line args and sending the file over as an argument. If so, how do I also specify the file in the receiving
CleanUp.R


And here is the CleanUp.R script

library(ggplot2)
library(gtools)
library(openxlsx)

MyData<-read.xlsx("/Users/sebastianzeki/Reports.xlsx", sheet = 1, startRow = 1, colNames = TRUE)
MyData$Dx<-gsub("[Nn]o .*?\n","",MyData$Dx)
MyData$Dx<-gsub("[Nn]ormal.*\n","",MyData$Dx)

Answer

You can easily turn this into a function:

cleanUp <- function( file ) {
    library(openxlsx) 
    MyData<-read.xlsx(file, sheet = 1, startRow = 1, colNames = TRUE) 
    MyData$Dx<-gsub("[Nn]o .*?\n","",MyData$Dx) 
    MyData$Dx<-gsub("[Nn]ormal.*\n","",MyData$Dx)
    return(MyData)
}

Source that in your script (or build it into a package and load the package) then run:

df <- cleanUp( "/Users/sebastianzeki/Reports.xlsx" )

Edit: I believe you shouldn't need ggplot2 or gtools in the function, so I've removed them. Put them back if you do need them in there.