[英]wrapper function; function that returns function
I am not sure how to write a wrapper function for something that includes magrittr pipe (%>%). 我不确定如何为包含magrittr管道(%>%)的东西编写包装函数。 I am trying to create a wrapper function that returns
addPolyline()
, addPolygon()
or addCircleMarkers()
depending on the layer type but I have not been successful. 我正在尝试创建一个包装函数,该函数根据图层类型返回
addPolyline()
, addPolygon()
或addCircleMarkers()
,但是我没有成功。 It seems the answer will be in Hadley's Advanced material but most likely it went over my head. 答案似乎将在Hadley的Advanced材料中,但最有可能超过了我。 As a simple example, how can I make something like the following example work:
作为一个简单的示例,如何使以下示例工作:
library(leaflet)
wrapper <- function() {
myfunc <- function() {addPolylines(data = leaflet::atlStorms2005)}
return(myfunc)
}
# I want this
leaflet() %>% addTiles() %>% addPolylines(data = leaflet::atlStorms2005)
# but I need a wrapper function
leaflet() %>% addTiles() %>% wrapper()
It seems that you need to modify your wrapper function a little and use %>% wrapper()()
in the pipe. 似乎您需要稍微修改包装器功能,并在管道中使用
%>% wrapper()()
。
library(leaflet)
wrapper <- function() {
myfunc <- function(.) {addPolylines(., data = leaflet::atlStorms2005)}
return(myfunc)
}
# I want this
leaflet() %>% addTiles() %>% addPolylines(data = leaflet::atlStorms2005)
# but I need a wrapper function
leaflet() %>% addTiles() %>% wrapper()()
It doesn't seem like you need a function that returns a function. 似乎不需要返回一个函数的函数。 You just need a function that can call other functions.
您只需要一个可以调用其他函数的函数。
wrapper <- function(x) {
x %>% addPolylines(data = leaflet::atlStorms2005)
}
leaflet() %>% addTiles() %>% wrapper()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.