Skip to content

Commit

Permalink
version 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwharris authored and cran-robot committed Mar 31, 2022
1 parent 4e61984 commit 4f3724d
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 19 deletions.
22 changes: 12 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
Package: speedycode
Type: Package
Title: Automate Code for Adding Labels, Renaming Variables, and
Converting ASCII Files
Version: 0.2.0
Author: Jacob Harris
Maintainer: Jacob Harris <jh2689@cornell.edu>
Description: Label, rename, and convert datasets and ASCII files more efficiently. 'speedycode' automates the code necessary for labeling variables with the 'labelled' package, renaming variables with 'dplyr' syntax, and converting ASCII files with the 'readroper' package. Most functions require only the name of the dataset and the code will be automatically written. Some convenience functions useful for converting ASCII files are also included.
Version: 0.3.0
Date: 2022-03-30
Title: Automate Code for Adding Labels, Recoding and Renaming
Variables, and Converting ASCII Files
Authors@R: person("Jacob", "Harris", role = c("aut", "cre"),
email = "jh2689@cornell.edu")
Author: Jacob Harris [aut, cre]
Suggests: labelled, readroper
Description: Label, recode, rename, and convert datasets and ASCII files more efficiently. 'speedycode' automates the code necessary for labeling variables with the 'labelled' package, recoding and renaming variables with 'dplyr' syntax, and converting ASCII files with the 'readroper' package. Most functions require only the name of the dataset and the code will be automatically written. Some convenience functions useful for converting ASCII files are also included.
License: GPL-3
Imports: dplyr, stringr, purrr
Suggests: labelled, readroper
Encoding: UTF-8
NeedsCompilation: no
Packaged: 2021-11-02 18:55:46 UTC; Jacob
Packaged: 2022-03-31 00:44:24 UTC; Jacob
Maintainer: Jacob Harris <jh2689@cornell.edu>
Repository: CRAN
Date/Publication: 2021-11-02 19:10:02 UTC
Date/Publication: 2022-03-31 06:40:05 UTC
11 changes: 6 additions & 5 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
b0b532a3e357b14061639ca343877b7a *DESCRIPTION
7a2e90953688a4fd39c4223957d73ed3 *DESCRIPTION
0b1727142e519c8fd70ea6b0de9aecc1 *NAMESPACE
0501b441a19f84c69f0a97b8a581c8f8 *R/speedycode.R
f67f4006daaf28fbbb03197e947b7200 *R/speedycode.R
c5d8c08d3a23dfcbf7d5311215ebaa70 *man/debug_ascii.Rd
04a28fc42f6914e493062e4b1857dc43 *man/speedy_classes.Rd
3bffd122ef0df120dc61268b920bf589 *man/speedy_labels.Rd
afa3ce618aecd5dbc1ebb992b20291ee *man/speedy_rename.Rd
29ea5ff8d05971b9de1d357f4ad41b05 *man/speedy_classes.Rd
668096b2594b72ed7893f273b141b1b7 *man/speedy_labels.Rd
118495538a70396cd49432074c1fada0 *man/speedy_mutate.Rd
98e5501f1ba55afb34ab606a99a3064d *man/speedy_rename.Rd
451e1078f88e5a3ffd5b32c2d7e99d59 *man/speedy_varnames.Rd
95 changes: 94 additions & 1 deletion R/speedycode.R
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,100 @@ speedy_labels <- function(data, nrows = 5, path = "") {
}
}

#### speedy_mutate ####
speedy_mutate <- function(data, var, var_classes = "sn", path = ""){
x <- data %>%
select(all_of(var)) %>%
distinct() %>%
arrange()

# BOTH old and new var are strings
if(var_classes == "ss") {
line1 <- paste("data"," <- ", "data", " %>%", sep = "")
line2a <- "mutate("
line2b <- paste('NEW_VAR', " = ", "case_when(", "\n", var, " == ", '"', x[1,1], '"', " ~ ", '"', "NEW_VAL", '"', ",", sep = "")
line2d <- character()
for(i in 2:(nrow(x)-1)) {
line2d <- c(line2d, paste(var, " == ", '"', x[i,1], '"', " ~ ", '"', "NEW_VAL", '"', ",", "\n", sep = ""))
# cat(line2d)
}
line2e <- paste(var, " == ", '"', x[nrow(x),1], '"', " ~ ", '"', "NEW_VAL", '"', ")", "\n", sep = "")
last_line <- paste(")", sep = "")

# This prints the code in the console
cat(line1, "\n", line2a, "\n", line2b, "\n", line2d, line2e, last_line)

# This writes the code to a separate R script
suppressWarnings(utils::capture.output(cat(line1, "\n", line2a, "\n", line2b, "\n", line2d, line2e, last_line), file = path))
}

else if(var_classes == "sn") {
line1 <- paste("data"," <- ", "data", " %>%", sep = "")
line2a <- "mutate("
line2b <- paste('NEW_VAR', " = ", "case_when(", "\n", var, " == ", '"', x[1,1], '"', " ~ ", "NEW_VAL", ",", sep = "")
line2d <- character()
for(i in 2:(nrow(x)-1)) {
line2d <- c(line2d, paste(var, " == ", '"', x[i,1], '"', " ~ ", "NEW_VAL", ",", "\n", sep = ""))
# cat(line2d)
}
line2e <- paste(var, " == ", '"', x[nrow(x),1], '"', " ~ ", "NEW_VAL", ")", "\n", sep = "")
last_line <- paste(")", sep = "")

# This prints the code in the console
cat(line1, "\n", line2a, "\n", line2b, "\n", line2d, line2e, last_line)

# This writes the code to a separate R script
suppressWarnings(utils::capture.output(cat(line1, "\n", line2a, "\n", line2b, "\n", line2d, line2e, last_line), file = path))
}

else if(var_classes == "nn") {
line1 <- paste("data"," <- ", "data", " %>%", sep = "")
line2a <- "mutate("
line2b <- paste('NEW_VAR', " = ", "case_when(", "\n", var, " == ", x[1,1], " ~ ", "NEW_VAL", ",", sep = "")
line2d <- character()
for(i in 2:(nrow(x)-1)) {
line2d <- c(line2d, paste(var, " == ", x[i,1], " ~ ", "NEW_VAL", ",", "\n", sep = ""))
# cat(line2d)
}
line2e <- paste(var, " == ", x[nrow(x),1], " ~ ", "NEW_VAL", ")", "\n", sep = "")
last_line <- paste(")", sep = "")

# This prints the code in the console
cat(line1, "\n", line2a, "\n", line2b, "\n", line2d, line2e, last_line)

# This writes the code to a separate R script
suppressWarnings(utils::capture.output(cat(line1, "\n", line2a, "\n", line2b, "\n", line2d, line2e, last_line), file = path))
}

else if(var_classes == "ns") {
line1 <- paste("data"," <- ", "data", " %>%", sep = "")
line2a <- "mutate("
line2b <- paste('NEW_VAR', " = ", "case_when(", "\n", var, " == ", x[1,1], " ~ ", '"', "NEW_VAL", '"', ",", sep = "")
line2d <- character()
for(i in 2:(nrow(x)-1)) {
line2d <- c(line2d, paste(var, " == ", x[i,1], " ~ ", '"', "NEW_VAL", '"', ",", "\n", sep = ""))
# cat(line2d)
}
line2e <- paste(var, " == ", x[nrow(x),1], " ~ ", '"', "NEW_VAL", '"', ")", "\n", sep = "")
last_line <- paste(")", sep = "")

# This prints the code in the console
cat(line1, "\n", line2a, "\n", line2b, "\n", line2d, line2e, last_line)

# This writes the code to a separate R script
suppressWarnings(utils::capture.output(cat(line1, "\n", line2a, "\n", line2b, "\n", line2d, line2e, last_line), file = path))
}

# This throws an error if the code is saved out to a file that is NOT an R script
final_path_char <- stringr::str_sub(path,-2,-1)
if(stringr::str_detect(path, ".") && final_path_char != ".R") {
stop('Must specify ".R" at the end of the path to save formatted code to an R script file')
}

oo <- options(crayon.enabled = FALSE)
on.exit(options(oo))
}

#### speedy_rename ####
speedy_rename <- function(data, path = "") {

Expand All @@ -369,7 +463,6 @@ speedy_rename <- function(data, path = "") {
stop('Must specify ".R" at the end of the path to save formatted code to an R script file')
}


# This writes the code to a separate R script
suppressWarnings(utils::capture.output(cat(line1, "\n", line2a, "\n", line2b, "\n", newcode, line4), file = path))
}
Expand Down
2 changes: 1 addition & 1 deletion man/speedy_classes.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ speedy_classes(data, path = "")
name of dataset
}
\item{path}{
If saving code to a separate file, specify the file path here. Leave blank if not saving code.
If saving code to a new R script file, specify the file path here. Leave blank if not saving code.
}
}

Expand Down
2 changes: 1 addition & 1 deletion man/speedy_labels.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ speedy_labels(data, nrows = 5, path = "")
Number of rows for value labels of each variable. The minimum number of rows allowed is 2 and the maximum is 10. Extra rows without values are set to NA.
}
\item{path}{
If saving code to a separate file, specify the file path here. Leave blank if not saving code.
If saving code to a new R script file, specify the file path here. Leave blank if not saving code.
}
}
\details{
Expand Down
45 changes: 45 additions & 0 deletions man/speedy_mutate.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
\name{speedy_mutate}
\alias{speedy_mutate}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{
Automate code for recoding variables with the 'mutate' and 'case_when' functions
}
\description{
`speedy_mutate` automates the code for quickly recoding variables with a large number of unique levels with `dplyr` syntax. The user only needs to supply the variable to recode and whether or not those variables should be quoted or not.
}
\usage{
speedy_mutate(data, var, var_classes = "sn", path = "")
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{data}{
Name of dataset
}

\item{var}{
String of the name of the variable being recoded
}

\item{var_classes}{
Specifies whether or not the current variable and the new variable being created should have quotes around them. There are four possible inputs ("ss", "sn", "nn", "ns"). "ss" means the current and new variable with both have quotes. "sn" means the first will have quotes and the second will not and so forth.
}

\item{path}{
If saving code to a new R script file, specify the file path here. Leave blank if not saving code.
}
}
\details{
`speedy_mutate` generates a formatted chunk of the code for creating a new variable using the `mutate` and `case_when` functions. The code may be copied and pasted from the console or saved out to a separate R script. This is useful when a new variable needs to be created with many different levels based on the values in another variable.
}
\value{
Formatted code written with 'dplyr' syntax for recoding variables with `mutate` and `case_when`.
}

%% ~Make other sections like Warning with \section{Warning }{....} ~

\examples{
\dontrun{A simple applications is to add geographical FIPS codes to U.S. states}

states <- as.data.frame(state.abb)
speedy_mutate(data = states, var = "state.abb")
}
2 changes: 1 addition & 1 deletion man/speedy_rename.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ speedy_rename(data, path = "")
name of dataset
}
\item{path}{
If saving code to a separate file, specify the file path here. Leave blank if not saving code.
If saving code to a new R script file, specify the file path here. Leave blank if not saving code.
}
}
\details{
Expand Down

0 comments on commit 4f3724d

Please sign in to comment.