Skip to content

Commit

Permalink
#107 docs
Browse files Browse the repository at this point in the history
  • Loading branch information
edgar-manukyan committed Dec 2, 2024
1 parent a187e18 commit 18a69d6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
109 changes: 55 additions & 54 deletions R/generate_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,61 @@ tgt_vars <- c(
"CMENDTC"
)

#' Generate the code for the mapping SDTM specification
#'
#' One can use the option `width` to control the width of the code. A width of
#' twenty will almost always place every parameter on a separate line. This is
#' useful for debugging and understanding the code. The higher the width, the
#' more parameters will be placed on a single line and code will be shorter.
#' See the examples for more details.
#'
#' @param spec The specification data frame.
#' @param domain The SDTM domain to generate the code for.
#' @param out_dir The directory to save the code file. Default is the current
#' directory.
#'
#' @return Side effect: the code is generated and saved to a file.
#' @export
#'
#' @examples
#' \dontrun{
#' spec <- read_spec("cm_sdtm_oak_spec_cdash.csv")
#' domain <- "cm"
#' generate_code(spec, domain)
#'
#' # One can use option width to control the width of the code
#' # Twenty will almost always place every parameter on a separate line
#' spec <- read_spec("cm_sdtm_oak_spec_cdash.csv")
#' domain <- "cm"
#' old_width <- options(width = 20)
#' generate_code(spec, domain)
#' # Restore original width
#' options(width = old_width$width)
#' }
#'
generate_code <- function(spec, domain, out_dir = ".") {
admiraldev::assert_data_frame(spec, required_vars = rlang::syms(expected_columns))
admiraldev::assert_character_scalar(domain)

spec_domain <- get_domain_spec(spec, domain)

# Generate the code for each variable row in spec_domain
styled_code <- purrr::map(
seq_len(nrow(spec_domain)),
\(row) generate_one_var_code(spec_domain[row, ])
) |>
unlist() |>
# remove_last_pipe() |>
append(cm_template_prefix, after = 0L) |>
append(cm_template_suffix) |>
styler::style_text()

# Save the code to a file
file_name <- paste0(domain, "_sdtm_oak_code.R")
writeLines(styled_code, file.path(out_dir, file_name))
}


#' The template suffix for the cm code
#'
#' @keywords internal
Expand Down Expand Up @@ -111,60 +166,6 @@ is_character <- function(var_in) {
}


#' Generate the code for the mapping SDTM specification
#'
#' One can use the option `width` to control the width of the code. A width of
#' twenty will almost always place every parameter on a separate line. This is
#' useful for debugging and understanding the code. The higher the width, the
#' more parameters will be placed on a single line and code will be shorter.
#' See the examples for more details.
#'
#' @param spec The specification data frame.
#' @param domain The SDTM domain to generate the code for.
#' @param out_dir The directory to save the code file. Default is the current
#' directory.
#'
#' @return Side effect: the code is generated and saved to a file.
#' @export
#'
#' @examples
#' \dontrun{
#' spec <- read_spec("cm_sdtm_oak_spec_cdash.csv")
#' domain <- "cm"
#' generate_code(spec, domain)
#'
#' # One can use option width to control the width of the code
#' # Twenty will almost always place every parameter on a separate line
#' spec <- read_spec("cm_sdtm_oak_spec_cdash.csv")
#' domain <- "cm"
#' old_width <- options(width = 20)
#' generate_code(spec, domain)
#' options(width = old_width)
#' }
#'
generate_code <- function(spec, domain, out_dir = ".") {
admiraldev::assert_data_frame(spec, required_vars = rlang::syms(expected_columns))
admiraldev::assert_character_scalar(domain)

spec_domain <- get_domain_spec(spec, domain)

# Generate the code for each variable row in spec_domain
styled_code <- purrr::map(
seq_len(nrow(spec_domain)),
\(row) generate_one_var_code(spec_domain[row, ])
) |>
unlist() |>
# remove_last_pipe() |>
append(cm_template_prefix, after = 0L) |>
append(cm_template_suffix) |>
styler::style_text()

# Save the code to a file
file_name <- paste0(domain, "_sdtm_oak_code.R")
writeLines(styled_code, file.path(out_dir, file_name))
}


#' Generate the code for one variable
#'
#' @param spec_var The specification for one variable.
Expand Down
3 changes: 2 additions & 1 deletion man/generate_code.Rd

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

0 comments on commit 18a69d6

Please sign in to comment.