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

Static #3

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ coverage.*
init.sh
workflows.md
images
^\_targets\.yaml$
^README\.Rmd$
^data-raw$
^LICENSE\.md$
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ coverage.*
node_modules/
package-lock.json
package.json
data-raw/_targets
data-raw/mmrm_review.R

40 changes: 16 additions & 24 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
Type: Package
Package: r.pkg.template
Title: R Package Template
Version: 0.1.0.9130
Date: 2023-07-05
Authors@R:
person("insightsengineering", , , "[email protected]", role = c("aut", "cre"))
Description: R package template with GitHub Actions workflows included.
License: Apache License 2.0 | file LICENSE
URL: https://github.com/insightsengineering/r.pkg.template/
BugReports: https://github.com/insightsengineering/r.pkg.template/issues
Depends:
R (>= 3.6)
Imports:
plumber,
shiny,
stringr
Suggests:
future,
httr,
Package: clindata
Title: Open Clinical Data
Version: 0.0.1
Date: 2023-11-09
Authors@R: person("Jonathan","Sidi" , , "[email protected]", role = c("aut", "cre"))
Description: Reproducible simulated clinical trials for use in R packages.
License: MIT + file LICENSE
URL: https://github.com/openpharma/clindata
BugReports: https://github.com/openpharma/clindata/issues
Depends: R (>= 4.0)
Imports: tools, utils, MASS, clusterGeneration
Suggests:
knitr,
shinytest,
testthat (>= 2.0)
VignetteBuilder:
knitr
biocViews:
withr,
testthat (>= 3.0.0)
VignetteBuilder: knitr
Encoding: UTF-8
Language: en-US
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Config/testthat/edition: 3
15 changes: 2 additions & 13 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
Copyright 2022 F. Hoffmann-La Roche AG

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
YEAR: 2024
COPYRIGHT HOLDER: clindata authors
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2024 clindata authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 13 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Generated by roxygen2: do not edit by hand

export(hello)
export(plumber_api)
export(shiny_app)
importFrom(utils,packageName)
export(cache_data)
export(cache_destroy)
export(cache_dir)
export(cache_init)
export(cache_ls)
export(cache_rm)
export(compute_unstructured_matrix)
export(generate_outcomes)
export(list_data)
export(load_data)
export(mar)
export(mcar)
export(read_data)
97 changes: 97 additions & 0 deletions R/api_cache.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#' @title Data Caching API
#' @description Functions to manage cache data across sessions.
#' @param path character, path to cache directory, Default: `cache_dir()`
#' @param \dots arguments passed to [list.files()]
#' @param verbose logical, show message on success of [cache_destroy()]
#' @return character
#' @details
#' If the environment variable CLINDATA_CACHE_DIR is set then [cache_dir()] will use the value.
#'
#' If the environment variable CLINDATA_CACHE_DIR is not set then [cache_dir()] will
#' default to the internal mechanism via [R_user_dir][tools::R_user_dir].
#' @examples
#' cache_dir()
#' cache_init()
#' cache_ls()
#' cache_destroy()
#' @rdname cache_api
#' @export
cache_init <- function(path = cache_dir()) {

dir.create(path = path, recursive = TRUE, showWarnings = FALSE)

}

#' @rdname cache_api
#' @export
cache_ls <- function(path = cache_dir(), ...) {

list.files(path = path, ...)

}

#' @rdname cache_api
#' @export
cache_dir <- function() {
if (nzchar(Sys.getenv("CLINDATA_CACHE_DIR")))
return(Sys.getenv("CLINDATA_CACHE_DIR"))

tools::R_user_dir(
package = "clindata",
which = "cache"
)

}

#' @title Clear data cache
#' @description Clear file(s) from the cache path.
#' @param files character, files to be removed,
#' Default: [cache_ls][cache_ls](full.names = TRUE)
#' @param recursive boolean. Should directories be deleted recursively?
#' @param force boolean. Should permissions be changed (if possible) to
#' allow the file or directory to be removed?
yonicd marked this conversation as resolved.
Show resolved Hide resolved
#' @return NULL
#' @examples
#' # initialize the cache directory
#' cache_init()
#'
#' # populate the cache
#' cache_data("fev_data")
#' cache_data("cars")
#'
#' # list files in the cache
#' cache_ls()
#'
#' # clear all the files in the cache
#' cache_rm()
#' cache_ls()
#'
#' # remove a single file based on a pattern
#' cache_data("fev_data")
#' cache_data("cars")
#' cache_rm(cache_ls(pattern = "fev", full.names = TRUE))
#' cache_ls()
#'
#' # cleanup
#' cache_destroy()
#' @export
cache_rm <- function(
files = cache_ls(full.names = TRUE),
recursive = FALSE,
force = FALSE) {

invisible(
lapply(files,
function(f) {
unlink(x = f, recursive = recursive, force = force)
})
)
}

