Skip to content

Commit

Permalink
add tidyverse helper
Browse files Browse the repository at this point in the history
  • Loading branch information
whtns committed Oct 15, 2020
1 parent 8ce0b69 commit 4b2ee33
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 15 deletions.
21 changes: 12 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 <yourself@somewhere.net>
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
exportPattern("^[[:alpha:]]+")
importFrom(magrittr,"%>%")
41 changes: 41 additions & 0 deletions R/tidy_helpers.R
Original file line number Diff line number Diff line change
@@ -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, ...)
}

8 changes: 4 additions & 4 deletions R/twitter_funcs.R
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 6 additions & 2 deletions man/fav_by_author.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions man/transmute_pairwise.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4b2ee33

Please sign in to comment.