data.frame__to__grouped_df: create S3 method for class 'grouped_df'

Description Usage Arguments Details Value How to use

View source: R/data.frame__to__grouped_df.R

Description

Given an S3 method for class data.frame, create an S3 method for class grouped_df for the same generic function.

Usage

1

Arguments

fun

a function; this must be an S3 method for class data.frame; also, its first argument must be named x

Details

This is a utility function for writing packages. Functions that modify data frames by replacing or adding columns may fail on grouped data frames so the class will require a dedicated method. This is a generalized constructor of such methods.

In all cases the original call to foo is captured with match.call and reconstructed into a call to the data.frame method. Grouping variables are isolated as a list, and the first argument of foo, which must be named x, is split into a list with split, using the list of grouping variables as splitting factors. Empty levels are dropped. The list is then passed to lapply, with the call to the data.frame method as FUN. The result is reconstituted into a single data.frame with unsplit, again dropping empty levels.

Value

a function: a method for class grouped_df (from package dplyr)

How to use

When creating an .R file for generic foo and its S3 methods, simply add another entry where foo.grouped_df is defined as the result of data.frame__to__grouped_df acting on foo.data.frame.

Copy the following roxygen tag before the function definition to have it show up nicely in the documentation file:

@describeIn foo see \code{\link[metamethods]{data.frame__to__grouped_df}}


olobiolo/metamethods documentation built on Oct. 6, 2021, 2:08 p.m.