【发布时间】:2017-09-20 23:57:59
【问题描述】:
我有三个向量,我需要将它们应用于模板、替换某些内容并创建新文件。这是函数:
multx<-function(){
readLines(Template) %>%
gsub(pattern = "stx", replace = stimearray) %>% #Replaces the start time
gsub(pattern = "etx", replace = etimearray) %>% #Replaces the end time
write.table(., paste0("ds/", iNames), #Writes out a file for every batch
row.names=F, col.names=F, quote = F)
}
x <- mapply(multx)
这是创建也用于其他函数的全局变量的部分:
runStart <- lubridate::ymd_hm(startDate) #Start date
stimearray <- runStart + months(0:(nMonths-1))
etimearray <- runStart + months(1:nMonths) - lubridate::dhours(1)
但在这种情况下,stimearray、etimearray 和 iNames 是 vectors,它们在之前的计算中已经在全局环境中可用。
如何创建一个空参数函数来创建批量文件?
或者,还有其他方法吗?
数据
模板
c("", "\"! ***********************************************************************************************************************\"",
"\"simulStart stx ! (01) simulation start time -- must be in single quotes\"",
"\"simulFinsh etx ! (02) simulation end time -- must be in single quotes\"",
"\"\"", "\"! ***********************************************************************************************************************\""
)
输入(时间数组)
structure(c(1475280000, 1477958400, 1480550400, 1483228800, 1485907200,
1488326400, 1491004800, 1493596800, 1496275200, 1498867200, 1501545600,
1504224000, 1506816000, 1509494400, 1512086400, 1514764800, 1517443200,
1519862400, 1522540800, 1525132800), class = c("POSIXct", "POSIXt"
), tzone = "UTC")
dput(etimearray)
structure(c(1477954800, 1480546800, 1483225200, 1485903600, 1488322800,
1491001200, 1493593200, 1496271600, 1498863600, 1501542000, 1504220400,
1506812400, 1509490800, 1512082800, 1514761200, 1517439600, 1519858800,
1522537200, 1525129200, 1527807600), tzone = "UTC", class = c("POSIXct",
"POSIXt"))
【问题讨论】:
-
我不认为在自定义函数中使用全局环境变量是可取的......你不能只添加相应的参数吗?如果它们是从另一个函数调用传递的,那么你应该使用
...我猜 -
对函数调用
mapply而不提供任何参数进行迭代只会返回list(),这可能不是您想要的。 -
@DamianoFantini 如果这些变量也用于其他函数怎么办?我是否将计算添加到每个函数?还有另一种组织方式吗?计算时间不长。但是这些变量被用于 3-4 个函数。
-
@alistaire 你能解释一下我应该做什么。没看懂,
-
@DamianoFantini 你的意思是把变量作为参数传递,即使变量已经在全局范围内可用?
标签: r function apply lapply mapply