#' @rdname cache_api
#' @export
cache_destroy <- function(path = cache_dir(), verbose = TRUE) {
unlink(path, recursive = TRUE, force = TRUE)
if (verbose)
message(path, " directory removed")
}
117 changes: 117 additions & 0 deletions R/api_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#' @title Read data
#' @description Read data directly into a global environment
#' @param data character, name of data object
#' @param filetype character, extension type of the data. Default: "rds"
#' @param use_cache boolean, use the data stored in cache? Default: FALSE
#' @return data stored in either the local cache directory or within the package.
#' @details
#' Currently only loading of rds files is supported
#' @examples
#' read_data("fev_data")
#' read_data("bcva_data")
#' @rdname data_api
#' @export
read_data <- function(data, filetype = "rds", use_cache = FALSE) {

if(use_cache) {
cache_dir <- cache_dir()
if(dir.exists(cache_dir)) {
items <- tools::file_path_sans_ext(list.files(cache_dir))
if(data %in% items){
data_path <- file.path(cache_dir, sprintf("%s.%s", data, filetype))
message(sprintf("Retrieving %s from cache", data))
if(filetype == "rds"){
data_out <- readRDS(data_path)
}
} else {
stop(sprintf("%s not in cache", data))
}
}
}else{
data_out <- get(data, asNamespace("clindata"))
}

return(data_out)

}

#' @title Load data
#' @description Load data directly into an enivornment
#' @param data character, name of data object
#' @param env environment, Default: parent.frame()
#' @param ... arguments passed to assign [assign][base::assign]
#' @return NULL
#' @details
#' By default the data is loaded into the parent frame from which it is called.
#' @examples
#' load_data("fev_data")
#' @rdname load_data
#' @export
load_data <- function(data, env = parent.frame(), ...) {

invisible(
Map(function(x) {
assign(x, read_data(x, ...), envir = env)
},
x = data
)
)

}

#' @title Store data
#' @description Store data in the cache
#' @param data character, name of the object to store
#' @param filetype character, extension of the
#' filetype to save the data. Default: "rds"
#' @return NULL
#' @details
#' Currently only rds is supported
#' @examples
#' # initialize the cache directory
#' cache_init()
#'
#' # populate the cache
#' cache_data("fev_data")
#' cache_ls()
#'
#' # cleanup
#' cache_destroy()
#' @rdname cache_data
#' @export
cache_data <- function(data, filetype = "rds") {
cl <- match.call()
data_name <- as.character(cl[[2]])

cache_dir <- cache_dir()

if(!dir.exists(cache_dir)) {
cache_init()
}

if(filetype == "rds") {
saveRDS(
object = data,
file = file.path(cache_dir, sprintf("%s.%s", data_name, filetype))
)
}

}

#' @title List data
#' @description List the clinical data stored in the package
#' @return character
#' @examples
#' list_data()
#' @rdname list_data
#' @export
list_data <- function() {

utils::data(package = "clindata")[["results"]][, "Item"]

}

test_data <- function(data) {
f <- system.file(file.path("benchmark", sprintf("%s.rds", data)), package = "clindata")
readRDS(f)
}
6 changes: 6 additions & 0 deletions R/clindata-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#' `clindata` Package
#'
#' `clindata` data package for clinical trials data.
#'
#' @aliases clindata-package
"_PACKAGE"
20 changes: 20 additions & 0 deletions R/data-bcva.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#' Simulated BCVA Data
#'
#' @description `r lifecycle::badge("experimental")`
#'
#' @note Measurements of BCVA (best corrected visual acuity) is a measure of how
#' how many letters a person can read off of an eye chart using corrective
#' lenses or contacts. This a common endpoint in ophthalmology trials.
#'
#' @format A `tibble` with 10,000 rows and 7 variables:
#' - `USUBJID`: subject ID.
#' - `VISITN`: visit number (numeric).
#' - `AVISIT`: visit number (factor).
#' - `ARMCD`: treatment, `TRT` or `CTL`.
#' - `RACE`: 3-category race.
#' - `BCVA_BL`: BCVA at baseline.
#' - `BCVA_CHG`: Change in BCVA at study visits.
#'
#' @source This is an artificial dataset.
#' @name bcva_data
NULL
21 changes: 21 additions & 0 deletions R/data-fev.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#' Simulated FEV1 Data
#'
#' @description `r lifecycle::badge("experimental")`
#'
#' @note Measurements of FEV1 (forced expired volume in one second) is a measure
#' of how quickly the lungs can be emptied. Low levels of FEV1 may indicate
#' chronic obstructive pulmonary disease (COPD).
#'
#' @format A `tibble` with 800 rows and 7 variables:
#' - `USUBJID`: subject ID.
#' - `AVISIT`: visit number.
#' - `ARMCD`: treatment, `TRT` or `PBO`.
#' - `RACE`: 3-category race.
#' - `SEX`: sex.
#' - `FEV1_BL`: FEV1 at baseline (%).
#' - `FEV1`: FEV1 at study visits.
#' - `WEIGHT`: weighting variable.
#'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include the VISIT* variables?

#' @source This is an artificial dataset.
#' @name fev_data
NULL
Loading
Loading