- Instalación y carga de librerías
- Usar función sin cargar librería
- Carga e instalación simulatanea
- Instalación desde otros repositorios
Instalación y carga de librerías
Si usas R, sabes que una de sus grandes ventajas es la ampliación de sus funcionalidades mediantes paquetes o librerías.
Te presentamos varios tips a fin de acelerar el proceso de instalación y carga de tus librerías.
Instalación normal
install.packages("xts")
Instalación normal con todas sus dependencias
install.packages("dplyr", dependencies = T)
Si por alguna razón necesitamos los paquetes en zip los guardamos con el argumento destdir.
install.packages("ineq", destdir = "C:/temp")
Lo cual también aplica para descargar un paquete con todas sus dependencias.
install.packages("tm", dependencies = T, destdir = "C:/temp")
Installing package into �C:/Users/crbdl/Documents/R/win-library/3.4�
(as �lib� is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/ineq_0.2-13.zip'
Content type 'application/zip' length 67130 bytes (65 KB)
downloaded 65 KB
package ‘ineq’ successfully unpacked and MD5 sums checked
Installing package into �C:/Users/crbdl/Documents/R/win-library/3.4�
(as �lib� is unspecified)
Warning in install.packages :
dependencies ‘Rcampdf’, ‘Rgraphviz’, ‘Rpoppler’, ‘tm.lexicon.GeneralInquirer’ are not available
also installing the dependencies �NLP�, �slam�, �filehash�, �SnowballC�, �XML�
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/NLP_0.1-11.zip'
Content type 'application/zip' length 293996 bytes (287 KB)
downloaded 287 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/slam_0.1-40.zip'
Content type 'application/zip' length 118237 bytes (115 KB)
downloaded 115 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/filehash_2.4-1.zip'
Content type 'application/zip' length 275509 bytes (269 KB)
downloaded 269 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/SnowballC_0.5.1.zip'
Content type 'application/zip' length 3082209 bytes (2.9 MB)
downloaded 2.9 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/XML_3.98-1.9.zip'
Content type 'application/zip' length 4323674 bytes (4.1 MB)
downloaded 4.1 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/tm_0.7-1.zip'
Content type 'application/zip' length 1274636 bytes (1.2 MB)
downloaded 1.2 MB
package ‘NLP’ successfully unpacked and MD5 sums checked
package ‘slam’ successfully unpacked and MD5 sums checked
package ‘filehash’ successfully unpacked and MD5 sums checked
package ‘SnowballC’ successfully unpacked and MD5 sums checked
package ‘XML’ successfully unpacked and MD5 sums checked
package ‘tm’ successfully unpacked and MD5 sums checked
Una vez que hayas instalado un paquete debes cargarlo para poder usas sus funciones o información.
Carga normal
library(dplyr)
require(dplyr)
Ambas funciones cumplen la función de carga, pero ¿en que se diferencian?
La principal diferencia entre estas funciones es:
Por defecto, library() devuelve un error si el paquete solicitado no existe.
library(abcd)
Error in library(abcd) : there is no package called ‘abcd’
Mientras requiere() da una advertencia.
require(abcd)
## Loading required package: abcd
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'abcd'
require () está diseñado para ser utilizado dentro de funciones, ya que da un mensaje de advertencia y devuelve un valor lógico decir, FALSE si el paquete solicitado no se encuentra y TRUE si el paquete está cargado.
ifelse(require(dplyr)==T, "Cargado", "No cargado")
## Loading required package: dplyr
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## [1] "Cargado"
# 0=FALSE
sum(require(abcd))
## Loading required package: abcd
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'abcd'
## [1] 0
La mejor opción es utilizar library(), ya que da el mensaje de error si el paquete no se encuentra durante el tiempo de carga del paquete, lo cual nos dará una alerta temprana y evitará continuar generando errores innecesarios.
Usar función sin cargar librería
Podemos utilizar una función de un paquete sin cargarlo, cargando únicamente dicha función de la siguiente forma:
rpart::car.test.frame
## Price Country Reliability Mileage Type
## Eagle Summit 4 8895 USA 4 33 Small
## Ford Escort 4 7402 USA 2 33 Small
## Ford Festiva 4 6319 Korea 4 37 Small
## Honda Civic 4 6635 Japan/USA 5 32 Small
## Mazda Protege 4 6599 Japan 5 32 Small
## Mercury Tracer 4 8672 Mexico 4 26 Small
## Nissan Sentra 4 7399 Japan/USA 5 33 Small
## Pontiac LeMans 4 7254 Korea 1 28 Small
## Subaru Loyale 4 9599 Japan 5 25 Small
## Subaru Justy 3 5866 Japan NA 34 Small
## Toyota Corolla 4 8748 Japan/USA 5 29 Small
## Toyota Tercel 4 6488 Japan 5 35 Small
## Volkswagen Jetta 4 9995 Germany 3 26 Small
## Chevrolet Camaro V8 11545 USA 1 20 Sporty
## Dodge Daytona 9745 USA 1 27 Sporty
## Ford Mustang V8 12164 USA 1 19 Sporty
## Ford Probe 11470 USA 3 30 Sporty
## Honda Civic CRX Si 4 9410 Japan 5 33 Sporty
## Honda Prelude Si 4WS 4 13945 Japan 5 27 Sporty
## Nissan 240SX 4 13249 Japan 3 24 Sporty
## Plymouth Laser 10855 USA NA 26 Sporty
## Subaru XT 4 13071 Japan NA 28 Sporty
## Audi 80 4 18900 Germany NA 27 Compact
## Buick Skylark 4 10565 USA 2 23 Compact
## Chevrolet Beretta 4 10320 USA 1 26 Compact
## Chrysler Le Baron V6 10945 USA 4 25 Compact
## Ford Tempo 4 9483 USA 2 24 Compact
## Honda Accord 4 12145 Japan/USA 5 26 Compact
## Mazda 626 4 12459 Japan/USA 4 24 Compact
## Mitsubishi Galant 4 10989 Japan 5 25 Compact
## Mitsubishi Sigma V6 17879 Japan 4 21 Compact
## Nissan Stanza 4 11650 Japan 5 21 Compact
## Oldsmobile Calais 4 9995 USA 2 23 Compact
## Peugeot 405 4 15930 France NA 24 Compact
## Subaru Legacy 4 11499 Japan/USA 5 23 Compact
## Toyota Camry 4 11588 Japan/USA 5 27 Compact
## Volvo 240 4 18450 Sweden 3 23 Compact
## Acura Legend V6 24760 Japan 5 20 Medium
## Buick Century 4 13150 USA 3 21 Medium
## Chrysler Le Baron Coupe 12495 USA 2 22 Medium
## Chrysler New Yorker V6 16342 USA 3 22 Medium
## Eagle Premier V6 15350 USA 2 22 Medium
## Ford Taurus V6 13195 USA 3 22 Medium
## Ford Thunderbird V6 14980 USA 1 23 Medium
## Hyundai Sonata 4 9999 Korea NA 23 Medium
## Mazda 929 V6 23300 Japan 5 21 Medium
## Nissan Maxima V6 17899 Japan 5 22 Medium
## Oldsmobile Cutlass Ciera 4 13150 USA 2 21 Medium
## Oldsmobile Cutlass Supreme V6 14495 USA NA 21 Medium
## Toyota Cressida 6 21498 Japan 3 23 Medium
## Buick Le Sabre V6 16145 USA 3 23 Large
## Chevrolet Caprice V8 14525 USA 1 18 Large
## Ford LTD Crown Victoria V8 17257 USA 3 20 Large
## Chevrolet Lumina APV V6 13995 USA NA 18 Van
## Dodge Grand Caravan V6 15395 USA 3 18 Van
## Ford Aerostar V6 12267 USA 3 18 Van
## Mazda MPV V6 14944 Japan 5 19 Van
## Mitsubishi Wagon 4 14929 Japan NA 20 Van
## Nissan Axxess 4 13949 Japan NA 20 Van
## Nissan Van 4 14799 Japan NA 19 Van
## Weight Disp. HP
## Eagle Summit 4 2560 97 113
## Ford Escort 4 2345 114 90
## Ford Festiva 4 1845 81 63
## Honda Civic 4 2260 91 92
## Mazda Protege 4 2440 113 103
## Mercury Tracer 4 2285 97 82
## Nissan Sentra 4 2275 97 90
## Pontiac LeMans 4 2350 98 74
## Subaru Loyale 4 2295 109 90
## Subaru Justy 3 1900 73 73
## Toyota Corolla 4 2390 97 102
## Toyota Tercel 4 2075 89 78
## Volkswagen Jetta 4 2330 109 100
## Chevrolet Camaro V8 3320 305 170
## Dodge Daytona 2885 153 100
## Ford Mustang V8 3310 302 225
## Ford Probe 2695 133 110
## Honda Civic CRX Si 4 2170 97 108
## Honda Prelude Si 4WS 4 2710 125 140
## Nissan 240SX 4 2775 146 140
## Plymouth Laser 2840 107 92
## Subaru XT 4 2485 109 97
## Audi 80 4 2670 121 108
## Buick Skylark 4 2640 151 110
## Chevrolet Beretta 4 2655 133 95
## Chrysler Le Baron V6 3065 181 141
## Ford Tempo 4 2750 141 98
## Honda Accord 4 2920 132 125
## Mazda 626 4 2780 133 110
## Mitsubishi Galant 4 2745 122 102
## Mitsubishi Sigma V6 3110 181 142
## Nissan Stanza 4 2920 146 138
## Oldsmobile Calais 4 2645 151 110
## Peugeot 405 4 2575 116 120
## Subaru Legacy 4 2935 135 130
## Toyota Camry 4 2920 122 115
## Volvo 240 4 2985 141 114
## Acura Legend V6 3265 163 160
## Buick Century 4 2880 151 110
## Chrysler Le Baron Coupe 2975 153 150
## Chrysler New Yorker V6 3450 202 147
## Eagle Premier V6 3145 180 150
## Ford Taurus V6 3190 182 140
## Ford Thunderbird V6 3610 232 140
## Hyundai Sonata 4 2885 143 110
## Mazda 929 V6 3480 180 158
## Nissan Maxima V6 3200 180 160
## Oldsmobile Cutlass Ciera 4 2765 151 110
## Oldsmobile Cutlass Supreme V6 3220 189 135
## Toyota Cressida 6 3480 180 190
## Buick Le Sabre V6 3325 231 165
## Chevrolet Caprice V8 3855 305 170
## Ford LTD Crown Victoria V8 3850 302 150
## Chevrolet Lumina APV V6 3195 151 110
## Dodge Grand Caravan V6 3735 202 150
## Ford Aerostar V6 3665 182 145
## Mazda MPV V6 3735 181 150
## Mitsubishi Wagon 4 3415 143 107
## Nissan Axxess 4 3185 146 138
## Nissan Van 4 3690 146 106
Carga e instalación simultánea
Carga de varios paquetes en una lìnea. En este caso usamos require ya que nos devuelve un valor lógico.
p <- c("dplyr", "tm")
sapply(p, require, character.only = TRUE)
## Loading required package: tm
## Loading required package: NLP
## dplyr tm
## TRUE TRUE
Usamos una función del paquete tm para comprobar su carga.
removeNumbers("R 1 Users 2 Group 3- Ecuador")
## [1] "R Users Group - Ecuador"
También podemos instalar varios paquetes en una línea con la forma anterior cambiando la función por install.packages(), pero en su lugar vamos a usar paquetes adicionales.
easypackages permite cargar e instalar varios paquetes con las funciones libraries() y packages() respectivamente.
#install.packages("easypackages")
library("easypackages")
paq <- c("raster", "git2r")
libraries(paq)
## Loading required package: raster
## Loading required package: sp
##
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
##
## select
## Loading required package: git2r
##
## Attaching package: 'git2r'
## The following object is masked from 'package:raster':
##
## init
## The following object is masked from 'package:NLP':
##
## content
## The following object is masked from 'package:dplyr':
##
## pull
## All packages loaded successfully
Si algún paquete ya existe no lo vuelve a instalar, solamente instala los faltantes.
paq <- c("ggplot2", "flexdashboard")
packages(paq)
The following package is not installed: flexdashboard
Would you like to install this package now? [y/n]
1: y
Installing package into �C:/Users/crbdl/Documents/R/win-library/3.4�
(as �lib� is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/flexdashboard_0.5.zip'
Content type 'application/zip' length 649070 bytes (633 KB)
downloaded 633 KB
package ‘flexdashboard’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\crbdl\AppData\Local\Temp\RtmpK6QNvA\downloaded_packages
All packages installed successfully
Loading required package: ggplot2
Attaching package: �ggplot2�
The following objects are masked from �package:psych�:
%+%, alpha
Loading required package: flexdashboard
All packages loaded successfully
lubripack permite cargar e instalar varios paquetes a la vez y nos devuelve valores lógicos si la carga o instalación se realizan correctamente.
install.packages("lubripack")
Ya que no está actualizado el paquete para mi versión de R no puedo instalarla directamente del CRAN, pero al ser una función pequeña la copio:
lubripack <- function(...,silent=FALSE){
#check names and run 'require' function over if the given package is installed
requirePkg<- function(pkg){if(length(setdiff(pkg,rownames(installed.packages())))==0)
require(pkg, quietly = TRUE,character.only = TRUE)
}
# list of packages to install and load
packages <- as.vector(unlist(list(...)))
if(!is.character(packages))stop("No numeric allowed! Input must contain package names to install and load")
if (length(setdiff(packages,rownames(installed.packages()))) > 0 )
install.packages(setdiff(packages,rownames(installed.packages())),
repos = c("https://cran.revolutionanalytics.com/", "http://owi.usgs.gov/R/"))
res<- unlist(sapply(packages, requirePkg))
if(silent == FALSE && !is.null(res)) {cat("\nBellow Packages Successfully Installed:\n\n")
print(res)
}
}
Esta función permite cargar paquetes declarados en un vector y no se han instalado, se instalan desde el CRAN o desde OWI y devuelve valores lógicos.
c(paq,"ggvis") -> varios
lubripack(varios)
Installing package into �C:/Users/crbdl/Documents/R/win-library/3.4�
(as �lib� is unspecified)
trying URL 'https://cran.revolutionanalytics.com/bin/windows/contrib/3.4/ggvis_0.4.3.zip'
Content type 'application/zip' length 959625 bytes (937 KB)
downloaded 937 KB
package ‘ggvis’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\crbdl\AppData\Local\Temp\RtmpK6QNvA\downloaded_packages
Attaching package: �ggvis�
The following object is masked from �package:ggplot2�:
resolution
Bellow Packages Successfully Installed:
ggplot2 flexdashboard ggvis
TRUE TRUE TRUE
El paquete pacman permite cargar e instalar varios paquetes a la vez con sus funciones p_load y p_install respectivamente.
#install.packages("pacman")
library("pacman")
p_load(dplyr, psych, tm)
p_install(NLP)
## The following packages have been unloaded:
## NLP
## Installing package into 'C:/Users/crbdl/Documents/R/win-library/3.3'
## (as 'lib' is unspecified)
## package 'NLP' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\crbdl\AppData\Local\Temp\RtmpUnc4w1\downloaded_packages
##
## NLP installed
Instalación desde otros repositorios
Por defecto descargamos desde CRAN, pero no son las únicas opciones, tenemos varios repositorios como GRAN, MRAN, Bioconductor, Github, etc.
Para descargar paquetes desde estos repositorios podemos usar la función install.packages() con el argumento repos:
install.packages("smwrData", repos=c("https://owi.usgs.gov/R",getOption("repos")))
Installing package into �C:/Users/crbdl/Documents/R/win-library/3.4�
(as �lib� is unspecified)
trying URL 'https://owi.usgs.gov/R/bin/windows/contrib/3.4/smwrData_1.1.2.zip'
Content type 'application/x-zip-compressed' length 351710 bytes (343 KB)
downloaded 343 KB
package ‘smwrData’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\crbdl\AppData\Local\Temp\RtmpK6QNvA\downloaded_packages
library(smwrData)
Para descargar desde github podemos usar la función install_git().
library(devtools)
install_git("git://github.com/hadley/stringr.git")
## Downloading git repo git://github.com/hadley/stringr.git
## Installing stringr
## "C:/PROGRA~1/R/R-33~1.3/bin/x64/R" --no-site-file --no-environ --no-save \
## --no-restore --quiet CMD INSTALL \
## "C:/Users/crbdl/AppData/Local/Temp/RtmpUnc4w1/fileddc35ab6f3" \
## --library="C:/Users/crbdl/Documents/R/win-library/3.3" --install-tests
##
Desde bioconductor
source("https://bioconductor.org/biocLite.R")
biocLite(c("survival"))
BioC_mirror: https://bioconductor.org
Using Bioconductor 3.5 (BiocInstaller 1.26.1), R 3.4.1 (2017-06-30).
Installing package(s) �survival�
cannot open URL 'https://bioconductor.org/packages/3.5/data/annotation/bin/windows/contrib/3.4/PACKAGES.rds': HTTP status was '404 Not Found'cannot open URL 'https://bioconductor.org/packages/3.5/data/experiment/bin/windows/contrib/3.4/PACKAGES.rds': HTTP status was '404 Not Found'trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/survival_2.41-3.zip'
Content type 'application/zip' length 5427811 bytes (5.2 MB)
downloaded 5.2 MB
package ‘survival’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\crbdl\AppData\Local\Temp\RtmpK6QNvA\downloaded_packages
installation path not writeable, unable to update packages: BH, boot, car, caret, crayon, curl, DBI, dplyr, evaluate, foreign, glmnet,
haven, htmlwidgets, httpuv, httr, knitr, Matrix, mgcv, modelr, openssl, PKI, psych, purrr, quantmod, R6, Rcpp, repr, rlang, rmarkdown,
rsconnect, rstudioapi, scales, shiny, tibble, tidyr, TTR, xts
Old packages: 'backports'
Update all/some/none? [a/s/n]: