diff --git a/DESCRIPTION b/DESCRIPTION index e56078f..89e34f4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,6 +14,7 @@ RoxygenNote: 7.2.3 Imports: data.table, dplyr, + magrittr, srvyr, stats, stringr diff --git a/NAMESPACE b/NAMESPACE index 7a7bc6d..52fbdf4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export("%>%") export(factorize_column) export(factorize_df) export(hts_bin_var) @@ -20,6 +21,12 @@ export(hts_summary_num) export(hts_to_so) export(hts_trip_vehid) import(data.table) -import(dplyr) -import(srvyr) -import(stats) +importFrom(data.table,melt) +importFrom(dplyr,group_by_at) +importFrom(dplyr,summarize) +importFrom(magrittr,"%>%") +importFrom(srvyr,survey_mean) +importFrom(srvyr,survey_median) +importFrom(srvyr,survey_prop) +importFrom(srvyr,survey_total) +importFrom(stats,quantile) diff --git a/R/hts_bin_var.R b/R/hts_bin_var.R index 94f3738..3a7efce 100644 --- a/R/hts_bin_var.R +++ b/R/hts_bin_var.R @@ -8,7 +8,7 @@ #' @export #' #' -#' @import stats +#' @importFrom stats quantile #' #' @examples #' diff --git a/R/hts_find_var.R b/R/hts_find_var.R index d970629..453fb35 100644 --- a/R/hts_find_var.R +++ b/R/hts_find_var.R @@ -3,7 +3,7 @@ #' @param var searchable variable in string format #' @param variables_dt codebook variable list in data table format #' -#' @import data.table +#' @importFrom data.table melt #' #' @return variable location in string format #' @export diff --git a/R/hts_melt_vars.R b/R/hts_melt_vars.R index 55640c9..b44f9a3 100644 --- a/R/hts_melt_vars.R +++ b/R/hts_melt_vars.R @@ -19,6 +19,8 @@ #' @param to_single_row Boolean if treating multiple checkbox selections as 'Two #' or more'. Defaults to FALSE. #' +#' @import data.table +#' #' @return Inputted data table with checkbox variables melted into a single variable, #' with a 'variable' column to indicate original variable names and a 'value' column #' with the original value of the checkbox variable. diff --git a/R/hts_summary_cat.R b/R/hts_summary_cat.R index 72b1688..b76e7ec 100644 --- a/R/hts_summary_cat.R +++ b/R/hts_summary_cat.R @@ -14,8 +14,11 @@ #' @param checkbox_yesval Value of checkbox_valname that indicates it was selected. #' Default is NULL. Must be provided if summarize_var is a checkbox variable. #' -#' @import dplyr -#' @import srvyr +#' +#' @importFrom srvyr survey_prop +#' @importFrom srvyr survey_total +#' @importFrom dplyr summarize +#' @importFrom dplyr group_by_at #' #' @return List of unweighted and weighted categorical summaries including counts #' and proportions. @@ -290,5 +293,5 @@ hts_summary_cat = function(prepped_dt, } ## quiets concerns of R CMD check -utils::globalVariables(c("value_labels", "prop", "est")) +utils::globalVariables(c("value_labels", "prop", "est", "count")) diff --git a/R/hts_summary_num.R b/R/hts_summary_num.R index ae20a74..44249fa 100644 --- a/R/hts_summary_num.R +++ b/R/hts_summary_num.R @@ -13,6 +13,11 @@ #' when weighted = TRUE. #' @param strataname Name of strata name to bring in. Default is NULL. #' +#' @importFrom srvyr survey_mean +#' @importFrom srvyr survey_median +#' @importFrom dplyr summarize +#' @importFrom dplyr group_by_at +#' #' @return List of unweighted and weighted numeric summaries including count, min, #' max, mean, se, and median. #' @export diff --git a/R/hts_trip_vehid.R b/R/hts_trip_vehid.R index 79d26d3..f82314b 100644 --- a/R/hts_trip_vehid.R +++ b/R/hts_trip_vehid.R @@ -70,5 +70,5 @@ before proceding.") } ## quiets concerns of R CMD check -utils::globalVariables(c("value_labels", "vehicle_id", "mode_1", "mode_type")) +utils::globalVariables(c("value_labels", "vehicle_id", "mode_1", "mode_type", "hh_id")) diff --git a/R/utils-pipe.R b/R/utils-pipe.R new file mode 100644 index 0000000..fd0b1d1 --- /dev/null +++ b/R/utils-pipe.R @@ -0,0 +1,14 @@ +#' Pipe operator +#' +#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +#' +#' @name %>% +#' @rdname pipe +#' @keywords internal +#' @export +#' @importFrom magrittr %>% +#' @usage lhs \%>\% rhs +#' @param lhs A value or the magrittr placeholder. +#' @param rhs A function call using the magrittr semantics. +#' @return The result of calling `rhs(lhs)`. +NULL diff --git a/man/hts_filter_data.Rd b/man/hts_filter_data.Rd index d008d91..ed59d36 100644 --- a/man/hts_filter_data.Rd +++ b/man/hts_filter_data.Rd @@ -4,16 +4,14 @@ \alias{hts_filter_data} \title{Filter datasets to only keep specified ids} \usage{ -hts_filter_data(hts_data, ids, id_type = "hh") +hts_filter_data(hts_data, ids, id_name) } \arguments{ -\item{hts_data}{List of containing household, person, day, trip, and vehicle -tables in data.table format} +\item{hts_data}{List of containing household travel data tables.} \item{ids}{List of ids to keep in all of the tables} -\item{id_type}{Type of id being used for filtering. Options are 'hh', 'person', -'day', and 'trip'. Defaults to 'hh'.} +\item{id_name}{Name of id being used for filtering (e.g., hh_id, person_id)} } \value{ Inputted list of tables filtered to the specified ids. @@ -24,12 +22,11 @@ Filter datasets to only keep specified ids \examples{ require(data.table) -hts_filter_data(hts_data = list('hh' = hh, - 'person' = person, - 'day' = day, - 'trip' = trip, - 'vehicle' = vehicle), + +data(test_data) + +hts_filter_data(hts_data = test_data, ids = hh[num_people > 5, hh_id], - id_type = 'hh') + id_name = 'hh_id') } diff --git a/man/pipe.Rd b/man/pipe.Rd new file mode 100644 index 0000000..a648c29 --- /dev/null +++ b/man/pipe.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-pipe.R +\name{\%>\%} +\alias{\%>\%} +\title{Pipe operator} +\usage{ +lhs \%>\% rhs +} +\arguments{ +\item{lhs}{A value or the magrittr placeholder.} + +\item{rhs}{A function call using the magrittr semantics.} +} +\value{ +The result of calling \code{rhs(lhs)}. +} +\description{ +See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +} +\keyword{internal} diff --git a/tests/testthat/test_hts_filter_data.R b/tests/testthat/test_hts_filter_data.R index 3b9c2c8..541ddb4 100644 --- a/tests/testthat/test_hts_filter_data.R +++ b/tests/testthat/test_hts_filter_data.R @@ -12,7 +12,7 @@ library(data.table) hts_data = test_data # Call the function to filter based on hh_id - result_hh = hts_filter_data(hts_data, hh[num_people > 5, hh_id], id_type = 'hh') + result_hh = hts_filter_data(hts_data, hh[num_people > 5, hh_id], id_name = 'hh') # Check if the result is a list expect_is(result_hh, "list", diff --git a/tests/testthat/test_hts_summary_num.R b/tests/testthat/test_hts_summary_num.R index cd8d7c9..9f211c7 100644 --- a/tests/testthat/test_hts_summary_num.R +++ b/tests/testthat/test_hts_summary_num.R @@ -4,6 +4,7 @@ context("Test suite for hts_summary_num function") # Load necessary libraries and setup environment library(testthat) library(data.table) +library(srvyr) DT = hts_prep_data(summarize_var = 'speed_mph', diff --git a/vignettes/getting_started.Rmd b/vignettes/getting_started.Rmd index e3c2ad5..e7297b4 100644 --- a/vignettes/getting_started.Rmd +++ b/vignettes/getting_started.Rmd @@ -111,6 +111,8 @@ In order to create summaries of our data we first need to prepare our data. We c ```{r, hts_prep_data, eval=TRUE, echo=TRUE } library(travelSurveyTools) +library(data.table) +library(srvyr) # Load data data("test_data") @@ -264,7 +266,7 @@ DT = hts_prep_triprate(summarize_by = 'employment', 'vehicle' = vehicle)) trip_rate_by_employment_summary = hts_summary(prepped_dt = DT$num, - summarize_var = 'num_trips', + summarize_var = 'num_trips_wtd', summarize_by = 'employment', summarize_vartype = 'numeric', weighted = TRUE,