From 4b2ee33835e049a5af86405776eebdaad4feb9b8 Mon Sep 17 00:00:00 2001 From: whtns Date: Thu, 15 Oct 2020 16:43:19 -0700 Subject: [PATCH] add tidyverse helper --- DESCRIPTION | 21 +++++++++++--------- NAMESPACE | 1 + R/tidy_helpers.R | 41 +++++++++++++++++++++++++++++++++++++++ R/twitter_funcs.R | 8 ++++---- man/fav_by_author.Rd | 8 ++++++-- man/transmute_pairwise.Rd | 26 +++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 R/tidy_helpers.R create mode 100644 man/transmute_pairwise.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 3fffefb..36b6ac5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,16 +1,19 @@ Package: ksmisc -Type: Package -Title: What the Package Does (Title Case) -Version: 0.1.0 -Author: Who wrote it -Maintainer: The package maintainer -Description: More about what it does (maybe more than one line) - Use four spaces when indenting paragraphs within the Description. -License: What license is it under? +Title: Utility Function for Kevin Stachelek +Version: 0.0.0.9000 +Authors@R: + person(given = "Kevin", + family = "Stachelek", + role = c("aut", "cre"), + email = "kstachelek@protonmail.com", + comment = c(ORCID = "0000-0003-2085-695X")) +Description: What the package does (one paragraph). +License: MIT + file LICENSE Encoding: UTF-8 LazyData: true -RoxygenNote: 7.0.2 +Roxygen: list(markdown = TRUE) Imports: magrittr, dplyr, rtweet +RoxygenNote: 7.1.0 diff --git a/NAMESPACE b/NAMESPACE index d75f824..e8607c1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1 +1,2 @@ exportPattern("^[[:alpha:]]+") +importFrom(magrittr,"%>%") diff --git a/R/tidy_helpers.R b/R/tidy_helpers.R new file mode 100644 index 0000000..eaec5c4 --- /dev/null +++ b/R/tidy_helpers.R @@ -0,0 +1,41 @@ +#' Transmute Pairwise +#' +#' return a dataframe consisting of all combinations of arbitrary pairwise operations across a selection of columns +#' +#' @param df +#' @param fun +#' @param ... +#' @param associative +#' +#' @return +#' @export +#' +#' @examples mpg %>% +#' select(where(is.numeric)) %>% +#' mutate(select(cur_data(), c(displ, cty, hwy)) %>% +#' transmute_pairwise(`/`, associative = FALSE) %>% +#' rename_with(~paste0(.x, "_ratio")) %>% +#' identity() +#' ) +transmute_pairwise <- function(df, fun, ..., associative = TRUE){ + + transmute2 <- function(df, .x, .y, fun, ...){ + + dplyr::transmute(df, "{{ .x }}_{{ .y }}" := fun({{ .x }}, {{ .y }}, ...)) + } + + var_pairs <- t(combn(names(df), 2)) %>% + tibble::as_tibble() %>% + setNames(c(".x", ".y")) + + if(!associative){ + var_pairs <- dplyr::bind_rows( + var_pairs, + rename(var_pairs, .y = .x, .x = .y) + ) + } + + dplyr::mutate(var_pairs, dplyr::across(dplyr::everything(), syms)) %>% + purrr::pmap_dfc(transmute2, df = df, fun = fun, ...) +} + diff --git a/R/twitter_funcs.R b/R/twitter_funcs.R index 1cfaf8f..80b53a8 100644 --- a/R/twitter_funcs.R +++ b/R/twitter_funcs.R @@ -1,14 +1,14 @@ #' retrieve my twitter favorites from a given user #' -#' @param likes_user -#' @param twitter_user -#' @param ... +#' @param likes_user the user whose favorite we'll query +#' @param twitter_user the user whose tweets were favorited +#' @param ... additional arguments passed to `rtweet::get_favorites` #' #' @return #' @export #' #' @examples -fav_by_author <- function(likes_user = "stchlk", twitter_user, ...){ +fav_by_author <- function(twitter_user, likes_user = "stchlk", ...){ my_favs <- rtweet::get_favorites(likes_user, ...) %>% dplyr::filter(screen_name == twitter_user) %>% dplyr::select(text, status_url) diff --git a/man/fav_by_author.Rd b/man/fav_by_author.Rd index 24938ac..7c0897a 100644 --- a/man/fav_by_author.Rd +++ b/man/fav_by_author.Rd @@ -4,10 +4,14 @@ \alias{fav_by_author} \title{retrieve my twitter favorites from a given user} \usage{ -fav_by_author(likes_user = "stchlk", twitter_user, ...) +fav_by_author(twitter_user, likes_user = "stchlk", ...) } \arguments{ -\item{...}{} +\item{twitter_user}{the user whose tweets were favorited} + +\item{likes_user}{the user whose favorite we'll query} + +\item{...}{additional arguments passed to \code{rtweet::get_favorites}} } \value{ diff --git a/man/transmute_pairwise.Rd b/man/transmute_pairwise.Rd new file mode 100644 index 0000000..06932ec --- /dev/null +++ b/man/transmute_pairwise.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tidy_helpers.R +\name{transmute_pairwise} +\alias{transmute_pairwise} +\title{Transmute Pairwise} +\usage{ +transmute_pairwise(df, fun, ..., associative = TRUE) +} +\arguments{ +\item{associative}{} +} +\value{ + +} +\description{ +return a dataframe consisting of all combinations of arbitrary pairwise operations across a selection of columns +} +\examples{ +mpg \%>\% +select(where(is.numeric)) \%>\% + mutate(select(cur_data(), c(displ, cty, hwy)) \%>\% + transmute_pairwise(`/`, associative = FALSE) \%>\% + rename_with(~paste0(.x, "_ratio")) \%>\% + identity() + ) +}