如何创建一个带有空参数的函数?

How can I create a function with null arguments?

我有三个向量,我需要将它们应用于模板、替换某些内容并创建新文件。这是函数:

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)

但在这种情况下,stimearrayetimearrayiNamesvectors,根据之前的计算已在全局环境中可用。

如何创建一个空参数函数来创建批处理文件?

或者,还有其他方法吗?

数据

模板

c("", "\"! ***********************************************************************************************************************\"", 
"\"simulStart              stx     ! (01) simulation start time -- must be in single quotes\"", 
"\"simulFinsh              etx      ! (02) simulation end time -- must be in single quotes\"", 
"\"\"", "\"! ***********************************************************************************************************************\""
)

dput(stimearray)

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"))

您可以使用 Maplapply 的多变量版本)来执行此操作:

template <- c("", "\"! ***********************************************************************************************************************\"", 
              "\"simulStart              stx     ! (01) simulation start time -- must be in single quotes\"", 
              "\"simulFinsh              etx      ! (02) simulation end time -- must be in single quotes\"", 
              "\"\"", "\"! ***********************************************************************************************************************\"")

stime <- 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")

etime <- 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"))

dir.create("ds")

file_data <- Map(function(start, end, name){
        filled_template <- gsub("stx", start, template, fixed = TRUE)
        filled_template <- gsub("etx", end, filled_template, fixed = TRUE)
        writeLines(filled_template, file.path("ds", name))
        filled_template    # return vector written to file
    }, 
    start = stime,
    end = etime,
    name = paste0("file-", as.integer(stime), ".txt")
)

file_data[1:2]
#> [[1]]
#> [1] ""                                                                                                                             
#> [2] "\"! ***********************************************************************************************************************\""
#> [3] "\"simulStart              2016-10-01     ! (01) simulation start time -- must be in single quotes\""                          
#> [4] "\"simulFinsh              2016-10-31 23:00:00      ! (02) simulation end time -- must be in single quotes\""                  
#> [5] "\"\""                                                                                                                         
#> [6] "\"! ***********************************************************************************************************************\""
#> 
#> [[2]]
#> [1] ""                                                                                                                             
#> [2] "\"! ***********************************************************************************************************************\""
#> [3] "\"simulStart              2016-11-01     ! (01) simulation start time -- must be in single quotes\""                          
#> [4] "\"simulFinsh              2016-11-30 23:00:00      ! (02) simulation end time -- must be in single quotes\""                  
#> [5] "\"\""                                                                                                                         
#> [6] "\"! ***********************************************************************************************************************\""

请注意,午夜 POSIXct 时间的默认打印方法省略了时间部分。如果你想要它打印,请在时间调用 format,例如format(start, "%F %T").