R 如何创建具有空参数的函数?

R 如何创建具有空参数的函数?,r,function,apply,lapply,mapply,R,Function,Apply,Lapply,Mapply,我有三个向量,我需要将它们应用于模板、替换某些内容和创建新文件。这就是功能: multx<-function(){ readLines(Template) %>% gsub(pattern = "stx", replace = stimearray) %>% #Replaces the start time gsub(pattern = "etx", replace = etimearray) %>% #Repl

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

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)
dput(Stimarray) dput(etimearray)
您可以使用
Map
,即
lappy
的多变量版本来执行此操作:

模板[5]“\”
#> [6] "\"! ***********************************************************************************************************************\""
#> 
#> [[2]]
#> [1] ""                                                                                                                             
#> [2] "\"! ***********************************************************************************************************************\""
#>[3]“\”同步启动2016-11-01!(01)模拟开始时间--必须在单引号中“”
#>[4]“\”simulFinsh 2016-11-30 23:00:00!(02)模拟结束时间--必须用单引号括起来“”
#> [5] "\"\""                                                                                                                         
#> [6] "\"! ***********************************************************************************************************************\""

请注意,午夜POSIXct时间的默认打印方法忽略了时间部分。如果要打印,请在指定时间调用
格式
,例如
格式(开始,“%F%T”)

我认为不建议在自定义函数中使用全局环境变量。。。你不能添加相应的参数吗?如果它们是从另一个函数调用传递过来的,那么您应该对一个函数使用
..
我猜调用
mapply
,而不提供任何参数来迭代,只返回
list()
,这可能不是您想要的。@DamianoFantini如果这些变量也在其他函数中使用会怎么样?我是否将计算添加到每个函数中?有没有其他方法来组织这件事?计算时间不长。但是这些变量正在3-4个函数中使用。@alistaire你能解释一下我应该怎么做吗。我不明白。@DamianoFantini你是说即使变量已经在全球范围内可用,也要将变量作为参数传递吗?谢谢!太棒了。一个简短的问题。我如何将日期插入为“2016-11-30 23:00:00”,即使用逗号。例如,这是我应该在startTime数据帧内执行的操作吗?或者我可以用格式做些什么?你是说单引号<代码>粘贴0(“”,开始(“”)傻我,非常感谢!
runStart <- lubridate::ymd_hm(startDate) #Start date 
stimearray <- runStart + months(0:(nMonths-1)) 
etimearray <- runStart + months(1:nMonths) - lubridate::dhours(1)
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")
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"))