简体   繁体   English

如何在自定义R函数中将文本字符串用作变量

[英]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. 我有以下问题:我有大量的xlsx文件包含相同类型的数据,并且我想使用一个函数加载和操作它们,这样我每次运行我的文件时就不必复制粘贴新名称。新文件上的代码。 My code is as follows (where Laerkevejen_1.xlsx is the filename) : 我的代码如下(其中Laerkevejen_1.xlsx是文件名):

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. 现在,我正在考虑在一个函数中进行所有这些操作,以便可以调用Myfun(Laerkevejen_1)Myfun(Rolsoegaardvej_31)并获取所需的图和所需的东西。 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. 但是,我不确定如何实际创建函数,以便将“ Laerkevejen_1”作为变量名传递并像在当前代码中一样使用。

Anyone who go an idea of how to do this in a smooth way? 有谁想过如何顺利进行此操作的人?

Laerkevejen_1 seems to be the data, not a variable. Laerkevejen_1似乎是数据,而不是变量。 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. 然后,使用FUN("Laerkevejen_1")调用, FUN("Laerkevejen_1")获得通常的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM