0

I have an R script that I would like to run to import some data. The R script is called load_in_year1-4. I have designed this script so that I only have to make 3 changes to the code at the top and everything will run and import the correct data files.

The 3 changes are;

year <- "Year4"
weeks <- "1270_1321"
product <- "/cookies"

However I have 20 years worth of data and more than 50 products.

I am currently manually changing the top of each file and running it, so I have no errors currently in the data.

What I would like to do is to create a separate R script which will run the current script.

I would like to have something like

year <- c("year1", "year2", "year3"....)
weeks <- c("1270_1321", "1321_1327"....)
product <- c("product1", "product2"....)

So it will take year 1, week 1270_1321 and product1, call them year, week, product and run the R script which I have created.

Is there a grid function anybody can suggest?

EDIT: I have something like the following

#Make changes here
year <- "Year11"
weeks <- "1635_1686"
product <- "/cigets"

# year1: "1114_1165", year2: "1166_1217", year3: "1218_1269"


#Does not need changing
files <- gsub("Year1", as.character(year), "E:/DATA/Dataset/Year1")

parsedstub <- "E:/DATA/Dataset/files/"
produc <- paste0("prod", gsub("/", "_", as.character(product)))

drug <- "_drug_"
groc <- "_groc_"

####################Reading in the data###########################################

drug <- read.table(paste0(files, product, product, drug, weeks), header = TRUE)
groc <- read.table(paste0(files, product, product, groc, weeks), header = TRUE)
5
  • 1
    Why not make year, weeks, product be function arguments for a functional version of your original script, rather than things that you need to manually edit before rerunning? In any event, if you want more detailed help, please post a minimal reproducible example with R-reproducible data. Commented Oct 13, 2018 at 16:01
  • Can you elaborate a little further? Are you suggesting I create a function and wrap it around the whole script? Commented Oct 13, 2018 at 16:07
  • Yes, you seem to want to run the original script with different inputs. That is much easier to do if the inputs are actual inputs to a function rather than implicit inputs which you have to manually change in order to run. Commented Oct 13, 2018 at 16:09
  • I edited my original post with some of the code and where I would like the make changes Commented Oct 13, 2018 at 16:17
  • Do you have special (non-functional) requirements why you want to run script from another script multiple times like releasing memory after each run? Then you had to write the current actual arguments to a parameter file, otherwise the proposed answer with a function is perfect Commented Oct 13, 2018 at 16:42

1 Answer 1

1

To make a function out of your script, do something like this:

get.tables <- function(year,weeks,product){
  files <- gsub("Year1", as.character(year), "E:/DATA/Dataset/Year1")
  parsedstub <- "E:/DATA/Dataset/files/"
  product <- paste0("prod", gsub("/", "_", as.character(product)))
  drug <- "_drug_"
  groc <- "_groc_"
  ####################Reading in the data###########################################
  drug <- read.table(paste0(files, product, product, drug, weeks), header = TRUE)
  groc <- read.table(paste0(files, product, product, groc, weeks), header = TRUE)
  list(drug = drug, groc = groc)
}

Then you could use something in the apply family to apply this function to different years, weeks, and products.

Sign up to request clarification or add additional context in comments.

2 Comments

How would I apply the function to the different years, weeks and products? apply(years, get.tables)?
Okay got it : get.tables(year = year, weeks = weeks, product = product)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.