##-------------------------------------------------------------
##
##-------------------------------------------------------------
update_pac = function(
lib.loc = NULL,
repos = getOption("bibliotheca.repos"),
verbose = TRUE,
menu = TRUE)
{
#Installed=Package=ReposVer=pk=.N=`:=`=NULL
libs = .Library.site
libp = .libPaths()
if(is.null(repos)) repos = "https://cran.fiocruz.br/"
if(is.null(lib.loc)) lib.loc = libp[!libp %in% libs]
#lib.usr = libp[libp %in% libs]
##
if(is.null(old.packages(lib.loc = lib.loc, repos = repos)))
{
if(verbose) mensagem( aviso = "Update", subtitulo = "All packages are up to date", msg=5)
return(invisible(NULL))
}#end if
DF = data.frame(old.packages(lib.loc = lib.loc, repos = repos), stringsAsFactors = FALSE)
N = nrow(DF)
if(.Platform$OS.type == "unix"){
DF$pk = paste0("[", 1:N, "]", " ", DF$Package, " ",
DF$Installed," \u2500\u25BA ", DF$ReposVer)
} else {
DF$pk = paste0("[", 1:N, "]", " ", DF$Package, " ",
DF$Installed," -> ", DF$ReposVer)
}#end if
if(menu)
{
mensagem(DF$pk, msg = 1, aviso = "Update - CRAN", simb="none")
switch(menu(c("Select the update", "Update all", "Exit")) + 1,
cat("Nothing done\n"),
up_selection(DF, repos = repos, verbose = verbose),
up_all(DF, repos = repos, verbose = verbose),
cat("Nothing done\n"))
} else {
DF$pk = NULL
return(DF)
}#end if
}#end function update_pac
#update_pac(menu = T)
up_selection = function(DF, repos, verbose = TRUE)
{
#Package=DF=.N=NULL
pinguim = readRDS(system.file('asciiart/pinguim.rds', package = 'bibliotheca'))
#pac = DF[,1]
n = nrow(DF)
message(sprintf(pinguim, paste(" Insert a number between 1 e", n,
"\n or package name",
"\n Separate with comma or space")))
mensagem(DF$pk, msg = 1, aviso = "Update", simb="none")
## leitura do console
np = read_console(n)
#if(!length(unique(np))) print(1)#return(NULL)
## seleciona nomes dos pacotes
pkgs.up = DF$Package
names(pkgs.up) <- pkgs.up
pkgs.up = pkgs.up[np]
## remove NA
pkgs.up = pkgs.up[!is.na(pkgs.up)]
pk = DF[pkgs.up,][,"pk"]
## remove NA
pk = pk[!is.na(pk)]
if(all(is.na(pk)))
{
return()
} else {
## atualiza pacote
#pkgs.up = c("RGtk2", "ggmap")[2]
unload_pac(pkgs.up, verbose=FALSE)
pki = install_pac(pkgs.up, repos=repos, supress = TRUE, verbose=FALSE)
## seleciona pacotes atualizados
pg = DF[DF[,"Package"] %in% pki[[1]],]["pk"][,1]
#pg = "ggmap"
## message with name and version
if(verbose) mensagem(pg, msg = 1, aviso = "Packages updated - CRAN", simb="check3")
DF$up = ""
if(!is.null(pki[[1]])) DF[DF$Package %in% pki[[1]],]$up = "yes"
DF$pk = NULL
return(invisible(DF))
}#end if
}#end function up_selection
up_all = function(DF, repos, verbose = TRUE)
{
Package = NULL
## updatable packages
pkgs.up = DF[,"Package"]
unload_pac(pkgs.up, verbose=FALSE)
## install packages
pki = install_pac(pkgs.up, repos=repos, supress = TRUE, verbose=FALSE)
## only installed packages
pg = DF[DF[,1] %in% pki[[1]],]["pk"][,1]
## message with name and version
if(verbose) mensagem(pg, msg = 1, aviso = "Packages updated - CRAN", simb="check3")
DF$up = ""
if(!is.null(pki[[1]])) DF[DF[, "Package"] %in% pki[[1]],]$up = "yes"
DF$pk = NULL
return(invisible(DF))
}#end up_all
read_console = function(n)
{
stri = readline(prompt = crayon::yellow("Pakcages:"))
a = gsub(" +", " ", stri)
b = gsub(",", " ", a)
c = gsub(" +", " ", b)
stri = gsub(" ", ",", c)
if(!grepl("[a-z|A-Z]", stri))
{
npac = 1:n
x = "np"
y = paste0("npac[c(", stri, ")]")
out = unique(eval(parse(text = paste(x, "=", y))))
out = out[!is.na(out)]
} else {
out = unlist(strsplit(stri, split = ","))
#return(out)
}
return(out)
}#end funtion console
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.