HTML5   发布时间:2022-04-25  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Programming over R大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
(This article was first published on  R – Win-Vector Blog,and kindly contributed to  R-bloggers)

138
SHARES

R is a very fluid language amenable to meta-programming,or alterations of the language itself. This has allowed the late user-driven introduction of a number of powerful features such as magrittr pipesthe foreach systemfuturesdata.table,and dplyr. Please read on for some small Meta-progrAMMing effects we have been experimenTing with.

Programming over R

Meta-ProgrAMMing

Meta-progrAMMing is a powerful tool that allows one to re-shape a progrAMMing language or write programs that automate parts of working with a progrAMMing language.

Meta-progrAMMing itself has the central conTradiction that one hopes nobody else is doing Meta-progrAMMing,but that they are instead dutifully wriTing referentially transparent code that is safe to perform transformations over,so that one can safely introduce their own cLever Meta-progrAMMing. For example: one would hate to lose the ability to use a powerful package such as future because we already “used up all the referential transparency” for some minor notational effect or convenience.

That being said, R is an open system and it is fun to play with the notation. I have been experimenTing with different notations for progrAMMing over R for a while,and thought I would demonstrate a few of them here.

Let Blocks

We have been using let to code over non-standard evaluation (NSE) packages in R for a while Now. This allows code such as the following:

library("dplyr")
library("wrapr")

d <- data.frame(x = c(1,NA))

cname <- 'x'
rname <- paste(cname,'isNA',sep = '_')

let(list(COL = cname,RES = rName),d %>% mutate(RES = is.na(COL))
)

 #    x x_isNA
 # 1  1  falSE
 # 2 NA   TRUE

let is in fact quite hAndy notation that will work in a non-deprecated mAnner with both dplyr 0.5 and dplyr 0.6. it is how we are future-proofing our current dplyr workflows.

UnquoTing

dplyr 0.6 is introducing a new execution system (alternately called rlang or tidyeval,see here) which uses a notation more like the following (but fewer parenthesis,and with the ability to control left-hand side of an in-argument assignment):

beval(d %>% mutate(x_isNA = is.na((!!cName))))

The inability to re-map the right-hand side of the apparent assignment is because the “(!! )” notation doesn’t successfully masquerade as a lexical token valid on the left-hand side of assignments or function argument bindings.

and there was an R language proposal for a notation like the following (but without the quotes,and with some care to keep it syntactically disTinct from other uses of “@”):

ateval('d %>% mutate(@rname = is.na(@cName))')

beval and ateval are just curiosities implemented to try and get a taste of the new dplyr notation,and we don’t recommend using them in production — their ad-hoc demonstration implementations are just not powerful enough to supply a uniform interface. dplyritself seems to be replacing a lot of R‘s execution framework to achieve stronger effects.

Write Arrow

We are experimenTing with “write arrow” (a deliberate homophone of “right arrow”). It allows the convenient storing of a pipe result into a variable chosen by name.

library("dplyr")
library("replyr")

'x' -> whereToStoreResult

7 %>% sin %>% cos %->_% whereToStoreResult

print(X)
 ## [1] 0.7918362

Notice,the value “7” is stored in the variable “x” not in a variable named “whereToStoreResult”. “whereToStoreResult” was able to name where to store the value parametrically.

This allows code such as the following:

for(i in 1:3) { 
  i %->_% paste0('x',i)
}

(Please run the above to see the automatic creation of variables named “x1”,“x2”,and “x3”,storing values 1,2,and 3 respectively.)

We kNow left to right assignment is heterodox; but the notation is very slick if you are consistent with it,and add in some formatTing rules (such as insisTing on a line break after each pipe stagE).

Conclusion

One wants to use Meta-progrAMMing with care. In addition to bringing in desired convenience it can have unexpected effects and interactions deeper in a language or when exposed to other Meta-progrAMMing systems. This is one reason why a “seemingly harmless” proposal such as “user defined unary functions” or “at unquoting” takes so long to consider. This is also why new language features are best tried in small packages first (so users can easily chose to include them or not in their larger workflow) to drive public request for comments (RFC) processes or allow the ideas to evolve (and not be frozen at their first good idea,a great example of community accepted change being Haskel’s switch from request chaining IO to monadic IO; the first IO system “seemed inevitable” until it was completely replaced).

大佬总结

以上是大佬教程为你收集整理的Programming over R全部内容,希望文章能够帮你解决Programming over R所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:programmingr