Skip to content

Instantly share code, notes, and snippets.

@gmp26
Created April 5, 2022 11:44
Show Gist options
  • Save gmp26/b859872eeb53c753ece08c7e632f7b02 to your computer and use it in GitHub Desktop.
Save gmp26/b859872eeb53c753ece08c7e632f7b02 to your computer and use it in GitHub Desktop.
Problems calling R attach from bb in certain contexts

I now have one failing case and three passing cases...

For the failing case, start test.clj in a Calva Babashka REPL. This is how I was testing...

clj꞉test꞉> 
; Evaluating file: test.clj
{:exit 1, :out "", :err "Error in print(tx_sex_m) : object 'tx_sex_m' not found\nExecution halted\n"}

This is the issue I was reporting

Without bb this is what to expect:

$ Rscript adjcox.R
[1] 0

Here are the passing cases: From the bb terminal repl

$ bb
...
user=> (babashka.process/sh ["ls"])
{:proc #object[java.lang.ProcessImpl 0x25c0c982 "Process[pid=21366, exitValue=0]"], :exit 0, :in #object[java.lang.ProcessImpl$ProcessPipeOutputStream 0xb048e81 "java.lang.ProcessImpl$ProcessPipeOutputStream@b048e81"], :out "adjcox.R\nadjcox.Rout\nfoo.txt\nparams.csv\n", :err "", :prev nil, :cmd ["ls"]}
user=> (babashka.process/sh ["Rscript" "adjcox.R"])
{:proc #object[java.lang.ProcessImpl 0x9d1d8c3 "Process[pid=21750, exitValue=0]"], :exit 0, :in #object[java.lang.ProcessImpl$ProcessPipeOutputStream 0x185eca3d "java.lang.ProcessImpl$ProcessPipeOutputStream@185eca3d"], :out "[1] 0\n", :err "", :prev nil, :cmd ["Rscript" "adjcox.R"]}

as expected

Also surprisingly

bb test.clj
{:exit 0, :out "[1] 0\n", :err ""}

as expected.

Also (even more surprisingly) from the babashka terminal repl:

user=> (babashka.process/sh ["Rscript" "adjcox.R"])
{:proc #object[java.lang.ProcessImpl 0x9d1d8c3 "Process[pid=21750, exitValue=0]"], :exit 0, :in #object[java.lang.ProcessImpl$ProcessPipeOutputStream 0x185eca3d "java.lang.ProcessImpl$ProcessPipeOutputStream@185eca3d"], :out "[1] 0\n", :err "", :prev nil, :cmd ["Rscript" "adjcox.R"]}

as expected.

So we are only seeing the issue in the Calva REPL.

# Run from the R source directory
# params.csv should be in the same directory
param <- read.csv("params.csv") # In RStudio, param will appear in the environment
attach(param)
print(tx_sex_m) # should return "[1] 0"
tx_sex_m tx_sex_f tx_age_2 tx_age_3 tx_age_4 tx_age_5 tx_age_6 tx_age_7 tx_eth_wh tx_eth_nonwh tx_bld_o tx_bld_a tx_bld_b tx_bld_ab tx_match_1 tx_match_2 tx_match_3 tx_graft_0 tx_graft_1 tx_dialysis_1 tx_dialysis_2 tx_sens_1 tx_sens_2 tx_diabetes_1 tx_diabetes_2 rem_sex_m rem_sex_f rem_age_2 rem_age_3 rem_age_4 rem_age_5 rem_age_6 rem_age_7 rem_eth_wh rem_eth_nonwh rem_bld_o rem_bld_a rem_bld_b rem_bld_ab rem_match_1 rem_match_2 rem_match_3 rem_graft_0 rem_graft_1 rem_dialysis_1 rem_dialysis_2 rem_sens_1 rem_sens_2 rem_diabetes_1 rem_diabetes_2 dth_sex_m dth_sex_f dth_age_2 dth_age_3 dth_age_4 dth_age_5 dth_age_6 dth_age_7 dth_eth_wh dth_eth_nonwh dth_bld_o dth_bld_a dth_bld_b dth_bld_ab dth_match_1 dth_match_2 dth_match_3 dth_graft_0 dth_graft_1 dth_dialysis_1 dth_dialysis_2 dth_sens_1 dth_sens_2 dth_diabetes_1 dth_diabetes_2
0 -0.08932 0.55643 0.41619 0.1883 0 -0.23215 -0.429 0 -0.05609 0 0.63663 -0.02221 0.63663 0 -0.32047 -0.68447 0 -0.42997 0 0.23561 0 -0.79469 0 -0.13378 0 -0.15065 -0.93236 -0.58193 -0.36876 0 0.55684 1.4442 0 -0.17259 0 0.26513 0.03992 0.26513 0 -0.16539 -0.39542 0 -0.17259 0 -0.22529 0 0.10413 0 0.23813 0 -0.13832 -1.49163 -0.86431 -0.43495 0 0.18012 0.25913 0 -0.26034 0 0.26473 0.08114 0.26473 0 0.07006 -0.07087 0 0.26165 0 -0.33903 0 0.09477 0 0.73486
(ns test
(:require [clojure.java.shell :refer [sh]]))
(sh "ls")
;; => {:exit 0, :out "adjcox.R\nadjcox.Rout\nfoo.txt\nparams.csv\ntest.clj\n", :err ""}
(sh "Rscript" "adjcox.R")
;; => {:exit 1, :out "", :err "Error in print(tx_sex_m) : object 'tx_sex_m' not found\nExecution halted\n"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment