Skip to content

Commit ce72144

Browse files
authored
Merge pull request #271 from davidycliao/0.1.7
Update zzz.R
2 parents a5fad87 + 6ec1d82 commit ce72144

File tree

1 file changed

+15
-115
lines changed

1 file changed

+15
-115
lines changed

R/zzz.R

Lines changed: 15 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -19,173 +19,73 @@
1919
#' @importFrom reticulate py_module_available
2020
#' @importFrom reticulate py_install
2121
#' @keywords internal
22-
# .onAttach <- function(...) {
23-
# # Determine Python command
24-
# python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3"
25-
# python_path <- Sys.which(python_cmd)
26-
#
27-
# # Check Python path
28-
# if (python_path == "") {
29-
# 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."))
30-
# return(invisible(NULL)) # Exit .onAttach without stopping package loading
31-
# }
32-
#
33-
# # Check Python versio Try to get Python version
34-
# tryCatch({
35-
# python_version <- system(paste(python_path, "--version"), intern = TRUE)
36-
# if (!grepl("Python 3", python_version)) {
37-
# packageStartupMessage("Python 3 is required, but a different version was found. Please install Python 3. flaiR functionality requiring Python will not be available.")
38-
# return(invisible(NULL)) # Exit .onAttach without stopping package loading
39-
# }
40-
# }, error = function(e) {
41-
# packageStartupMessage(paste("Failed to get Python version with path:", python_path, "Error:", e$message, ". flaiR functionality requiring Python will not be available."))
42-
# return(invisible(NULL)) # Exit .onAttach without stopping package loading
43-
# })
44-
#
45-
# # Check if PyTorch is installed
46-
# check_torch_version <- function() {
47-
# # torch_version_command <- paste(python_path, "-c 'import torch; print(torch.__version__)'")
48-
# torch_version_command <- paste(python_path, "-c \"import torch; print(torch.__version__)\"")
49-
# result <- system(torch_version_command, intern = TRUE)
50-
# if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) {
51-
# return(list(paste("PyTorch", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE))
52-
# }
53-
# # Return flair version
54-
# return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m") ,result[1], sep = " "), TRUE, result[1]))
55-
# }
56-
#
57-
# # Check if flair is installed
58-
# check_flair_version <- function() {
59-
# # flair_version_command <- paste(python_path, "-c 'import flair; print(flair.__version__)'")
60-
# flair_version_command <- paste(python_path, "-c \"import flair; print(flair.__version__)\"")
61-
# result <- system(flair_version_command, intern = TRUE)
62-
# if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) {
63-
# return(list(paste("flair", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE))
64-
# }
65-
# # Return flair version
66-
# return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"),result[1], sep = " "), TRUE, result[1]))
67-
# }
68-
#
69-
# flair_version <- check_flair_version()
70-
# torch_version <- check_torch_version()
71-
#
72-
# if (isFALSE(flair_version[[2]])) {
73-
# packageStartupMessage(sprintf(" Flair %-50s", paste0("is installing from Python")))
74-
#
75-
# commands <- c(
76-
# paste(python_path, "-m pip install --upgrade pip"),
77-
# paste(python_path, "-m pip install torch"),
78-
# paste(python_path, "-m pip install flair"),
79-
# paste(python_path, "-m pip install scipy==1.12.0")
80-
# )
81-
# command_statuses <- vapply(commands, system, FUN.VALUE = integer(1))
82-
#
83-
# flair_check_again <- check_flair_version()
84-
# if (isFALSE(flair_check_again[[2]])) {
85-
# packageStartupMessage("Failed to install Flair. {flaiR} requires Flair NLP. Please ensure Flair NLP is installed in Python manually.")
86-
# }
87-
# } else {
88-
# 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 = "")))
89-
# }
90-
# }
91-
9222
.onAttach <- function(...) {
93-
# Clear any existing RETICULATE_PYTHON setting first
94-
Sys.unsetenv("RETICULATE_PYTHON")
95-
9623
# Determine Python command
9724
python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3"
9825
python_path <- Sys.which(python_cmd)
9926

100-
# If python_path is empty, try to find Python from common locations
101-
if (python_path == "") {
102-
common_paths <- c(
103-
"/usr/bin/python3",
104-
"/usr/local/bin/python3",
105-
Sys.getenv("CONDA_PYTHON_EXE"), # Check for conda environment
106-
file.path(Sys.getenv("USERPROFILE"), "Anaconda3", "python.exe"), # Windows Anaconda
107-
file.path(Sys.getenv("HOME"), "anaconda3", "bin", "python") # Unix Anaconda
108-
)
109-
110-
for (path in common_paths) {
111-
if (file.exists(path)) {
112-
python_path <- path
113-
break
114-
}
115-
}
116-
}
117-
11827
# Check Python path
11928
if (python_path == "") {
12029
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."))
121-
return(invisible(NULL))
30+
return(invisible(NULL)) # Exit .onAttach without stopping package loading
12231
}
12332

124-
# Set RETICULATE_PYTHON to the found Python path
125-
Sys.setenv(RETICULATE_PYTHON = python_path)
126-
127-
# Try to initialize reticulate with the found Python
128-
tryCatch({
129-
if (!requireNamespace("reticulate", quietly = TRUE)) {
130-
install.packages("reticulate")
131-
}
132-
reticulate::use_python(python_path, required = TRUE)
133-
}, error = function(e) {
134-
packageStartupMessage(paste("Failed to initialize Python environment:", e$message))
135-
return(invisible(NULL))
136-
})
137-
138-
# Check Python version
33+
# Check Python versio Try to get Python version
13934
tryCatch({
14035
python_version <- system(paste(python_path, "--version"), intern = TRUE)
14136
if (!grepl("Python 3", python_version)) {
14237
packageStartupMessage("Python 3 is required, but a different version was found. Please install Python 3. flaiR functionality requiring Python will not be available.")
143-
return(invisible(NULL))
38+
return(invisible(NULL)) # Exit .onAttach without stopping package loading
14439
}
14540
}, error = function(e) {
14641
packageStartupMessage(paste("Failed to get Python version with path:", python_path, "Error:", e$message, ". flaiR functionality requiring Python will not be available."))
147-
return(invisible(NULL))
42+
return(invisible(NULL)) # Exit .onAttach without stopping package loading
14843
})
14944

150-
# Version check functions
45+
# Check if PyTorch is installed
15146
check_torch_version <- function() {
47+
# torch_version_command <- paste(python_path, "-c 'import torch; print(torch.__version__)'")
15248
torch_version_command <- paste(python_path, "-c \"import torch; print(torch.__version__)\"")
15349
result <- system(torch_version_command, intern = TRUE)
15450
if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) {
15551
return(list(paste("PyTorch", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE))
15652
}
157-
return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m"), result[1], sep = " "), TRUE, result[1]))
53+
# Return flair version
54+
return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m") ,result[1], sep = " "), TRUE, result[1]))
15855
}
15956

57+
# Check if flair is installed
16058
check_flair_version <- function() {
59+
# flair_version_command <- paste(python_path, "-c 'import flair; print(flair.__version__)'")
16160
flair_version_command <- paste(python_path, "-c \"import flair; print(flair.__version__)\"")
16261
result <- system(flair_version_command, intern = TRUE)
16362
if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) {
16463
return(list(paste("flair", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE))
16564
}
166-
return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"), result[1], sep = " "), TRUE, result[1]))
65+
# Return flair version
66+
return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"),result[1], sep = " "), TRUE, result[1]))
16767
}
16868

169-
# Check versions and install if needed
17069
flair_version <- check_flair_version()
17170
torch_version <- check_torch_version()
17271

17372
if (isFALSE(flair_version[[2]])) {
17473
packageStartupMessage(sprintf(" Flair %-50s", paste0("is installing from Python")))
74+
17575
commands <- c(
17676
paste(python_path, "-m pip install --upgrade pip"),
17777
paste(python_path, "-m pip install torch"),
17878
paste(python_path, "-m pip install flair"),
17979
paste(python_path, "-m pip install scipy==1.12.0")
18080
)
18181
command_statuses <- vapply(commands, system, FUN.VALUE = integer(1))
82+
18283
flair_check_again <- check_flair_version()
18384
if (isFALSE(flair_check_again[[2]])) {
18485
packageStartupMessage("Failed to install Flair. {flaiR} requires Flair NLP. Please ensure Flair NLP is installed in Python manually.")
18586
}
18687
} else {
187-
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",
188-
paste("\033[1m\033[33m", flair_version[[3]], "\033[39m\033[22m", sep = "")))
88+
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 = "")))
18989
}
19090
}
19191

0 commit comments

Comments
 (0)