sapply()如何应用具有undefined(没有默认值)参数的函数

时间:2021-01-13 10:57:10

The following code block taken from chernan's sample REST queries will apply FUN to a list of parameters, but "param_name" is not provided. How is this possible?

从chernan的示例REST查询中获取的以下代码块将FUN应用于参数列表,但未提供“param_name”。这怎么可能?

rcurl_request <- function(service_url, parameters) {

# Collapse all parameters into one string
all_parameters <- paste(
    sapply(names(parameters), 
           FUN=function(param_name, parameters) {
               paste(param_name, paste(parameters[[param_name]], collapse=','), collapse='', sep='=')
           }, 
           parameters),
    collapse="&")

# Paste base URL and parameters
requested_url <- paste0(service_url, all_parameters)

# Encode URL (in case there would be any space character for instance)
requested_url <- URLencode(requested_url)

# Start request to service
response <- getURL(requested_url, .opts = list(ssl.verifypeer = FALSE))

return(response)
}

2 个解决方案

#1


4  

The *apply family of functions are designed to apply FUN to the elements of the object supplied to the respective *apply function.

* apply系列函数用于将FUN应用于提供给相应* apply函数的对象元素。

In your example, the elements to which FUN is applied are the individual elements of names(parameters). sapply() takes the first element, names(parameters)[1] and passes that to FUN as the first argument. Hence param_name is used to refer to names(parameters)[1], then to names(parameters)[2], and so on.

在您的示例中,应用FUN的元素是名称(参数)的各个元素。 sapply()接受第一个元素,名称(参数)[1]并将其作为第一个参数传递给FUN。因此,param_name用于表示名称(参数)[1],然后用于表示名称(参数)[2],依此类推。

In other words, sapply() arranges to pass, in turn, the elements of sapply()s first argument to FUN supplying these elements as the first argument of FUN.

换句话说,sapply()安排将sapply()的第一个参数的元素依次传递给FUN,将这些元素作为FUN的第一个参数提供。

You can see this more clearly with this simpler example:

您可以通过这个更简单的示例更清楚地看到这一点:

sapply(1:10, FUN = function(i) {writeLines(paste("working on", i)); i})

hence i takes on the values 1, 2, ..., 10, in turn and the anonymous function works on each of them:

因此我依次取值1,2,...,10,并且匿名函数适用于每一个:

> sapply(1:10, FUN = function(i) {writeLines(paste("working on", i)); i})
working on 1
working on 2
working on 3
working on 4
working on 5
working on 6
working on 7
working on 8
working on 9
working on 10
 [1]  1  2  3  4  5  6  7  8  9 10

#2


0  

names(parameters) is being passed to param_name. A more basic example: sapply(df1, mean). The implication being sapply(df1, function(x) mean(x)). In this case each element of df1 is being passed to mean as the argument x. The apply functions take the first argument and pass it to the specified function (the second argument of an apply function)

名称(参数)正在传递给param_name。一个更基本的例子:sapply(df1,mean)。暗示是sapply(df1,function(x)mean(x))。在这种情况下,df1的每个元素都被传递给作为参数x的意思。 apply函数接受第一个参数并将其传递给指定的函数(apply函数的第二个参数)

For example:

sapply(mtcars, mean)

       mpg        cyl       disp         hp       drat         wt       qsec 
 20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750 
        vs         am       gear       carb 
  0.437500   0.406250   3.687500   2.812500 

Also, the sapply() is constructed as sapply(x, FUN,...) the ... is any additional arguments passed to the function FUN. In your example, parametersis being passed as the second argument (names(parameters) is implicitly the first).

此外,sapply()构造为sapply(x,FUN,...)......是传递给函数FUN的任何其他参数。在您的示例中,参数作为第二个参数传递(名称(参数)隐式地是第一个参数)。

#1


4  

The *apply family of functions are designed to apply FUN to the elements of the object supplied to the respective *apply function.

* apply系列函数用于将FUN应用于提供给相应* apply函数的对象元素。

In your example, the elements to which FUN is applied are the individual elements of names(parameters). sapply() takes the first element, names(parameters)[1] and passes that to FUN as the first argument. Hence param_name is used to refer to names(parameters)[1], then to names(parameters)[2], and so on.

在您的示例中,应用FUN的元素是名称(参数)的各个元素。 sapply()接受第一个元素,名称(参数)[1]并将其作为第一个参数传递给FUN。因此,param_name用于表示名称(参数)[1],然后用于表示名称(参数)[2],依此类推。

In other words, sapply() arranges to pass, in turn, the elements of sapply()s first argument to FUN supplying these elements as the first argument of FUN.

换句话说,sapply()安排将sapply()的第一个参数的元素依次传递给FUN,将这些元素作为FUN的第一个参数提供。

You can see this more clearly with this simpler example:

您可以通过这个更简单的示例更清楚地看到这一点:

sapply(1:10, FUN = function(i) {writeLines(paste("working on", i)); i})

hence i takes on the values 1, 2, ..., 10, in turn and the anonymous function works on each of them:

因此我依次取值1,2,...,10,并且匿名函数适用于每一个:

> sapply(1:10, FUN = function(i) {writeLines(paste("working on", i)); i})
working on 1
working on 2
working on 3
working on 4
working on 5
working on 6
working on 7
working on 8
working on 9
working on 10
 [1]  1  2  3  4  5  6  7  8  9 10

#2


0  

names(parameters) is being passed to param_name. A more basic example: sapply(df1, mean). The implication being sapply(df1, function(x) mean(x)). In this case each element of df1 is being passed to mean as the argument x. The apply functions take the first argument and pass it to the specified function (the second argument of an apply function)

名称(参数)正在传递给param_name。一个更基本的例子:sapply(df1,mean)。暗示是sapply(df1,function(x)mean(x))。在这种情况下,df1的每个元素都被传递给作为参数x的意思。 apply函数接受第一个参数并将其传递给指定的函数(apply函数的第二个参数)

For example:

sapply(mtcars, mean)

       mpg        cyl       disp         hp       drat         wt       qsec 
 20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750 
        vs         am       gear       carb 
  0.437500   0.406250   3.687500   2.812500 

Also, the sapply() is constructed as sapply(x, FUN,...) the ... is any additional arguments passed to the function FUN. In your example, parametersis being passed as the second argument (names(parameters) is implicitly the first).

此外,sapply()构造为sapply(x,FUN,...)......是传递给函数FUN的任何其他参数。在您的示例中,参数作为第二个参数传递(名称(参数)隐式地是第一个参数)。