Skip to content

Commit

Permalink
Merge pull request #271 from davidycliao/0.1.7
Browse files Browse the repository at this point in the history
Update zzz.R
  • Loading branch information
davidycliao authored Jan 6, 2025
2 parents a5fad87 + 6ec1d82 commit ce72144
Showing 1 changed file with 15 additions and 115 deletions.
130 changes: 15 additions & 115 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,173 +19,73 @@
#' @importFrom reticulate py_module_available
#' @importFrom reticulate py_install
#' @keywords internal
# .onAttach <- function(...) {
# # Determine Python command
# python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3"
# python_path <- Sys.which(python_cmd)
#
# # Check Python path
# if (python_path == "") {
# packageStartupMessage(paste("Cannot locate the", python_cmd, "executable. Ensure it's installed and in your system's PATH. flaiR functionality requiring Python will not be available."))
# return(invisible(NULL)) # Exit .onAttach without stopping package loading
# }
#
# # Check Python versio Try to get Python version
# tryCatch({
# python_version <- system(paste(python_path, "--version"), intern = TRUE)
# if (!grepl("Python 3", python_version)) {
# packageStartupMessage("Python 3 is required, but a different version was found. Please install Python 3. flaiR functionality requiring Python will not be available.")
# return(invisible(NULL)) # Exit .onAttach without stopping package loading
# }
# }, error = function(e) {
# packageStartupMessage(paste("Failed to get Python version with path:", python_path, "Error:", e$message, ". flaiR functionality requiring Python will not be available."))
# return(invisible(NULL)) # Exit .onAttach without stopping package loading
# })
#
# # Check if PyTorch is installed
# check_torch_version <- function() {
# # torch_version_command <- paste(python_path, "-c 'import torch; print(torch.__version__)'")
# torch_version_command <- paste(python_path, "-c \"import torch; print(torch.__version__)\"")
# result <- system(torch_version_command, intern = TRUE)
# if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) {
# return(list(paste("PyTorch", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE))
# }
# # Return flair version
# return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m") ,result[1], sep = " "), TRUE, result[1]))
# }
#
# # Check if flair is installed
# check_flair_version <- function() {
# # flair_version_command <- paste(python_path, "-c 'import flair; print(flair.__version__)'")
# flair_version_command <- paste(python_path, "-c \"import flair; print(flair.__version__)\"")
# result <- system(flair_version_command, intern = TRUE)
# if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) {
# return(list(paste("flair", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE))
# }
# # Return flair version
# return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"),result[1], sep = " "), TRUE, result[1]))
# }
#
# flair_version <- check_flair_version()
# torch_version <- check_torch_version()
#
# if (isFALSE(flair_version[[2]])) {
# packageStartupMessage(sprintf(" Flair %-50s", paste0("is installing from Python")))
#
# commands <- c(
# paste(python_path, "-m pip install --upgrade pip"),
# paste(python_path, "-m pip install torch"),
# paste(python_path, "-m pip install flair"),
# paste(python_path, "-m pip install scipy==1.12.0")
# )
# command_statuses <- vapply(commands, system, FUN.VALUE = integer(1))
#
# flair_check_again <- check_flair_version()
# if (isFALSE(flair_check_again[[2]])) {
# packageStartupMessage("Failed to install Flair. {flaiR} requires Flair NLP. Please ensure Flair NLP is installed in Python manually.")
# }
# } else {
# packageStartupMessage(sprintf("\033[1m\033[34mflaiR\033[39m\033[22m: \033[1m\033[33mAn R Wrapper for Accessing Flair NLP\033[39m\033[22m %-5s", paste("\033[1m\033[33m", flair_version[[3]], "\033[39m\033[22m", sep = "")))
# }
# }

