如何在此处设置条件函数参数值?

时间:2018-05-23 12:44:54

标签: r function conditional

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

更具体地说,我想设置一个时区值(offset)来自动调整给定region的时间值。

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

部分函数(应该)根据offset的值设置region值,并连接到相应的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"

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

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

您的代码有效。

> 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

答案 1 :(得分:0)

事实证明,我按照作业实施的顺序进行了疏忽,现在才发现它。

对于那些可能遇到类似问题的人,可能值得强调我出错的地方:我在使用它之后设置了<div> <div class="form-group"> <div> <label for="debugLevel" id="debugLevellabel"> Debug Log Level </label> <select class="custom-select" style="width:30ch" name="debugLevel" id="debugLevel" aria-describedby="debugLevelhelp"> <option value="0"> Severe </option> <option selected="selected" value="1"> Warning </option> <option value="2"> Info </option> <option value="3"> Config </option> <option value="4"> Fine </option> <option value="5"> Finer </option> <option value="6"> Finest </option> </select> </div> <p id="debugLevelhelp" class="form-text text-muted"> Sets how much logging is written to the debug log, increasing the log level can help Jthink identify the cause of any problems encountered in SongKong </p> </div> <div class="form-group"> <div> <label for="ioDebugLevel" id="ioDebugLevellabel"> Debug IO Log Level </label> <select class="custom-select" style="width:30ch" name="ioDebugLevel" id="ioDebugLevel" aria-describedby="ioDebugLevelhelp"> <option value="0"> Severe </option> <option selected="selected" value="1"> Warning </option> <option value="2"> Info </option> <option value="3"> Config </option> <option value="4"> Fine </option> <option value="5"> Finer </option> <option value="6"> Finest </option> </select> </div> <p id="ioDebugLevelhelp" class="form-text text-muted"> Sets how much logging is written to the debug log when SongKong reads files or saves changes to files, increasing the log level can help Jthink identify the cause of any problems encountered in SongKong </p> </div> </div> 值,这意味着它被使用/处理(使用前一个迭代的值)然后为它分配了新值。

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

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