Skip to content

Commit

Permalink
give hybrid meaning to group_indices(), as in #1185
Browse files Browse the repository at this point in the history
```r
> df <- data_frame(v1 = c(NA, NA, 2, 2, 3), v2 = c(NA, NA, 3,3, 4)) %>% group_by(v1)
> df %>% mutate( g = group_indices() )
# A tibble: 5 x 3
# Groups:   v1 [3]
     v1    v2     g
  <dbl> <dbl> <int>
1   NA    NA      3
2   NA    NA      3
3    2.    3.     1
4    2.    3.     1
5    3.    4.     2
```
  • Loading branch information
romainfrancois committed May 3, 2018
1 parent a66e962 commit bb3fa9d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions inst/include/dplyr/HybridHandlerMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void install_window_handlers(HybridHandlerMap& handlers);
void install_offset_handlers(HybridHandlerMap& handlers);
void install_in_handlers(HybridHandlerMap& handlers);
void install_debug_handlers(HybridHandlerMap& handlers);
void install_group_handlers(HybridHandlerMap& handlers);

bool hybridable(RObject arg);

Expand Down
3 changes: 2 additions & 1 deletion src/hybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ HybridHandlerMap& get_handlers() {
install_offset_handlers(handlers);
install_in_handlers(handlers);
install_debug_handlers(handlers);
install_group_handlers(handlers);
}
return handlers;
}
Expand Down Expand Up @@ -228,7 +229,7 @@ Result* get_handler(SEXP call, const ILazySubsets& subsets, const Environment& e

if (TYPEOF(call) == LANGSXP) {
int depth = Rf_length(call);

HybridHandlerMap& handlers = get_handlers();

bool in_dplyr_namespace = false;
Expand Down
46 changes: 46 additions & 0 deletions src/hybrid_group.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "pch.h"
#include <dplyr/main.h>

#include <dplyr/HybridHandlerMap.h>

#include <dplyr/Result/ILazySubsets.h>

#include <dplyr/Result/Result.h>

using namespace Rcpp;
using namespace dplyr;


class GroupHybrid : public Result {
public:
SEXP process(const RowwiseDataFrame&) {
Rprintf( "GroupHybrid::RowwiseDataFrame()\n" ) ;
return R_NilValue ;
}

SEXP process(const GroupedDataFrame&) {
Rprintf( "GroupHybrid::GroupedDataFrame()\n" ) ;
return R_NilValue ;
}

SEXP process(const FullDataFrame&) {
Rprintf( "GroupHybrid::FullDataFrame()\n" ) ;
return R_NilValue ;
}

SEXP process(const SlicingIndex& i) {
return IntegerVector(i.size(), i.group() + 1);
}
};

Result* group_prototype(SEXP call, const ILazySubsets&, int nargs) {
if (nargs != 0)
return 0;

return new GroupHybrid();
}

void install_group_handlers(HybridHandlerMap& handlers) {
Environment ns_dplyr = Environment::namespace_env("dplyr");
handlers[ Rf_install("group_indices") ] = HybridHandler(group_prototype, ns_dplyr["group_indices"]);
}

0 comments on commit bb3fa9d

Please sign in to comment.