knitr::opts_chunk$set(echo = TRUE, comment = '.')
library(pmtables)
vars <- dplyr::vars

Motivation

I wanted a way to capture column names as a character vector along with a second set of names for renaming those data items. The primary need was a vector of names to use rather columns to select from a data set.

Solution

new_names class with methods for character and quosure. This is two methods and the generic.

pmtables:::new_names("a,b,c")
pmtables:::new_names(c("a", "b", "c"))
pmtables:::new_names(vars(a,b,c))
pmtables:::new_names(vars(A = a, B = b, C = c))

Also accepts rename as named list

re <- list(A = "a", B = "b")

pmtables:::new_names("A,B", table = re)

This is a little backward from the rename(new_name = old_name) syntax; but it is made to handle output from yspec, where things are already keyed by the existing column name. This could easily be modified to invert names / values during this lookup process.

Works with select

library(dplyr)
x <- pmtables:::new_names(vars(b = B, a = A))

x

tab <- tibble(A = 1, B = 2)


tab

select(tab,all_of(x))

Or rename

rename(tab, all_of(x))


metrumresearchgroup/pmtables documentation built on Oct. 27, 2024, 5:16 p.m.