33# ' @title Get the reticulate Python tskit module
44# ' @description This function imports the reticulate Python \code{tskit} module
55# ' and if it is not yet installed, then it attempts to install it first.
6- # ' @param obj_name character name of the object holding \code{tskit} reticulate
6+ # ' @param object_name character name of the object holding \code{tskit} reticulate
77# ' Python module. If this object exists in the global R environment and is a
88# ' reticulate Python object, then it is returned. Otherwise, the function
99# ' attempts to install and import tskit before returning it. If \code{NULL},
1010# ' then the function directly attempts to install and import tskit before
1111# ' returning it.
12+ # ' @param object reticulate Python module object, hopefully.
13+ # ' @param stop logical for throwing an error in \code{check_tskit_py}.
1214# ' @details This function is meant for users running \code{tskit <- get_tskit_py()}
1315# ' or similar code, but also by other functions in this package that need the
1416# ' \code{tskit} reticulate Python module. The point of \code{get_tskit_py} is
1517# ' to avoid importing the module repeatedly, if it has been imported already.
16- # ' @return \code{tskit} reticulate Python module.
18+ # ' @return \code{get_tskit_py} returns \code{tskit} reticulate Python module.
19+ # ' \code{check_tskit_py} returns \code{TRUE} if
1720# ' @examples
1821# ' tskit <- get_tskit_py()
1922# ' is(tskit)
20- # ' tskit$ALLELES_01
23+ # ' if (check_tskit_py(tskit)) {
24+ # ' tskit$ALLELES_01
25+ # ' }
2126# ' @export
22- get_tskit_py <- function (obj_name = " tskit" ) {
23- test <- ! is.null(obj_name ) &&
24- exists(obj_name , envir = .GlobalEnv , inherits = FALSE )
27+ get_tskit_py <- function (object_name = " tskit" ) {
28+ test <- ! is.null(object_name ) &&
29+ exists(object_name , envir = .GlobalEnv , inherits = FALSE )
2530 if (test ) {
26- tskit <- get(obj_name , envir = .GlobalEnv , inherits = FALSE )
31+ tskit <- get(object_name , envir = .GlobalEnv , inherits = FALSE )
2732 test <- reticulate :: is_py_object(tskit ) &&
2833 is(tskit ) == " python.builtin.module"
2934 if (test ) {
3035 return (tskit )
3136 } else {
3237 txt <- paste0(
3338 " Object '" ,
34- obj_name ,
35- " ' exists in the global environment but is not a reticulate Python module"
39+ object_name ,
40+ " ' exists in the global environment but is not a reticulate Python module! "
3641 )
3742 stop(txt )
3843 }
@@ -42,13 +47,31 @@ get_tskit_py <- function(obj_name = "tskit") {
4247 # nocov start
4348 if (! reticulate :: py_module_available(" tskit" )) {
4449 txt <- " Python module 'tskit' is not available. Attempting to install it ..."
45- cat (txt )
50+ message (txt )
4651 reticulate :: py_require(" tskit" )
4752 }
4853 # nocov end
4954 return (reticulate :: import(" tskit" , delay_load = TRUE ))
5055}
5156
57+ # ' @describeIn get_tskit_py Test if \code{get_tskit_py} returned a reticulate Python module object
58+ # ' @export
59+ check_tskit_py <- function (object , stop = FALSE ) {
60+ test <- reticulate :: is_py_object(object ) &&
61+ (" python.builtin.module" %in% is(object ))
62+ if (test ) {
63+ return (TRUE )
64+ } else {
65+ msg <- " object must be a reticulate Python module object!"
66+ if (stop ) {
67+ stop(msg )
68+ } else {
69+ message(msg )
70+ }
71+ return (FALSE )
72+ }
73+ }
74+
5275# ' @title Load a tree sequence from a file
5376# ' @param file a string specifying the full path of the tree sequence file.
5477# ' @param options integer bitwise options (see details at
@@ -182,9 +205,7 @@ ts_r_to_py_ptr <- function(ts, tskit_module = get_tskit_py(), cleanup = TRUE) {
182205 if (! is(ts , " externalptr" )) {
183206 stop(" ts must be an object of externalptr class!" )
184207 }
185- if (! reticulate :: is_py_object(tskit_module )) {
186- stop(" tskit_module must be a reticulate Python module object!" )
187- }
208+ check_tskit_py(tskit_module , stop = TRUE )
188209 ts_file <- tempfile(fileext = " .trees" )
189210 if (cleanup ) {
190211 on.exit(file.remove(ts_file ))
@@ -247,16 +268,18 @@ ts_py_to_r_ptr <- function(ts, cleanup = TRUE) {
247268# '
248269# ' # Use the tskit Python API to work with a tree sequence (via reticulate)
249270# ' tskit <- get_tskit_py()
250- # ' ts_py <- tskit$load(ts_file)
251- # ' is(ts_py)
252- # ' ts_py$num_samples # 160
253- # ' ts2_py <- ts_py$simplify(samples = c(0L, 1L, 2L, 3L))
254- # ' ts2_py$num_samples # 4
271+ # ' if (check_tskit_py(tskit)) {
272+ # ' ts_py <- tskit$load(ts_file)
273+ # ' is(ts_py)
274+ # ' ts_py$num_samples # 160
275+ # ' ts2_py <- ts_py$simplify(samples = c(0L, 1L, 2L, 3L))
276+ # ' ts2_py$num_samples # 4
255277# '
256- # ' # Transfer the tree sequence to R and use RcppTskit
257- # ' ts2_r <- ts_py_to_r(ts2_py)
258- # ' is(ts2_r)
259- # ' ts2_r$num_samples() # 4
278+ # ' # Transfer the tree sequence to R and use RcppTskit
279+ # ' ts2_r <- ts_py_to_r(ts2_py)
280+ # ' is(ts2_r)
281+ # ' ts2_r$num_samples() # 4
282+ # ' }
260283# ' @export
261284ts_py_to_r <- function (ts , cleanup = TRUE ) {
262285 ptr <- ts_py_to_r_ptr(ts = ts , cleanup = cleanup )
0 commit comments