File: Surv.R

package info (click to toggle)
r-cran-popepi 0.4.13%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,656 kB
  • sloc: sh: 13; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 2,072 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70





#' @md
#' @title Survival Objects
#' @description
#' Wrapper for [survival::Surv].
#' @param time see  [survival::Surv]
#' @param time2 see  [survival::Surv]
#' @param event see  [survival::Surv]
#' @param type see  [survival::Surv]
#' @param origin see [survival::Surv]
#' @section Surv in survival vs. in popEpi:
#' `popEpi::Surv` is a wrapper for [survival::Surv].
#' Therefore you don't need to to do `library("survival")` when using `Surv`
#' with e.g.
#' `[survtab]`. Remember that if you do `library("survival")` after
#' `library("popEpi")`, the `Surv` from \pkg{survival} is used instead of
#' from \pkg{popEpi} (`R` throws a warning about this) when an expression
#' such as `Surv(my_times, my_events)` is evaluated. You can avoid such
#' conflicts by writing e.g. `popEpi::Surv(my_times, my_events)` instead.
#' However, `popEpi::Surv` is designed in such a way that this should not
#' become a problem and you should be able to use the two interchangeably.
#' @export
#' @family main functions
#' @family survtab functions
#' @family survmean functions
#' @importFrom survival Surv
#' @return
#' See `[survival::Surv]`.
#'
Surv <- function(
  time,
  time2,
  event,
  type = c("right", "left", "interval", "counting", "interval2", "mstate"),
  origin = 0
) {

  pf <- parent.frame(1)
  arg_nms <- names(formals(Surv))
  test_env <- environment()
  is_missing <- vapply(arg_nms, function(arg_nm) {
    eval(substitute(
      missing(OBJ),
      list(OBJ = parse(text = arg_nm)[[1]])
    ), envir = test_env)
  }, logical(1))
  pass_arg_nms <- arg_nms[!is_missing]
  pass_arg_nms <- intersect(pass_arg_nms, names(formals(survival::Surv)))

  pass_arg_list <- mget(pass_arg_nms)
  eval_env <- as.environment(pass_arg_list)
  parent.env(eval_env) <- pf
  expr_args <- lapply(pass_arg_nms, function(stri) {
    parse(text = stri)[[1]]
  })
  names(expr_args) <- pass_arg_nms
  surv_expr <- as.call(c(
    quote(survival::Surv),
    expr_args
  ))
  eval(surv_expr, envir = eval_env)
}