| # Licensed to the Apache Software Foundation (ASF) under one |
| # or more contributor license agreements. See the NOTICE file |
| # distributed with this work for additional information |
| # regarding copyright ownership. The ASF licenses this file |
| # to you 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. |
| |
| .onLoad <- function(...) { |
| # Load geoarrow to manage conversion of arrow results to/from spatial objects |
| requireNamespace("geoarrow", quietly = TRUE) |
| |
| s3_register("sf::st_as_sf", "sedonadb_dataframe") |
| |
| # Inject what we need to reduce the Rust code to a simple Rf_eval() |
| ns <- asNamespace("sedonadb") |
| call <- call("check_interrupts") |
| init_r_runtime_interrupts(call, ns) |
| } |
| |
| # The function we call from Rust to check for interrupts. R checks for |
| # interrupts automatically when evaluating regular R code and signals |
| # an interrupt condition, |
| check_interrupts <- function() { |
| tryCatch({ |
| FALSE |
| }, interrupt = function(...) TRUE, error = function(...) TRUE) |
| } |
| |
| # Permissively licensed (unlicense) from |
| # https://github.com/r-lib/vctrs/blob/b63a233bd8b8d6511cf3f7d9182c359a3b8c3cab/R/register-s3.R |
| s3_register <- function(generic, class, method = NULL) { |
| stopifnot(is.character(generic), length(generic) == 1) |
| stopifnot(is.character(class), length(class) == 1) |
| |
| pieces <- strsplit(generic, "::")[[1]] |
| stopifnot(length(pieces) == 2) |
| package <- pieces[[1]] |
| generic <- pieces[[2]] |
| |
| caller <- parent.frame() |
| |
| get_method_env <- function() { |
| top <- topenv(caller) |
| if (isNamespace(top)) { |
| asNamespace(environmentName(top)) |
| } else { |
| caller |
| } |
| } |
| get_method <- function(method) { |
| if (is.null(method)) { |
| get(paste0(generic, ".", class), envir = get_method_env()) |
| } else { |
| method |
| } |
| } |
| |
| register <- function(...) { |
| envir <- asNamespace(package) |
| |
| # Refresh the method each time, it might have been updated by |
| # `devtools::load_all()` |
| method_fn <- get_method(method) |
| stopifnot(is.function(method_fn)) |
| |
| |
| # Only register if generic can be accessed |
| if (exists(generic, envir)) { |
| registerS3method(generic, class, method_fn, envir = envir) |
| } else if (identical(Sys.getenv("NOT_CRAN"), "true")) { |
| warn <- .rlang_s3_register_compat("warn") |
| |
| warn(c( |
| sprintf( |
| "Can't find generic `%s` in package %s to register S3 method.", |
| generic, |
| package |
| ), |
| "i" = "This message is only shown to developers using devtools.", |
| "i" = sprintf("Do you need to update %s to the latest version?", package) |
| )) |
| } |
| } |
| |
| # Always register hook in case package is later unloaded & reloaded |
| setHook(packageEvent(package, "onLoad"), function(...) { |
| register() |
| }) |
| |
| # For compatibility with R < 4.0 where base isn't locked |
| is_sealed <- function(pkg) { |
| identical(pkg, "base") || environmentIsLocked(asNamespace(pkg)) |
| } |
| |
| # Avoid registration failures during loading (pkgload or regular). |
| # Check that environment is locked because the registering package |
| # might be a dependency of the package that exports the generic. In |
| # that case, the exports (and the generic) might not be populated |
| # yet (#1225). |
| if (isNamespaceLoaded(package) && is_sealed(package)) { |
| register() |
| } |
| |
| invisible() |
| } |