Skip to content

Instantly share code, notes, and snippets.

@philipp-baumann
Last active January 6, 2024 19:09
Show Gist options
  • Save philipp-baumann/35ced36eb1596c1340b3f7977c74d920 to your computer and use it in GitHub Desktop.
Save philipp-baumann/35ced36eb1596c1340b3f7977c74d920 to your computer and use it in GitHub Desktop.
Testing new {rix} with_nix()
# install_github("b-rodrigues/rix", ref = "71-with-nix")
library("rix")
library("dplyr")
library("data.table")
# fusen::load_flat_functions(flat_file = "dev/build_envs.Rmd")
# fusen::load_flat_functions(flat_file = "dev/get_os.Rmd")
library("rix")
rix(r_ver = "latest", r_pkgs = c("dplyr", "data.table"), overwrite = TRUE)
# for RStudio
old_path <- Sys.getenv("PATH")
Sys.setenv(PATH = paste(old_path, "/nix/var/nix/profiles/default/bin",
sep = ":"))
# main function to run
nrow_mtcars <- function(m = mtcars) {
a <- m |> filter(cyl == 6L)
stopifnot(test_df(a))
cat("Object outside of function scope is of class: ", class(env_a))
nrow_impl(a)
}
nrow_impl <- function(a) {
not_returned <- data.table(a = 1:3)
# `not_used` below should produce code check warning
cat("`not_returned`", "has", nrow(not_returned), "rows.")
df_check(a)
nrow(a)
}
env_a <- new.env(parent = baseenv()) # enclosure package:base
env_a$key <- "value"
df_check <- function(x) stopifnot("x must be data.frame" = is_df(x))
# that does not work both in host an nix R session, which is expected,
# since function is in an environment
env_a$df_check <- function(x) stopifnot("x must be data.frame" = is_df(x))
# no downstream
is_df <- function(x) is.data.frame(x)
test_df <- function(a) is_df_a(a)
is_df_a <- function(a) is.data.frame(a)
out_nix <- with_nix(
expr = nrow_mtcars,
exec_mode = "non-blocking",
project_path = "."
)
out_session <- nrow_mtcars(m = mtcars)
identical(
out_session,
out_nix
)
my_print <- function(mtcars) print(mtcars)
myprint_nix <- with_nix(
expr = my_print,
exec_mode = "non-blocking",
project_path = "."
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment