如何简化在R中粘贴字符串的if语句列表

brjng4g3  于 2023-07-31  发布在  其他
关注(0)|答案(2)|浏览(89)

我正在尝试编写一个与YouTube Analytics API交互的函数。有11个可能使用的参数,其中4个是必需的。我写了几个if语句来检查startDateendDatemetrics是否为null,但在此之后,我有一个if语句列表,说明如果其他参数之一不为null,则将其附加到baseUrl,如下所示。

# Arguments
currency = NULL
dimensions = NULL
endDate = NULL,
filters = NULL
ids = "channel==MINE"
includeHistoricalChannelData = NULL
maxResults = NULL
metrics = NULL
sort = NULL,
startDate = NULL
startIndex = NULL

# Check that start date, end date, and metrics aren't null
if(is.null(startDate) | is.null(endDate)) {
  stop("Please provide start/end dates")
}
if(is.null(metrics)) {
  stop("Please provide valid metrics")
}

baseUrl <- paste0("https://youtubeanalytics.googleapis.com/v2/reports?",
                    "&ids=", ids
                    "&startDate=", startDate,
                    "&endDate=", endDate)

 # Append additional arguments to baseUrl
  if (!is.null(dimensions)){ baseUrl <- paste0(baseUrl, "&dimensions=", dimensions)}
  if (!is.null(metrics)){    baseUrl <- paste0(baseUrl, "&metrics=", metrics)}
  if (!is.null(sort)){       baseUrl <- paste0(baseUrl, "&sort=", sort)}
  if (!is.null(maxResults)){ baseUrl <- paste0(baseUrl, "&maxResults=", maxResults)}
  if (!is.null(filters)){    baseUrl <- paste0(baseUrl, "&filters=", filters)}

字符串
这个方法可以工作,但是写起来很长。我想知道是否有更简单的方法来编写这个if语句列表?

6l7fqoea

6l7fqoea1#

我发现最简单的方法是将所有变量放入一个列表中,然后过滤掉NULL值,如下所示:

myList <- list(currency = NULL,
               dimensions = NULL,
               endDate = NULL,
               filters = NULL,
               ids = "channel==MINE",
               includeHistoricalChannelData = NULL,
               maxResults = NULL,
               metrics = NULL,
               sort = NULL,
               startDate = NULL,
               startIndex = NULL)

newList <- Filter(Negate(is.null), myList)

字符串
然后我可以将所有值粘贴在一起,执行以下操作:

params <- paste0("&", names(newList), "=", unlist(newList), collapse = "")

url <- paste0("https://youtubeanalytics.googleapis.com/v2/reports?", params)

pw136qt2

pw136qt22#

如果你想摆脱许多if语句,你可以创建一个类,并为这个特定的类创建一个将被调用的方法。虽然我在上面的评论中已经说过,但我更愿意保持你的代码现在的样子。

# define the class. 
my_class <- function(x)  structure(x, class = "my_class")

# Define `+` method for this class
"+.my_class" <- function(e1, e2){
  if(is.null(e2)) return(e1)
  my_class(paste0(e1, '&', substitute(e2), '=', e2))
}


# Arguments
currency = NULL
dimensions = NULL
endDate = as.character(today() + 1)
filters = NULL
ids = "channel==MINE"
includeHistoricalChannelData = NULL
maxResults = NULL
metrics = 'accuracy'
sort = NULL
startDate = as.character(today())
startIndex = NULL

# Check that start date, end date, and metrics aren't null
if(is.null(startDate) | is.null(endDate)) {
  stop("Please provide start/end dates")
}
if(is.null(metrics)) {
  stop("Please provide valid metrics")
}

字符串

现在拨打:

c(my_class("https://youtubeanalytics.googleapis.com/v2/reports?") +
  ids + startDate + endDate +  dimensions + metrics + sort +
  maxResults + filters)

[1] "https://youtubeanalytics.googleapis.com/v2/reports?&ids=channel==MINE&startDate=2023-07-07&endDate=2023-07-08&metrics=accuracy"

相关问题