Automatic creation of 200 plots.

Im building about 200 different plots with pulse as Y-axis and time as X-axis. The pulse data for the plots are extracted from individual csv files while the time data is extracted from a txt file containing info on all the cases. The name of the csv file looks like this: 76P1XBE9_qcpr_perQRS.csv. The time data is extracted from the txt file by using subset() (C1<-subset(C, ID == "76P1XBE9_log.csv", select = c("ID","state","to","time1", "time2"))). As you see i have to insert 76P1XBE9 at several points.

The code for the plot is complete and working. I could manually insert the casename (eg. 76P1XBE9) at the 3 or 4 places its needed, create the plot and export it to a word or pdf file and then repeat this 200 times.
I would really like R to automatically find the case name in my Excel overview file, insert it where its needed, create the plot and add it to a word file automatically. The final product will therefore be a word document containing all my plots.
Is this possible and how can i go about it?

break this down into 3 parts

  1. turn your code into a function,
    in this way you can call your code with a single param ('76P1BE9') for example and get the plot creation that you said you achieved.
    You do this by replacing the hardcoded value of 76P1BE9 with a variable.
    Read up on R Functions.
  2. iterate over a list of param values (your 76*type codes.) calling the function for each one automatically.
    subtask 1) make a list of the param values
  3. iterate over this, calling function on it. easiest approach is with purrr library, the walk and map functions
  4. work out how to construct PDF output, probably a simple markdown approach would work. read up on knitr and rmarkdown.

How can i make R understand that part of the filename is a function.
The code i use for opening the dataframe is "read.csv("76P1XBE9_qcpr_perQRS.csv", sep=";", na.strings="NaN")". I would like to say to R that

A<- 76P1XBE9
"read.csv("A_qcpr_perQRS.csv", sep=";", na.strings="NaN")"

When i do this it tell me that it cant find the file, which is understandable.

you need a function that can take two strings and stick them together. and that allows a variable to stand in for those strings.
the base R function for that are paste and paste0. For you paste0 would be best because it makes a good assumption of seperator between strings (i.e. none). I prefer meaningful variable names, so I changed your A to codename

codename<- "76P1XBE9"
read.csv(paste0(codename,"_qcpr_perQRS.csv"), sep=";", na.strings="NaN")

If the csv files are in a single directory, you can use R's file and directory functions.
Example, x <- list.files(path=<YOUR_DIR>) #x is vector of file names in YOUR_DIR.
Then just simply loop over this.
There's some small string manipulations you'll need plus some differences between windows vs linux systems.
Otherwise, it's pretty straightforward.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.