TommyNgx commited on
Commit
59c72f0
·
verified ·
1 Parent(s): b0c7d09

Update load_lib.R

Browse files
Files changed (1) hide show
  1. load_lib.R +59 -40
load_lib.R CHANGED
@@ -21,68 +21,87 @@ save_r_lib_zip <- function(zip_out = "/content/R_library.zip",
21
  # 2) LOAD: Unzip rồi dán đè packages vào R library hiện tại
22
  # =========================
23
  load_r_lib_zip <- function(zip_path,
24
- target_lib = .libPaths()[1],
25
- backup = TRUE,
 
26
  backup_dir = "/content",
 
 
27
  overwrite = TRUE,
28
  quiet = FALSE) {
29
 
30
  if (!file.exists(zip_path)) stop("Zip file not found: ", zip_path)
31
- dir.create(target_lib, recursive = TRUE, showWarnings = FALSE)
32
 
33
- if (!quiet) {
34
- message("ZIP: ", zip_path)
35
- message("Target R lib: ", target_lib)
36
- }
37
-
38
- # (optional) backup thư mục library hiện tại
39
  backup_tar <- NULL
40
- if (backup) {
 
41
  backup_tar <- file.path(
42
  backup_dir,
43
  sprintf("R_lib_backup_%s.tar.gz", format(Sys.time(), "%Y%m%d_%H%M%S"))
44
  )
45
- old <- getwd()
46
- on.exit(setwd(old), add = TRUE)
47
  setwd(dirname(target_lib))
48
  utils::tar(backup_tar, files = basename(target_lib), compression = "gzip")
49
  if (!quiet) message("🗄️ Backup saved: ", backup_tar)
50
  }
51
 
52
- # unzip ra folder tạm
53
- tmp <- tempfile("Rlib_unzip_")
54
- dir.create(tmp)
55
- utils::unzip(zipfile = zip_path, exdir = tmp, overwrite = overwrite)
56
-
57
- # tìm các folder package ( DESCRIPTION)
58
- desc_files <- list.files(tmp, pattern = "^DESCRIPTION$", recursive = TRUE, full.names = TRUE)
59
- pkg_dirs <- unique(dirname(desc_files))
60
- if (length(pkg_dirs) == 0) stop("Không tìm thấy package nào (không thấy file DESCRIPTION) trong zip.")
61
-
62
- if (!quiet) message("Found packages: ", length(pkg_dirs))
63
-
64
- # copy đè từng package
65
- copied <- 0L
66
- for (src in pkg_dirs) {
67
- pkg <- basename(src)
68
- dest <- file.path(target_lib, pkg)
69
-
70
- # xóa bản cũ để tránh sót file
71
- system2("bash", c("-lc", paste("rm -rf", shQuote(dest))))
72
- # copy bản mới vào target_lib
73
- system2("bash", c("-lc", paste("cp -a", shQuote(src), shQuote(target_lib))))
74
- copied <- copied + 1L
75
- }
76
 
77
  if (!quiet) {
78
- message(" Done. Overwritten packages: ", copied)
79
- message("Library paths now:"); print(.libPaths())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  }
81
 
82
  invisible(list(
83
  zip_path = zip_path,
84
- target_lib = target_lib,
85
- overwritten = copied,
 
 
 
 
86
  backup_tar = backup_tar
87
  ))
88
  }
 
21
  # 2) LOAD: Unzip rồi dán đè packages vào R library hiện tại
22
  # =========================
23
  load_r_lib_zip <- function(zip_path,
24
+ unpack_root = "/content/R_site_library_unpacked",
25
+ add_to_libpaths = TRUE,
26
+ backup = FALSE,
27
  backup_dir = "/content",
28
+ sync_to_target = FALSE,
29
+ target_lib = "/content/R_site_library",
30
  overwrite = TRUE,
31
  quiet = FALSE) {
32
 
33
  if (!file.exists(zip_path)) stop("Zip file not found: ", zip_path)
 
34
 
35
+ # B1) (optional) backup target_lib nếu bạn sync đè vào đó
 
 
 
 
 
36
  backup_tar <- NULL
37
+ if (backup && sync_to_target) {
38
+ dir.create(target_lib, recursive = TRUE, showWarnings = FALSE)
39
  backup_tar <- file.path(
40
  backup_dir,
41
  sprintf("R_lib_backup_%s.tar.gz", format(Sys.time(), "%Y%m%d_%H%M%S"))
42
  )
43
+ old <- getwd(); on.exit(setwd(old), add = TRUE)
 
44
  setwd(dirname(target_lib))
45
  utils::tar(backup_tar, files = basename(target_lib), compression = "gzip")
46
  if (!quiet) message("🗄️ Backup saved: ", backup_tar)
47
  }
48
 
49
+ # B2) Unpack zip
50
+ unlink(unpack_root, recursive = TRUE, force = TRUE)
51
+ dir.create(unpack_root, recursive = TRUE, showWarnings = FALSE)
52
+ utils::unzip(zipfile = zip_path, exdir = unpack_root, overwrite = overwrite)
53
+
54
+ # B3) Auto-detect best lib dir (chứa nhiều package nhất)
55
+ desc_files <- list.files(unpack_root, pattern = "^DESCRIPTION$", recursive = TRUE, full.names = TRUE)
56
+ if (length(desc_files) == 0) stop("Không thấy DESCRIPTION trong zip -> không phải thư viện R chuẩn.")
57
+
58
+ pkg_dirs <- unique(dirname(desc_files)) # .../<pkg>
59
+ lib_dirs <- unique(dirname(pkg_dirs)) # .../<lib> (vd .../site-library)
60
+
61
+ pkg_count <- sapply(lib_dirs, function(d) length(list.dirs(d, recursive = FALSE, full.names = TRUE)))
62
+ best_lib <- lib_dirs[which.max(pkg_count)]
 
 
 
 
 
 
 
 
 
 
63
 
64
  if (!quiet) {
65
+ message("Best lib dir = ", best_lib)
66
+ message("Packages in best lib = ", max(pkg_count))
67
+ }
68
+
69
+ # B4) Add best_lib vào .libPaths()
70
+ if (add_to_libpaths) {
71
+ .libPaths(c(best_lib, .libPaths()))
72
+ if (!quiet) { message("Library paths now:"); print(.libPaths()) }
73
+ }
74
+
75
+ # B5) (optional) Sync/copy đè best_lib -> target_lib (writable)
76
+ overwritten <- NA_integer_
77
+ if (sync_to_target) {
78
+ dir.create(target_lib, recursive = TRUE, showWarnings = FALSE)
79
+
80
+ # copy từng package folder
81
+ pkgs <- list.dirs(best_lib, recursive = FALSE, full.names = TRUE)
82
+ pkgs <- pkgs[file.exists(file.path(pkgs, "DESCRIPTION"))]
83
+
84
+ for (src in pkgs) {
85
+ pkg <- basename(src)
86
+ dest <- file.path(target_lib, pkg)
87
+ unlink(dest, recursive = TRUE, force = TRUE)
88
+ system2("bash", c("-lc", paste("cp -a", shQuote(src), shQuote(target_lib))))
89
+ }
90
+ overwritten <- length(pkgs)
91
+
92
+ # ưu tiên target_lib nếu bạn muốn dùng nó thay vì best_lib
93
+ .libPaths(c(target_lib, .libPaths()))
94
+ if (!quiet) message("✅ Synced packages to target_lib: ", target_lib, " (", overwritten, " pkgs)")
95
  }
96
 
97
  invisible(list(
98
  zip_path = zip_path,
99
+ unpack_root = unpack_root,
100
+ best_lib = best_lib,
101
+ added_to_libpaths = add_to_libpaths,
102
+ synced = sync_to_target,
103
+ target_lib = if (sync_to_target) target_lib else NULL,
104
+ overwritten = overwritten,
105
  backup_tar = backup_tar
106
  ))
107
  }