【问题标题】:How can I create a function with null arguments?如何创建带有空参数的函数?
【发布时间】: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)

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

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

或者,还有其他方法吗?

数据

模板

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


【解决方案1】:

您可以使用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").

【讨论】:

  • 谢谢!棒极了。一个快速的问题。我将如何将日期插入为“2016-11-30 23:00:00”,即使用逗号。例如,这是我应该在 startTime 数据框中做的事情吗?或者我可以使用格式做点什么吗?
  • 你的意思是单引号? paste0("'", start, "'")
  • 傻我,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多