Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs to remove epidemiology examples #23

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Suggests:
dplyr,
knitr,
magrittr,
outbreaks,
rmarkdown,
spelling,
testthat,
Expand Down
95 changes: 41 additions & 54 deletions R/datatagr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,73 +43,60 @@
#'
#' @examples
#'
#' if (require(outbreaks)) {
#' # using base R style
#' # using base R style
#' x <- make_datatagr(cars[1:50, ],
#' mph = "speed",
#' distance = "dist"
#' )
#' x
#'
#' ## dataset we'll create a datatagr from, only using the first 50 entries
#' measles_hagelloch_1861[1:50, ]
#' ## check tagged variables
#' tags(x)
#'
#' ## create datatagr
#' x <- make_datatagr(measles_hagelloch_1861[1:50, ],
#' id = "case_ID",
#' date_onset = "date_of_prodrome",
#' age = "age",
#' gender = "gender"
#' )
#' x
#' ## robust renaming
#' names(x)[1] <- "identifier"
#' x
#'
#' ## check tagged variables
#' tags(x)
#' ## example of dropping tags by mistake - default: warning
#' x[, 2]
#'
#' ## robust renaming
#' names(x)[1] <- "identifier"
#' x
#' ## to silence warnings when taggs are dropped
#' lost_tags_action("none")
#' x[, 2]
#'
#' ## example of dropping tags by mistake - default: warning
#' x[, 2:5]
#' ## to trigger errors when taggs are dropped
#' # lost_tags_action("error")
#' # x[, 2:5]
#'
#' ## to silence warnings when taggs are dropped
#' lost_tags_action("none")
#' x[, 2:5]
#' ## reset default behaviour
#' lost_tags_action()
#'
#' ## to trigger errors when taggs are dropped
#' # lost_tags_action("error")
#' # x[, 2:5]
#' # using tidyverse style
#'
#' ## reset default behaviour
#' lost_tags_action()
#' ## example of creating a datatagr, adding a new variable, and adding a tag
#' ## for it
#'
#' if (require(dplyr) && require(magrittr)) {
#' x <- cars %>%
#' tibble() %>%
#' make_datatagr(
#' mph = "speed",
#' distance = "dist"
#' ) %>%
#' mutate(result = if_else(speed > 50, "fast", "slow")) %>%
#' set_tags(ticket = "result")
#'
#' # using tidyverse style
#' head(x)
#'
#' ## example of creating a datatagr, adding a new variable, and adding a tag
#' ## for it
#' ## extract tagged variables
#' x %>%
#' select(has_tag(c("mph", "distance")))
#'
#' if (require(dplyr) && require(magrittr)) {
#' x <- measles_hagelloch_1861 %>%
#' tibble() %>%
#' make_datatagr(
#' id = "case_ID",
#' date_onset = "date_of_prodrome",
#' age = "age",
#' gender = "gender"
#' ) %>%
#' mutate(result = if_else(is.na(date_of_death), "survived", "died")) %>%
#' set_tags(outcome = "result") %>%
#' rename(identifier = case_ID)
#' x %>%
#' tags()
#'
#' head(x)
#'
#' ## extract tagged variables
#' x %>%
#' select(has_tag(c("gender", "age")))
#'
#' x %>%
#' tags()
#'
#' x %>%
#' select(starts_with("date"))
#' }
#' x %>%
#' select(starts_with("dist"))
#' }
#'
#' @keywords internal
Expand Down
21 changes: 8 additions & 13 deletions R/has_tag.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@
#' @export
#'
#' @examples
#' if (require(outbreaks) && require(dplyr)) {
#' ## dataset we'll create a datatagr from
#' measles_hagelloch_1861
#'
#' ## create datatagr
#' x <- make_datatagr(measles_hagelloch_1861,
#' id = "case_ID",
#' date_onset = "date_of_prodrome",
#' age = "age",
#' gender = "gender"
#' )
#' head(x)
#' ## create datatagr
#' x <- make_datatagr(cars,
#' mph = "speed",
#' distance = "dist"
#' )
#' head(x)
#'
#' if (require(dplyr) && require(magrittr)) {
#' x %>%
#' select(has_tag(c("id", "age"))) %>%
#' select(has_tag(c("mph", "distance"))) %>%
#' head()
#' }
has_tag <- function(
Expand Down
4 changes: 2 additions & 2 deletions R/make_datatagr.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#' @examples
#'
#' x <- make_datatagr(cars,
#' age = "speed",
#' mph = "speed",
#' distance = "dist"
#' )
#'
Expand All @@ -45,7 +45,7 @@
#'
#' ## Tags can also be passed as a list with the splice operator (!!!)
#' my_tags <- list(
#' age = "speed",
#' mph = "speed",
#' distance = "dist"
#' )
#' new_x <- make_datatagr(cars, !!!my_tags)
Expand Down
39 changes: 16 additions & 23 deletions R/names.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,27 @@
#' @export
#'
#' @examples
#' if (require(outbreaks)) {
#' ## dataset to create a datatagr from
#' measles_hagelloch_1861
#' ## create datatagr
#' x <- make_datatagr(cars,
#' mph = "speed",
#' distance = "dist"
#' )
#' head(x)
#'
#' ## create datatagr
#' x <- make_datatagr(measles_hagelloch_1861,
#' id = "case_ID",
#' date_onset = "date_of_prodrome",
#' age = "age",
#' gender = "gender"
#' )
#' head(x)
#' ## change names
#' names(x)[1] <- "speed in miles"
#'
#' ## change names
#' names(x)[1] <- "case_label"
#' ## see results: tags have been updated
#' head(x)
#' tags(x)
#'
#' ## see results: tags have been updated
#' # This also works with using `dplyr::rename()` because it uses names<-()
#' # under the hood
#' if (require(dplyr) && require(magrittr)) {
#' x <- x %>%
#' rename(speed = "speed in miles")
#' head(x)
#' tags(x)
#'
#' # This also works with using `dplyr::rename()` because it uses names<-()
#' # under hood
#' if (require(dplyr)) {
#' x <- x %>%
#' rename(case_id = case_label)
#' head(x)
#' tags(x)
#' }
#' }
`names<-.datatagr` <- function(x, value) {
# Strategy for renaming
Expand Down
43 changes: 17 additions & 26 deletions R/print.datatagr.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,23 @@
#' @export
#'
#' @examples
#' if (require(outbreaks)) {
#' ## dataset we'll create a datatagr from
#' measles_hagelloch_1861
#'
#' ## create datatagr
#' x <- make_datatagr(measles_hagelloch_1861,
#' id = "case_ID",
#' date_onset = "date_of_prodrome",
#' age = "age",
#' gender = "gender"
#' )
#'
#' ## print object - using only the first few entries
#' head(x)
#'
#' # version with a tibble
#' if (require(tibble) && require(magrittr)) {
#' measles_hagelloch_1861 %>%
#' tibble() %>%
#' make_datatagr(
#' id = "case_ID",
#' date_onset = "date_of_prodrome",
#' age = "age",
#' gender = "gender"
#' )
#' }
#' ## create datatagr
#' x <- make_datatagr(cars,
#' mph = "speed",
#' distance = "dist"
#' )
#'
#' ## print object - using only the first few entries
#' head(x)
#'
#' # version with a tibble
#' if (require(tibble) && require(magrittr)) {
#' cars %>%
#' tibble() %>%
#' make_datatagr(
#' mph = "speed",
#' distance = "dist"
#' )
#' }
print.datatagr <- function(x, ...) {
cat("\n// datatagr object\n")
Expand Down
43 changes: 16 additions & 27 deletions R/set_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,22 @@
#'
#' @examples
#'
#' if (require(outbreaks)) {
#' ## create a datatagr
#' x <- make_datatagr(measles_hagelloch_1861, date_onset = "date_of_rash")
#' tags(x)
#'
#' ## add new tags and fix an existing one
#' x <- set_tags(x,
#' age = "age",
#' gender = "gender",
#' date_onset = "date_of_prodrome"
#' )
#' tags(x)
#'
#' ## add non-default tags using allow_extra
#' x <- set_tags(x, severe = "complications", allow_extra = TRUE)
#' tags(x)
#'
#' ## remove tags by setting them to NULL
#' old_tags <- tags(x)
#' x <- set_tags(x, age = NULL, gender = NULL)
#' tags(x)
#'
#' ## setting tags providing a list (used to restore old tags here)
#' x <- set_tags(x, !!!old_tags)
#' tags(x)
#' }
#'
#' ## create a datatagr
#' x <- make_datatagr(cars, mph = "speed")
#' tags(x)
#'
#' ## add new tags and fix an existing one
#' x <- set_tags(x, distance = "dist")
#' tags(x)
#'
#' ## remove tags by setting them to NULL
#' old_tags <- tags(x)
#' x <- set_tags(x, mph = NULL, distance = NULL)
#' tags(x)
#'
#' ## setting tags providing a list (used to restore old tags here)
#' x <- set_tags(x, !!!old_tags)
#' tags(x)
set_tags <- function(x, ..., tag_defaults = list(), allow_extra = TRUE) {
# assert inputs
checkmate::assertClass(x, "datatagr")
Expand Down
15 changes: 6 additions & 9 deletions R/square_bracket.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,15 @@
#' @aliases sub_datatagr
#'
#' @examples
#' if (require(outbreaks) && require(dplyr) && require(magrittr)) {
#' if (require(dplyr) && require(magrittr)) {
#' ## create a datatagr
#' x <- measles_hagelloch_1861 %>%
#' x <- cars %>%
#' make_datatagr(
#' id = "case_ID",
#' date_onset = "date_of_prodrome",
#' age = "age",
#' gender = "gender"
#' mph = "speed",
#' distance = "dist"
#' ) %>%
#' mutate(result = if_else(is.na(date_of_death), "survived", "died")) %>%
#' set_tags(outcome = "result") %>%
#' rename(identifier = case_ID)
#' mutate(result = if_else(speed > 50, "fast", "slow")) %>%
#' set_tags(ticket = "result")
#' x
#'
#' ## dangerous removal of a tagged column setting it to NULL issues a warning
Expand Down
15 changes: 6 additions & 9 deletions R/tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@
#'
#' @examples
#'
#' if (require(outbreaks)) {
#' ## make a datatagr
#' x <- make_datatagr(measles_hagelloch_1861, date_onset = "date_of_prodrome")
#' ## make a datatagr
#' x <- make_datatagr(cars, mph = "speed")
#'
#' ## check non-null tags
#' tags(x)
#'
#' ## get a list of all tags, including NULL ones
#' tags(x, TRUE)
#' }
#' ## check non-null tags
#' tags(x)
#'
#' ## get a list of all tags, including NULL ones
#' tags(x, TRUE)
tags <- function(x, show_null = FALSE) {
checkmate::assertClass(x, "datatagr")
out <- attr(x, "tags")
Expand Down
19 changes: 6 additions & 13 deletions R/tags_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@
#'
#' @examples
#'
#' if (require(outbreaks) && require(magrittr)) {
#' ## create a tibble datatagr
#' x <- measles_hagelloch_1861 %>%
#' make_datatagr(
#' id = "case_ID",
#' date_onset = "date_of_prodrome",
#' age = "age",
#' gender = "gender"
#' )
#' x
#' x <- make_datatagr(cars,
#' mph = "speed",
#' distance = "dist"
#' )
#'
#' ## get a data.frame of all tagged variables
#' tags_df(x)
#' }
#' ## get a data.frame of all tagged variables
#' tags_df(x)
tags_df <- function(x) {
checkmate::assertClass(x, "datatagr")
tags <- unlist(tags(x))
Expand Down
Loading