【问题标题】:How can I set a conditional function argument value here?如何在这里设置条件函数参数值?
【发布时间】:2018-05-23 22:20:29
【问题描述】:

我想根据另一个参数的值自动设置函数中参数的值。

更具体地说,我想设置一个时区值 (offset) 以根据 region 自动调整时间值。

但是,我的实现似乎不起作用(因为偏移量永远不会应用,除非我专门将它作为参数传递给函数)。

部分功能(应该)根据region的值设置offset的值,并连接到对应的Elasticsearch服务器。

这就是我所拥有的:

if (region == "EU") {
    offset = "+00:00:00"
    # Code to connect to EU ElasticSearch server
  } else if (region == "US") {
    offset = "-06:00:00"
    # Code to connect to US ElasticSearch server
  } else {
  paste0(stop("Incorrect region supplied: ", region))
}

功能:

time_function <- function(region, retailer, start_date, end_date, offset = "+00:00:00"){
    # Function body
}

(注意我已经将offset的默认值设置为"+00",否则会抛出参数丢失的错误。)

显然我在某个地方出错了,因为除非我在参数列表中明确指定,否则永远不会应用偏移量。

这就是我想做的:

如果region == "US",则将offset设置为"-06:00:00", 否则,如果 region == "EU",则将 offset 设置为 "+00:00:00" 否则Error message: "supply valid region"

简而言之,我希望设置一个条件参数值。

我怎样才能做到这一点?

【问题讨论】:

  • 这与这个问题有关吗? stackoverflow.com/questions/25884011/…
  • 只是一个想法:(据我所知)您不能在 data.frame. 的单个列中拥有多个时区。你能提供一些数据/向量来帮助我们吗?
  • @Nova 没错——数据没有混合在一起,而是分别存在于不同的 Elasticsearch 后端系统中。这个函数背后的想法是,我可以从区域数据库(欧盟或美国)中提取数据,然后对其应用时区偏移量,以根据所需的时区移动显示的数据。从某种意义上说,它对所显示的时区造成了一种错觉——然而,它是正确的。
  • 你能给我们一些小的数据框吗?

标签: r function conditional


【解决方案1】:

原来我疏忽了分配执行的顺序,现在才发现。

对于那些可能遇到类似问题的人,可能值得强调我出错的地方:我在使用 offset 值后设置它,这意味着它已被使用/处理(使用上一次迭代的值),然后它被赋予了新值。

简而言之,我的实现完全正确,但事件顺序不正确

因此,请确保在处理所需的值之前设置它们。

【讨论】:

    【解决方案2】:

    您的代码有效。

    > time_function <- function(region){
    +   # Function body
    +   if (region == "EU") {
    +     offset = "+00:00:00"
    +     # Code to connect to EU ElasticSearch server
    +   } else if (region == "US") {
    +     offset = "-06:00:00"
    +     # Code to connect to US ElasticSearch server
    +   } else {
    +     stop(paste0("Incorrect region supplied: ", region))
    +   }
    +   
    +   return(offset)
    + }
    > 
    > time_function("EU")
    [1] "+00:00:00"
    > time_function("US")
    [1] "-06:00:00"
    > time_function("CH")
    Error in time_function("CH") : Incorrect region supplied: CH
    

    要优化您的代码,您可以使用开关。

    > time_function <- function(region){
    +   # Function body
    +   offset <- switch(region,
    +       EU = "+00:00:00",
    +       US = "-06:00:00",
    +       stop(paste0("Incorrect region supplied: ", region)))
    + 
    +   return(offset)
    + }
    > 
    > time_function("EU")
    [1] "+00:00:00"
    > time_function("US")
    [1] "-06:00:00"
    > time_function("CH")
    Error in time_function("CH") : Incorrect region supplied: CH
    

    有两个参数:

    > time_function <- function(region){
    +   # Function body
    +   list2env(switch(region,
    +          EU = list(offset = "+00:00:00", con = "EU_con"),
    +          US = list(offset = "+00:00:00", con = "US_con"),
    +          stop(paste0("Incorrect region supplied: ", region))), envir = environment())
    +   
    +   return(c(offset, con))
    + }
    > 
    > time_function("EU")
    [1] "+00:00:00" "EU_con"   
    > time_function("US")
    [1] "+00:00:00" "US_con"   
    > time_function("CH")
    Error in list2env(switch(region, EU = list(offset = "+00:00:00", con = "EU_con"),  : 
      Incorrect region supplied: CH
    

    【讨论】:

    • 谢谢您,但是否可以根据条件执行多个任务?我不仅需要设置offset的值,还要连接对应的Elasticsearch服务器。
    猜你喜欢
    • 2014-11-11
    • 2022-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 2013-01-01
    • 1970-01-01
    • 2019-01-18
    • 2010-10-22
    相关资源
    最近更新 更多