Search code examples
rcustom-function

How to use textstring as variable in custom R function


I have the following problem: I have a ton of xlsx files containing the same type of data, and I want to load and manipulate them using a function, so that I dont have to copy paste a new name every time I want to run my code on a new file. My code is as follows (where Laerkevejen_1.xlsx is the filename) :

library(ggplot2)

library(dplyr)

library(readxl)

library(cowplot)

Laerkestien_1 <- read_excel("H:/Projekter/SagsAndSwells/Adresser og Alarmer/Laerkestien_1.xlsx")

Laerkestien_1$EventName <- as.factor(Laerkestien_1$EventName)


Laerkestien_1_NAs <- filter(Laerkestien_1,EventName=="Historical Power Outage" | EventName=="Historical Power Restore" |EventName=="ReverseCurrentStart"|EventName=="Meter Power Outage Event"|EventName=="Meter Power Restore Event"|EventName=="Meter Reverse Rotation"|EventName=="Phase 1 Power Loss"|EventName=="Phase 2 Power Loss"|EventName=="Phase 3 Power Loss"|EventName=="Phase1_2PowerLossStart"|EventName=="Phase2_3PowerLossStart"|EventName=="Phase1_3PowerLossStart")

Laerkestien_1_SagSwells <- filter(Laerkestien_1,EventName=="SagPhase1Stop" |EventName=="SagPhase2Stop" |EventName=="SagPhase3Stop" |EventName=="SwellPhase1Stop" |EventName=="SwellPhase2Stop" |EventName=="SwellPhase3Stop" )

Laerkestien_1_THDs <- filter(Laerkestien_1,EventName=="AverageVoltTHDPhase1"|EventName=="AverageVoltTHDPhase2"|EventName=="AverageVoltTHDPhase3"|EventName=="MaximumVoltTHDPhase1"|EventName=="MaximumVoltTHDPhase2"|EventName=="MaximumVoltTHDPhase3")

Laerkestien_1$LocalEventTime <- as.POSIXlt(Laerkestien_1$LocalEventTime)

Laerkestien_1_NAs$LocalEventTime <- as.POSIXlt(Laerkestien_1_NAs$LocalEventTime)

Laerkestien_1_SagSwells$LocalEventTime <- as.POSIXlt(Laerkestien_1_SagSwells$LocalEventTime)

Laerkestien_1_THDs$LocalEventTime <- as.POSIXlt(Laerkestien_1_THDs$LocalEventTime)

Laerkestien_1_NAs$MeasuredValue <- as.factor(Laerkestien_1_NAs$MeasuredValue)

Laerkestien_1_SagSwells$MeasuredValue <- as.factor(Laerkestien_1_SagSwells$MeasuredValue)

Laerkestien_1_THDs$MeasuredValue <- as.factor(Laerkestien_1_THDs$MeasuredValue)

ggplot(Laerkestien_1_SagSwells)+geom_point(aes(x=LocalEventTime,y=MeasuredValue,color=EventName))+scale_x_datetime(date_breaks = "1 month",date_labels = "%b %y")+scale_colour_manual(values=c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7","#000000","red","cyan"))+theme(axis.text.x = element_text(angle=90))+ggtitle("Laerkevej_7_SagsSwells")

ggsave("Laerkestien_1_SagSwells.pdf",width=20,units="in")

ggplot(Laerkestien_1_THDs) + geom_point(aes(LocalEventTime,y=MeasuredValue,color=EventName))+scale_x_datetime(date_breaks = "1 month",date_labels = "%b %y")+scale_colour_manual(values=c("#F0E442", "#0072B2", "#D55E00", "#CC79A7","#000000","red","cyan"))+theme(axis.text.x = element_text(angle=90))+ggtitle("Laerkevej_7_THDs")

ggsave("Laerkestien_1_THDs.pdf",width=20,units="in")

ggplot(Laerkestien_1_NAs)+geom_point(aes(LocalEventTime,y=MeasuredValue,color=EventName))+scale_x_datetime(date_breaks = "1 month",date_labels = "%b %y")+scale_colour_manual(values = c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7","#000000","red","cyan"))+theme(axis.text.x = element_text(angle=90))+ggtitle("Laerkevej_7_NAs")

ggsave("Laerkestien_1_NAs.pdf",width=20,units="in")

Now, I was thinking of doing all of this in a function, so that I could call Myfun(Laerkevejen_1) or Myfun(Rolsoegaardvej_31), and get the plots and things that I need. Right now, I am copy pasting, and it works, but it seems very unelegant...

However, I am not sure how to actually create the function so that 'Laerkevejen_1' is passed as the variable name and used like in my current code.

Anyone who go an idea of how to do this in a smooth way?


Solution

  • Laerkevejen_1 seems to be the data, not a variable. You could simply create a standardized object for the dataset at the beginning of your function:

    FUN <- function(x){
      # Here we use the x to read the data into the internal data object
      temp_data <- read_excel(paste0("H:/Projekter/SagsAndSwells/Adresser og Alarmer/", x, ".xlsx"))
    
      # from now on we replace the "Laerkevejen_1" in your code above with "temp_data"
      temp_data$EventName <- as.factor(temp_data$EventName)
    
      [...]
    
      # Here we use the x again to get the string "Laerkejeven_1" into the filename
      ggsave(paste0(x, "_SagSwells.pdf"), width = 20, units = "in")
    
      [...]
    }
    

    Then you call it with FUN("Laerkevejen_1") and get your usual results.