File: attach.R

package info (click to toggle)
r-cran-m2r 1.0.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 472 kB
  • sloc: cpp: 195; python: 59; sh: 14; makefile: 2
file content (122 lines) | stat: -rw-r--r-- 2,137 bytes parent folder | download | duplicates (3)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
.onAttach <- function(...) {

  packageStartupMessage('  Please cite m2r! See citation("m2r") for details.')

  # set gmp
  set_m2r_option(gmp = FALSE)

  # set pathing
  if (is.mac() || is.linux()) {
    if (Sys.getenv("M2") == "") {
      set_m2r_option(m2_path = "/usr/bin")
    } else {
      set_m2r_option(m2_path = Sys.getenv("M2"))
    }
  } else {
    set_m2r_option(m2_path = "")
  }


  # check that M2 was found in ~/.Renviron
  startup_check_for_program()


  # return
  invisible(TRUE)
}




.onDetach <- function(...) {
  stop_m2()
  options(m2r = NULL)
}
# restart R
# library(m2r)
# m2("1+1")
# getOption("m2r")
# detach("package:m2r")
# getOption("m2r")





psm  <- packageStartupMessage

psms <- function(fmt, ...) packageStartupMessage(sprintf(fmt, ...))


startup_check_for_program <- function(){

  if(get_m2_path() != ""){

    psms("  M2 found in %s", get_m2_path())
    return(invisible(FALSE))

  } else {

    psms("  M2 not found; defaulting to cloud.")
    psms("  Use set_m2_path(\"/path/to/m2\") to run M2 locally.")
    return(invisible(FALSE))

  }

  invisible(TRUE)
}



setOption <- function(optionName, value){
  eval(parse(text = sprintf('options("%s" = "%s")', optionName, value)))
}






# set_m2r_option both sets options for m2r in the list m2r in options
# and initialized the list when m2r is attached to the search path
# (search())
set_m2r_option <- function(...) {

  # if there is no m2r option (package is being initialized)
  # create the list with the arguments and return
  if ("m2r" %notin% names(options())) {
    options(m2r = list(...))
    return(invisible())
  }

  # otherwise, go through arguments sequentially and add/update
  # them in the list m2r in options
  m2r <- getOption("m2r")
  arg_list <- lapply(as.list(match.call())[-1], eval, envir = parent.frame())
  for (k in seq_along(arg_list)) {
    if (names(arg_list)[k] %in% names(m2r)) {
      m2r[names(arg_list)[k]] <- arg_list[k]
    } else {
      m2r <- c(m2r, arg_list[k])
    }
  }

  # set new m2r
  options(m2r = m2r)

  # return
  invisible()
}
# (l <- list(a = 1, b = 2, c = 3))
# l[d] <- 5
# l