Changing function scope in GNU R example
来源:互联网 发布:instagram安卓翻墙软件 编辑:程序博客网 时间:2024/05/16 18:21
In my last post I have discussed how to work around GNU R scoping rules using environment function. This time let us look at a practical example using recode function from car package.
First let us look at how recode works:
transforms 1 2 3 1 2 3 into "a" "2" "3" "a" "2" "3". In further codes we will want to replicate this result using several different approaches sticking to the same definition of variable x.
Interestingly the string recodes is split and evaluated inside recode function so we can use the following code to get the same result:
a <- 1
Now we can change b variable to get different recoding results without the change of recode call. Note that this use of recode is not following its help page as documentation not support using variables inside recodes string.
Let us now try writing a simple wrapper around recode that does the same stuff. Unfortunately:
wrong.recode.one <- function(v, from, to) {
Here are two ways to work around it:
recode.one <- function(v, from, to) {
The first function recode.one moves recode into its own lexical scope. Unfortunately it also has to move squeezeBlanks function into its scope to work properly as recode calls it. To avoid this recode.one2 is put into environment(recode) environment so squeezeBlanks will be in its lexical scope.
Those examples helped me better understand how scoping works in GNU R, but they are dangerous. To see this one can look at the following code:
a <- 1
First let us look at how recode works:
library(car)
x <- rep(1:3, 2)
recode(x, "1='a'")transforms 1 2 3 1 2 3 into "a" "2" "3" "a" "2" "3". In further codes we will want to replicate this result using several different approaches sticking to the same definition of variable x.
Interestingly the string recodes is split and evaluated inside recode function so we can use the following code to get the same result:
a <- 1
b <- "a"
recode(x, "a[1]=b[1]")Now we can change b variable to get different recoding results without the change of recode call. Note that this use of recode is not following its help page as documentation not support using variables inside recodes string.
Let us now try writing a simple wrapper around recode that does the same stuff. Unfortunately:
wrong.recode.one <- function(v, from, to) {
recode(v, "from[1]=to[1]")
}
wrong.recode.one(x, 1, "a")
does not work and produces error. Due to lexical used in GNU R from and to variables are not within recode function scope.Here are two ways to work around it:
recode.one <- function(v, from, to) {
environment(recode) <- environment()
squeezeBlanks <- car:::squeezeBlanks
recode(v, "from[1]=to[1]")
}
recode.one(x, 1, "a")
recode.one2 <- function(v, from, to) {
environment(recode) <- environment()
recode(v, "from[1]=to[1]")
}
environment(recode.one2) <- environment(recode)
recode.one2(x, 1, "a")
The first function recode.one moves recode into its own lexical scope. Unfortunately it also has to move squeezeBlanks function into its scope to work properly as recode calls it. To avoid this recode.one2 is put into environment(recode) environment so squeezeBlanks will be in its lexical scope.
Those examples helped me better understand how scoping works in GNU R, but they are dangerous. To see this one can look at the following code:
a <- 1
is.fac <- "a"
recode(x,"a[1]=is.fac[1]")
Such call produces incorrect result 0 2 3 0 2 3 because is.fac is defined inside recode function so its evaluation is done inside recode body.
Another lesson that one has to be careful when hacking in GNU R.
Another lesson that one has to be careful when hacking in GNU R.
0 0
- Changing function scope in GNU R example
- Lexical scope and function closures in R
- Example: Create function in Postgresql
- Drawing function in R
- tapply() Function in R
- Emulating dynamic scoping in GNU R
- WVPlots: example plots in R using ggplot2
- An interesting example of name lookup in class scope
- A simple example of exceptional handing in function
- C++ 11: lexical closure in lambda function, example 1
- Code::Blocks 12.11 error: ‘nullptr’ was not declared in this scope&GNU GCC -std=gnu++0x
- Spring Bean Scope Example using @Scope Annotation
- oracle function example
- C callback function example
- Changing Hostnames in Oracle RAC
- Changing Options in U-boot
- Changing the Favicon in PeopleSoft
- Changing caption width in latex
- Linux 发送邮件
- 读取配置applicationContext.xml 的5种方法
- U盘存储
- opencv3.2中SurfFeatureDetector、SurfDescriptorExtractor、BruteForceMatcher用法
- 解决找不到javax.servlet或Server Runtime没有选项
- Changing function scope in GNU R example
- VC2008中处理CStatic控件的单击STN_CLICKED消息
- Android通过Http连接MySQL 实现登陆/注册(数据库+服务器+客户端),android+mysql
- MTK lk源码解析9( lk 阶段源码解析总结)
- 弱类型变量原理探究
- Nginx slab的实现 --- 第一篇“基本布局”
- Java分词工具HanLP
- mysql修改数据库表和表中的字段的编码格式的修改
- linux内核中tcp连接的断开处理