R/install_pac.R

##-------------------------------------------------------------
## 
##-------------------------------------------------------------
install_pac = function(pac,
                          repos = getOption("repos"),
                          type = getOption("pkgType"),
                          verbose = TRUE,
                          ...)
{
    tmp = sapply(pac, .install_pac_aux, repos=repos,
                 type=type, verbose=verbose, ..., USE.NAMES = FALSE)
    tmp = t(tmp)

    DF = as.data.frame(tmp)
    DF$Name = unlist(DF$Name)
    DF$Exist = unlist(DF$Exist)
    DF$Installed = unlist(DF$Installed)
    DF$Loaded = unlist(DF$Loaded)


    pk_inst = DF[DF["Installed"]==TRUE, ][,1]

	out = .aviso_cran(DF, verbose=verbose)

    return(invisible(list(Installed = out, Table = DF)))

}#end function install_pac


.aviso_cran = function(DF, verbose=TRUE)
{
	   pk_inst = DF[DF["Installed"]==TRUE, ][,1]

    if(length(pk_inst))
    {
        ver = unlist(lapply(pk_inst, function(x)as.character(packageVersion(x))))

        pkgok = paste(pk_inst, ver)

        if(verbose) mensagem(pkgok, msg = 1, aviso = "Installed packages - CRAN")

        out = pk_inst

    } else {
        out = NULL
    }#end if

    ## packages not installed
    pk_ni = DF[DF["Exist"]==TRUE & DF["Installed"]==FALSE, ][,1]

    if(length(pk_ni) & verbose) mensagem(pk_ni, msg = 3)

    ## packages not exist
    pk_ne = DF[DF["Exist"]==FALSE & DF["Installed"]==FALSE, ][,1]

    ## package is not available
    if(length(pk_ne) & verbose) mensagem(pk_ne, msg = 2)

	return(out)

}#end .aviso_cran
##---------------------------------------------------------------
##                    INSTALL_PAC_AUX
##---------------------------------------------------------------
.install_pac_aux = function(pac,
                           repos = getOption("repos"),
                           verbose = TRUE,
                           ...)
{
    AV = available.packages(repos = repos)

    ans = pac %in% AV

    yespkg = pac[ans]
    nopkg = pac[!ans]

    if(length(yespkg))
    {
        ## instala os pacotes
        pkno = tryCatch({
            unload_pac(yespkg, verbose = FALSE)
            install.packages(yespkg, repos=repos, ...)
        }, warning = function(w) { return(yespkg)})

        if(length(pkno)) Loaded = FALSE else Loaded = TRUE

        #une = unlist(lapply(yespkg, function(x) paste0("^", x, "$") ))
        une = paste0("^", yespkg, "$")

        instpkg = .packages(all.available = TRUE)

        #pkgok = unlist(lapply(une, grep,  instpkg, value = TRUE))
        pkgok = grep(une, instpkg, value = TRUE)


        if(length(pkgok))
        {
            #ver = unlist(lapply(pkgok, function(x)as.character(packageVersion(x))))
            #pkgok = paste(pkgok, ver)
            #mensagem(pkgok, msg = 1, aviso = "Pacote instalado")

            out = data.frame(Name=pkgok, Exist = TRUE, Installed = TRUE, Loaded,
                             stringsAsFactors = FALSE)

            return(out)
        }#end if
    }#end if

    if(length(yespkg))
    {
        out = data.frame(Name = yespkg, Exist=TRUE, Installed=FALSE, Loaded,
                         row.names=yespkg,  stringsAsFactors = FALSE)

    } else {
        out = data.frame(Name=pac, Exist=FALSE, Installed=FALSE, Loaded=FALSE, row.names=pac, stringsAsFactors = FALSE)

    }#end if

    return(out)

}#end function .install_pac_aux
salah31416/bibliotheca documentation built on June 16, 2019, 10:02 p.m.