Skip to content

Commit

Permalink
Merge pull request #14 from imbi-heidelberg/dev
Browse files Browse the repository at this point in the history
Add some unit tests and fix readme highlighting
  • Loading branch information
jan-imbi authored Nov 14, 2021
2 parents aa85dea + 7919bdc commit 4097bf7
Show file tree
Hide file tree
Showing 26 changed files with 447 additions and 150 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ index.Rmd
inst/.lintr$
^.lintr$
^.vscode$
^benchmarks$
3 changes: 3 additions & 0 deletions .github/workflows/pkgdownDeploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ on:
push:
branches: [main, master]
tags: ['*']
pull_request:
branches: [main, master]
tags: ['*']

name: pkgdown

Expand Down
55 changes: 46 additions & 9 deletions R/loadData.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#' or \code{\link[tibble]{tibble}}, or a vector
#'
#' @return list of labels
#' @examples
#' a <- c(1, 2)
#' attr(a, "label") <- "b"
#' identical(extract_labels(a), list(a = attr(a, "label")))
#' @export
#'
extract_labels <- function(dat) {
Expand All @@ -24,12 +28,18 @@ extract_labels <- function(dat) {
#' @inheritParams extract_labels
#'
#' @return data with the labels removed
#' @examples
#' a <- c(1, 2)
#' attr(a, "label") <- "b"
#' identical(unlabel(a), c(1, 2))
#' @export
#'
unlabel <- function(dat) {
unlabel_fun <- function(x) {
if (inherits(x, "labelled")) {
class(x) <- class(x)[class(x) != "labelled"]
}
if (!is.null(attr(x, "label") <- NULL)){
attr(x, "label") <- NULL
}
return(x)
Expand Down Expand Up @@ -57,6 +67,9 @@ unlabel <- function(dat) {
#' @param path_to_redcap_script (character) Path to the (automatically generated) redcap script for data import
#'
#' @return tibble with data
#' @examples
#' path_to_redcap_script <- system.file("examples", "testredcap.r", package = "DescrTab2")
#' read_redcap_formatted(path_to_redcap_script)
#' @export
#' @importFrom Hmisc label label<-
read_redcap_formatted <- function(path_to_redcap_script = NULL) {
Expand Down Expand Up @@ -87,7 +100,11 @@ read_redcap_formatted <- function(path_to_redcap_script = NULL) {
#'
#' @return a list of datasets separated into the categories as described
#' @export
#'
#'
#' @examples
#' path_to_redcap_script <- system.file("examples", "testredcap.r", package = "DescrTab2")
#' dat <- read_redcap_formatted(path_to_redcap_script)
#' d <- split_redcap_dataset(dat, guess_ID_variable(dat, TRUE))
split_redcap_dataset <- function(dat, id_name = "patid") {
missings_everywhere <-
dat %>% select(!!id_name, where(~ (all(is.na(.x)) | all(.x == ""))))
Expand Down Expand Up @@ -122,7 +139,10 @@ split_redcap_dataset <- function(dat, id_name = "patid") {
#'
#' @return tibble with data
#' @export
#'
#' @examples
#' path_to_data <- system.file("examples", "testsas.sas7bdat", package = "DescrTab2")
#' pat_to_format <- system.file("examples", "formats.sas7bcat", package = "DescrTab2")
#' haven::read_sas(path_to_data, pat_to_format)
#' @importFrom haven read_sas
#'
read_sas_formatted <- function(path_to_data = NULL, path_to_format = NULL) {
Expand All @@ -135,13 +155,16 @@ read_sas_formatted <- function(path_to_data = NULL, path_to_format = NULL) {
erg
}



#' Create a markdown listing from a character dataset
#'
#' @param dat a character \code{data.frame} or \code{tibble}.
#'
#' @return string containing markdown code listing all nonempty free text in the dataset
#' @examples
#' dat <- data.frame(Freetext = c("Some text", "More text"))
#' list_freetext_markdown(dat)
#' # use inside a .Rmd document like this:
#' # `r list_freetext_markdown(dat)`
#' @export
#'
list_freetext_markdown <- function(dat) {
Expand All @@ -155,15 +178,14 @@ list_freetext_markdown <- function(dat) {
var <- var[!(var %in% c("", NA_character_))]
if (length(var) > 0) {
namerow <- paste0("**", print_name, "**\n\n")
varrows <- paste0(" * ", var, "\n\n")
varrows <- paste0(" * ", var, "\n")
str <- paste0(str, namerow, paste0(varrows, collapse = ""), collapse = "")
}
}
str
}



#' Parse a text file containing format information
#'
#' Useful to extract factor formatting information contained in a proc format SAS statement.
Expand All @@ -174,6 +196,15 @@ list_freetext_markdown <- function(dat) {
#' @param encoding Encoding for the text file
#'
#' @return A named list with format definitions
#' @examples
#' tmpfile <- tempfile()
#' write( "proc format;
#' value yn 1=\"yes\"
#' 0=\"no\";
#' value sex 1=\"female\"
#' 0=\"male\";
#' run;",tmpfile)
#' parse_formats(tmpfile)
#' @export
parse_formats <- function(path_to_format_definition,
ignore_keywords = c("value"),
Expand Down Expand Up @@ -317,7 +348,6 @@ text file and make sure there are no labels containing strings of the form '/*'
return(format_list)
}


#' Create code to load all SAS datasets in a folder.
#'
#' This is useful if you work with lots of separate SAS datasets spread out in the same folder.
Expand All @@ -326,6 +356,8 @@ text file and make sure there are no labels containing strings of the form '/*'
#' @param format path to format file
#'
#' @return NULL. Relevant code is printed to the console.
#' @examples
#' codegen_load_all_sas_data(system.file("examples", package = "DescrTab2"))
#' @export
codegen_load_all_sas_data <- function(dir, format = NULL) {
e <- str_subset(list.files(dir), "\\.sas7bdat$")
Expand All @@ -348,7 +380,11 @@ codegen_load_all_sas_data <- function(dir, format = NULL) {
#'
#' @return if exactly one possible
#' @export
#'
#'
#' @examples
#' dat <- data.frame(ID = c(1,2,3,4,5),
#' other = c(1,2,3,4,5))
#' guess_ID_variable(dat)
#' @importFrom stringr str_to_lower
#' @importFrom magrittr `%>%`
guess_ID_variable <- function(dat, suppressWarnings = FALSE) {
Expand Down Expand Up @@ -383,7 +419,7 @@ guess_ID_variable <- function(dat, suppressWarnings = FALSE) {
return(NULL)
}
}

# nocov start
# work in progress
split_data_from_listing <- function(dat, n_split_levels = 10, n_split_characters = 35) {
dat <- as_tibble(dat)
Expand All @@ -408,3 +444,4 @@ split_data_from_listing <- function(dat, n_split_levels = 10, n_split_characters
list = dat[, unlist(idx_list)]
))
}
# nocov end
2 changes: 2 additions & 0 deletions R/prettyDigits.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# nocov start
#' Formatting function for absolute and relative frequencies
#'
#' @param numerator (numeric) numerator for \% calculations
Expand Down Expand Up @@ -176,3 +177,4 @@ good_format <- function(x,
x
}
}
# nocov end
68 changes: 43 additions & 25 deletions R/summaryStats.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,31 @@

.Q1 <- function(var) {
stats::quantile(var,
probs = 0.25,
na.rm = TRUE,
type = 2
probs = 0.25,
na.rm = TRUE,
type = 2
)
}

.Q3 <- function(var) {
stats::quantile(var,
probs = 0.75,
na.rm = TRUE,
type = 2
probs = 0.75,
na.rm = TRUE,
type = 2
)
}

.IQR <- function(var) {
stats::quantile(var,
probs = 0.75,
na.rm = TRUE,
type = 2) -
probs = 0.75,
na.rm = TRUE,
type = 2
) -
stats::quantile(var,
probs = 0.25,
na.rm = TRUE,
type = 2)
probs = 0.25,
na.rm = TRUE,
type = 2
)
}

.min <- function(var) {
Expand All @@ -72,11 +74,11 @@
}

.skew <- function(var) {
base::mean((var-.mean(var))^(3)) / (stats::sd(var))^(3/2)
base::mean((var - .mean(var))^(3)) / (stats::sd(var))^(3 / 2)
}

.kurtosis <- function(var) {
base::mean((var-.mean(var))^(4)) / (stats::sd(var))^(2) -3
base::mean((var - .mean(var))^(4)) / (stats::sd(var))^(2) - 3
}

.factormean <- function(var) {
Expand Down Expand Up @@ -200,14 +202,26 @@
if (any(!is.na(var))) {
var <- var[!is.na(var)]
conds <- list()
ret <- withCallingHandlers({
stats::wilcox.test(var, conf.int=TRUE)$conf.int[1]
},
condition = function(cond) {
conds <<- append(conds, cond)
}
)
for (cond in conds[names(conds)=="message"]) {
ret <-
tryCatch(
{
withCallingHandlers(
{
stats::wilcox.test(var, conf.int = TRUE)$conf.int[1]
},
condition = function(cond) {
conds <<- append(conds, cond)
if (inherits(cond, "warning")) {
tryInvokeRestart("muffleWarning")
}
}
)
},
warning = function(cond) {
return(NULL)
}
)
for (cond in conds[names(conds) == "message"]) {
if (cond == "requested conf.level not achievable") {
warning(cond)
return(NA_real_)
Expand All @@ -223,14 +237,18 @@
if (any(!is.na(var))) {
var <- var[!is.na(var)]
conds <- list()
ret <- withCallingHandlers({
stats::wilcox.test(var, conf.int=TRUE)$conf.int[1]
ret <- withCallingHandlers(
{
stats::wilcox.test(var, conf.int = TRUE)$conf.int[1]
},
condition = function(cond) {
conds <<- append(conds, cond)
if (inherits(cond, "warning")) {
tryInvokeRestart("muffleWarning")
}
}
)
for (cond in conds[names(conds)=="message"]) {
for (cond in conds[names(conds) == "message"]) {
if (cond == "requested conf.level not achievable") {
warning(cond)
return(NA_real_)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ Tables generated by DescrTab2 can be integrated in a variety of document formats


You can also install the development version of DescrTab2 (recommended) from github by typing:
```
```r
remotes::install_github("https://github.com/imbi-heidelberg/DescrTab2")
```

You may also install the stable version of DescrTab2 from cran by typing
```
```r
install.packages("DescrTab2")
```
into your R console.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ for (nsim in c(100, 200, 300, 400, 500)) {
}



for (nsim in c(100, 200, 300, 400, 500)) {
starttime <- Sys.time()

Expand Down
6 changes: 4 additions & 2 deletions docs/index.html

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

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ articles:
c_other_software_comparison: c_other_software_comparison.html
d_validation_statement: d_validation_statement.html
e_maintenance_guide: e_maintenance_guide.html
last_built: 2021-11-14T16:14Z
last_built: 2021-11-14T19:53Z
urls:
reference: https://imbi-heidelberg.github.io/DescrTab2/reference
article: https://imbi-heidelberg.github.io/DescrTab2/articles
Expand Down
Loading

0 comments on commit 4097bf7

Please sign in to comment.