.onAttach <- function(...) {
# Clear any existing RETICULATE_PYTHON setting first
Sys.unsetenv("RETICULATE_PYTHON")

# Determine Python command
python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3"
python_path <- Sys.which(python_cmd)

# If python_path is empty, try to find Python from common locations
if (python_path == "") {
common_paths <- c(
"/usr/bin/python3",
"/usr/local/bin/python3",
Sys.getenv("CONDA_PYTHON_EXE"), # Check for conda environment
file.path(Sys.getenv("USERPROFILE"), "Anaconda3", "python.exe"), # Windows Anaconda
file.path(Sys.getenv("HOME"), "anaconda3", "bin", "python") # Unix Anaconda
)

for (path in common_paths) {
if (file.exists(path)) {
python_path <- path
break
}
}
}

# Check Python path
if (python_path == "") {
packageStartupMessage(paste("Cannot locate the", python_cmd, "executable. Ensure it's installed and in your system's PATH. flaiR functionality requiring Python will not be available."))
return(invisible(NULL))
return(invisible(NULL)) # Exit .onAttach without stopping package loading
}

# Set RETICULATE_PYTHON to the found Python path
Sys.setenv(RETICULATE_PYTHON = python_path)

# Try to initialize reticulate with the found Python
tryCatch({
if (!requireNamespace("reticulate", quietly = TRUE)) {
install.packages("reticulate")
}
reticulate::use_python(python_path, required = TRUE)
}, error = function(e) {
packageStartupMessage(paste("Failed to initialize Python environment:", e$message))
return(invisible(NULL))
})

# Check Python version
# Check Python versio Try to get Python version
tryCatch({
python_version <- system(paste(python_path, "--version"), intern = TRUE)
if (!grepl("Python 3", python_version)) {
packageStartupMessage("Python 3 is required, but a different version was found. Please install Python 3. flaiR functionality requiring Python will not be available.")
return(invisible(NULL))
return(invisible(NULL)) # Exit .onAttach without stopping package loading
}
}, error = function(e) {
packageStartupMessage(paste("Failed to get Python version with path:", python_path, "Error:", e$message, ". flaiR functionality requiring Python will not be available."))
return(invisible(NULL))
return(invisible(NULL)) # Exit .onAttach without stopping package loading
})

# Version check functions
# Check if PyTorch is installed
check_torch_version <- function() {
# torch_version_command <- paste(python_path, "-c 'import torch; print(torch.__version__)'")
torch_version_command <- paste(python_path, "-c \"import torch; print(torch.__version__)\"")
result <- system(torch_version_command, intern = TRUE)
if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) {
return(list(paste("PyTorch", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE))
}
return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m"), result[1], sep = " "), TRUE, result[1]))
# Return flair version
return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m") ,result[1], sep = " "), TRUE, result[1]))
}

# Check if flair is installed
check_flair_version <- function() {
# flair_version_command <- paste(python_path, "-c 'import flair; print(flair.__version__)'")
flair_version_command <- paste(python_path, "-c \"import flair; print(flair.__version__)\"")
result <- system(flair_version_command, intern = TRUE)
if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) {
return(list(paste("flair", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE))
}
return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"), result[1], sep = " "), TRUE, result[1]))
# Return flair version
return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"),result[1], sep = " "), TRUE, result[1]))
}

# Check versions and install if needed
flair_version <- check_flair_version()
torch_version <- check_torch_version()

if (isFALSE(flair_version[[2]])) {
packageStartupMessage(sprintf(" Flair %-50s", paste0("is installing from Python")))

commands <- c(
paste(python_path, "-m pip install --upgrade pip"),
paste(python_path, "-m pip install torch"),
paste(python_path, "-m pip install flair"),
paste(python_path, "-m pip install scipy==1.12.0")
)
command_statuses <- vapply(commands, system, FUN.VALUE = integer(1))

flair_check_again <- check_flair_version()
if (isFALSE(flair_check_again[[2]])) {
packageStartupMessage("Failed to install Flair. {flaiR} requires Flair NLP. Please ensure Flair NLP is installed in Python manually.")
}
} else {
packageStartupMessage(sprintf("\033[1m\033[34mflaiR\033[39m\033[22m: \033[1m\033[33mAn R Wrapper for Accessing Flair NLP\033[39m\033[22m %-5s",
paste("\033[1m\033[33m", flair_version[[3]], "\033[39m\033[22m", sep = "")))
packageStartupMessage(sprintf("\033[1m\033[34mflaiR\033[39m\033[22m: \033[1m\033[33mAn R Wrapper for Accessing Flair NLP\033[39m\033[22m %-5s", paste("\033[1m\033[33m", flair_version[[3]], "\033[39m\033[22m", sep = "")))
}
}

Expand Down

0 comments on commit ce72144

Please sign in to comment.