From 55c8c6199cbf2abf228ea97aa5758c27890fa76d Mon Sep 17 00:00:00 2001 From: shikokuchuo <53399081+shikokuchuo@users.noreply.github.com> Date: Thu, 12 Sep 2024 13:29:44 +0100 Subject: [PATCH] only load RcppSimdJson if used --- NAMESPACE | 1 - NEWS.md | 4 ++++ R/ichimoku-package.R | 2 +- src/shikokuchuo.c | 10 +++++++++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index ac5f81ae..ed3c7c91 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -46,7 +46,6 @@ export(strat) export(stratcombine) export(tradingDays) export(xts_df) -importFrom(RcppSimdJson,is_valid_json) importFrom(ggplot2,"%+replace%") importFrom(ggplot2,GeomCol) importFrom(ggplot2,GeomLine) diff --git a/NEWS.md b/NEWS.md index 8be92332..a91a079f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,10 @@ * `oanda_studio()` adds option 'multi.session' to facilitate multiple sessions to be used with the Shiny app (for example more than one browser page). +#### Updates: + +* Optimizes package load efficiency with `mirai` and `RcppSimdJson` only loaded the first time they are used. + # ichimoku 1.5.4 #### Updates: diff --git a/R/ichimoku-package.R b/R/ichimoku-package.R index 67d80fac..5affc794 100644 --- a/R/ichimoku-package.R +++ b/R/ichimoku-package.R @@ -83,7 +83,6 @@ #' scale_x_continuous scale_y_continuous Stat StatIdentity theme theme_grey #' %+replace% #' @importFrom nanonext ncurl -#' @importFrom RcppSimdJson is_valid_json #' @importFrom shiny checkboxInput column downloadButton downloadHandler HTML #' fillPage fluidPage fluidRow hoverOpts invalidateLater isolate #' numericInput observeEvent plotOutput reactive reactiveVal renderPlot @@ -117,6 +116,7 @@ if (i %in% c(1:3, 11:13)) Sys.sleep(0.03) else Sys.sleep(0.08) } cat("\n") + RcppSimdJson::is_valid_json("") invisible(quote(expr=)) } diff --git a/src/shikokuchuo.c b/src/shikokuchuo.c index d6d89d5b..7d2e488a 100644 --- a/src/shikokuchuo.c +++ b/src/shikokuchuo.c @@ -340,6 +340,14 @@ SEXP _naomit(SEXP x) { // imports from the package 'RcppSimdJson' SEXP _deserialize_json(SEXP json, SEXP query) { + if (jsofun == NULL) { + SEXP str, call; + PROTECT(str = Rf_mkString("RcppSimdJson")); + PROTECT(call = Rf_lang2(Rf_install("loadNamespace"), str)); + Rf_eval(call, R_BaseEnv); + UNPROTECT(2); + jsofun = (SEXP (*)(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP)) R_GetCCallable("RcppSimdJson", "_RcppSimdJson_.deserialize_json"); + } return jsofun(json, query, R_NilValue, R_NilValue, R_NilValue, ichimoku_false, R_NilValue, ichimoku_false, R_NilValue, ichimoku_int_three, ichimoku_int_zero, ichimoku_int_zero); } @@ -352,7 +360,7 @@ static void RegisterSymbols(void) { ichimoku_PeriodicitySymbol = Rf_install("periodicity"); ichimoku_TickerSymbol = Rf_install("ticker"); naofun = (SEXP (*)(SEXP)) R_GetCCallable("xts", "na_omit_xts"); - jsofun = (SEXP (*)(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP)) R_GetCCallable("RcppSimdJson", "_RcppSimdJson_.deserialize_json"); + jsofun = NULL; } static void PreserveObjects(void) {