Skip to content

Commit

Permalink
Merge pull request #26 from edvinskis/master
Browse files Browse the repository at this point in the history
Update zzz.R
  • Loading branch information
tsrobinson committed Aug 23, 2023
2 parents 814ed28 + 14880a6 commit 8c1aedf
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
custom_readline <- function(prompt, default = "skip") {
if (interactive()) {
return(readline(prompt))
} else {
return(default)
}
}

custom_writelines <- function(input, con) {
writeLines(input, con = con)
}

custom_packageStartupMessage <- function(msg) {
messages <- list(
msg1 = "rMIDAS is loaded. In headless mode its environment and dependencies need to be set up manually. Please read https://github.com/MIDASverse/rMIDAS for additional help on how to set up and configure your environment.",
msg2 = "If you want to change your Python environment use 'reset_rMIDAS_env()' to reset your configuration. After resetting, you'll need to restart the R session and run 'library(rMIDAS)' again.",
msg3 = "rMIDAS is loaded but its environment and dependencies are not set up automatically. Please read https://github.com/MIDASverse/rMIDAS for additional help on how to set up and configure your environment.",
msg4 = "rMIDAS is being set up automatically.",
msg5 = "rMIDAS is loaded. Please read https://github.com/MIDASverse/rMIDAS for additional help on how to set up and configure your environment. If you want to use a different Python environment use 'reset_rMIDAS_env()' to reset the your configuration. After resetting, you'll need to restart the R session and run 'library(rMIDAS)' again.",
msg6 = "rMIDAS has been automatically set up.",
msg7 = "rMIDAS is loaded. Please read https://github.com/MIDASverse/rMIDAS for additional help on how to set up and configure your environment. If you want to change your Python environment use 'reset_rMIDAS_env()' to reset your configuration. After resetting, you'll need to restart the R session and run 'library(rMIDAS)' again."
)
packageStartupMessage(messages[msg])
}

.onLoad <- function(libname, pkgname) {

options("python_custom" = NULL)
Expand All @@ -21,19 +46,19 @@
if (!file.exists(config_file)) {
if (!interactive()) {
user_input <- "n"
packageStartupMessage("rMIDAS is loaded. In headless mode its environment and dependencies need to be set up manually. Please read https://github.com/MIDASverse/rMIDAS for additional help on how to set up and configure your environment.")
packageStartupMessage("If you want to change your Python environment use 'reset_rMIDAS_env()' to reset your configuration. After resetting, you'll need to restart the R session and run 'library(rMIDAS)' again.")
custom_packageStartupMessage("msg1")
custom_packageStartupMessage("msg2")
} else {
user_input <- readline("Do you want rMIDAS to automatically set up a Python environment and its dependencies? Enter 'y' for Yes, or any other key for No : \n")
writeLines(user_input, con = config_file)
if(tolower(user_input) != "y") {packageStartupMessage("rMIDAS is loaded but its environment and dependencies are not set up automatically. Please read https://github.com/MIDASverse/rMIDAS for additional help on how to set up and configure your environment.")}
if(tolower(user_input) == "y") {packageStartupMessage("rMIDAS is being set up automatically.")}
user_input <- custom_readline("Do you want rMIDAS to automatically set up a Python environment and its dependencies? Enter 'y' for Yes, or any other key for No : \n", default = "n")
custom_writelines(user_input, con = config_file)
if(tolower(user_input) != "y") {custom_packageStartupMessage("msg3")}
if(tolower(user_input) == "y") {custom_packageStartupMessage("msg4")}
}
} else {
user_input <- readLines(con = config_file)[1]
if(tolower(user_input) != "y") {
packageStartupMessage("\n \n","rMIDAS is loaded. Please read https://github.com/MIDASverse/rMIDAS for additional help on how to set up and configure your environment. If you want to use a different Python environment use 'reset_rMIDAS_env()' to reset the your configuration. After resetting, you'll need to restart the R session and run 'library(rMIDAS)' again.\n")
writeLines(user_input, con = config_file)
custom_packageStartupMessage("msg5")
custom_writelines(user_input, con = config_file)
}
}

Expand Down Expand Up @@ -82,20 +107,18 @@ scipy
}

requirements_file <- tempfile(fileext = ".txt")
writeLines(requirements_txt, requirements_file)
custom_writelines(requirements_txt, requirements_file)

reticulate::py_install(envname = virtual_env_name, pip = TRUE, packages = c("-r", requirements_file))
reticulate::py_config()
reticulate::use_virtualenv(virtual_env_name, required = TRUE)
packageStartupMessage("rMIDAS has been automatically set up.")
#.rs.restartR()
custom_packageStartupMessage("msg6")
}
packageStartupMessage("rMIDAS is loaded. Please read https://github.com/MIDASverse/rMIDAS for additional help on how to set up and configure your environment. If you want to change your Python environment use 'reset_rMIDAS_env()' to reset your configuration. After resetting, you'll need to restart the R session and run 'library(rMIDAS)' again.")
custom_packageStartupMessage("msg7")
}
}

.onAttach <- function(libname, pkgname) {

packageStartupMessage("\n## \n",
"## rMIDAS: Multiple Imputation using Denoising Autoencoders \n",
"## Authors: Thomas Robinson and Ranjit Lall \n",
Expand All @@ -106,4 +129,4 @@ scipy

.onDetach <- function(libpath) {
# Nothing
}
}

0 comments on commit 8c1aedf

Please sign in to comment.