Skip to content

Commit

Permalink
only allow global(<symbol>)
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Oct 28, 2015
1 parent b1ce96e commit 4367328
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
5 changes: 4 additions & 1 deletion inst/include/dplyr/Result/GroupedCallProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ namespace dplyr {

if( TYPEOF(obj) == LANGSXP && CAR(obj) == Rf_install("global") ){
SEXP symb = CADR(obj) ;
if( TYPEOF(symb) != SYMSXP ) stop( "global only handles symbols" ) ;
SEXP res = env.find(CHAR(PRINTNAME(symb))) ;
call = res ;
return ;
Expand All @@ -107,8 +108,10 @@ namespace dplyr {

switch( TYPEOF( head ) ){
case LANGSXP:
if( CAR(head) == Rf_install("global") && TYPEOF(CADR(head)) == SYMSXP ){
if( CAR(head) == Rf_install("global") ){
SEXP symb = CADR(head) ;
if( TYPEOF(symb) != SYMSXP ) stop( "global only handles symbols" ) ;

SEXP res = env.find( CHAR(PRINTNAME(symb)) ) ;

SETCAR(obj, res) ;
Expand Down
4 changes: 3 additions & 1 deletion src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace dplyr{

if( TYPEOF(obj) == LANGSXP && CAR(obj) == Rf_install("global") ){
SEXP symb = CADR(obj) ;
if( TYPEOF(symb) != SYMSXP ) stop( "global only handles symbols" ) ;
SEXP res = env.find(CHAR(PRINTNAME(symb))) ;
call = res ;
return ;
Expand All @@ -85,8 +86,9 @@ namespace dplyr{
SEXP head = CAR(obj) ;
switch( TYPEOF( head ) ){
case LANGSXP:
if( CAR(head) == Rf_install("global") && TYPEOF(CADR(head)) == SYMSXP ){
if( CAR(head) == Rf_install("global") ){
SEXP symb = CADR(head) ;
if( TYPEOF(symb) != SYMSXP ) stop( "global only handles symbols" ) ;
SEXP res = env.find( CHAR(PRINTNAME(symb)) ) ;

SETCAR(obj, res) ;
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-mutate.r
Original file line number Diff line number Diff line change
Expand Up @@ -443,15 +443,23 @@ test_that("mutate recognizes global #1469", {
vs <- 4
res <- mtcars %>% mutate(a = global(vs))
expect_true( all(res$a == 4) )
expect_error( mtcars %>% mutate(global("vs")), "global only handles symbols" )
res <- mtcars %>% mutate(a = global(vs) + 1)
expect_true( all(res$a == 5) )
expect_error( mtcars %>% mutate(global("vs") + 1), "global only handles symbols" )
res <- mtcars %>% mutate(a = 1+global(vs) )
expect_true( all(res$a == 5) )
expect_error( mtcars %>% mutate(1 + global("vs")), "global only handles symbols" )

res <- mtcars %>% group_by(cyl) %>% mutate(a = global(vs))
expect_true( all(res$a == 4) )
expect_error( mtcars %>% group_by(cyl) %>% mutate(a = global("vs")), "global only handles symbols" )
res <- mtcars %>% group_by(cyl) %>% mutate(a = global(vs)+1)
expect_true( all(res$a == 5) )
expect_error( mtcars %>% group_by(cyl) %>% mutate(a = global("vs") + 1), "global only handles symbols" )

res <- mtcars %>% group_by(cyl) %>% mutate(a = 1+global(vs))
expect_true( all(res$a == 5) )
expect_error( mtcars %>% group_by(cyl) %>% mutate(a = 1 + global("vs")), "global only handles symbols" )

})

0 comments on commit 4367328

Please sign in to comment.