New package in CRAN: PkgsFromFiles

Its been a while since I write a CRAN package and this weekend I decidedto work on a idea I had some time ago. The result is packagePkgsFromFiles.

When working with different computers at home or work, one of theproblems I have is installing missing packages across differentcomputers. As an example, a script that works in my work computer maynot work in my home computer. This is specially annoying when I have afresh install of the operating system or R. In this case, I mustmanually install all packages, case by case. Instead of focusing on thescript at hand, I spend considerable time finding and installing missingpackages. When using laptops for teaching R, many times I had to waitfor the installation of a package before continuing the class. With mynew package, PkgsFromFiles, I can scan any folder of my computer andinstall all necessary packages before using them, as we will soonlearn.

One of the available solutions to this problem is to use packagepacman. It includesfunction p_load that will check if a package is available and, if not,install it from CRAN. However, for me, I like using library andrequire as it is consistent with my code format. Also, in a fresh Rinstall, I rather install all my required packages in a single run sothat I don’t have to wait later.

Package PkgsFromFiles solves this issue by finding and parsing all Rrelated files (.R, .Rmd, .Rnw) from a given folder. It finds all callsto library() and require() and installs all packages that are notavailable locally.

1
2
3
4
5
6
7
# from cran (soon!)
install.packages('PkgsFromFiles')

# from github
if (!require(devtools)) install.packages('devtools')
devtools::install_github('msperlin/PkgsFromFiles')

The main function of the package is pff_find_and_install_pkgs, whichwill search and install missing packages from R files at a givendirectory. As an example, we’ll use my research folder from Dropbox. Itcontains all R script I have ever used in my research work. Let’s try itout:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Evaluation is disable so it passes CRAN CHECKS, but you should be able to run it in your computer
library(PkgsFromFiles)

# target folder
my.dir <- '~/Dropbox/01-Pesquisa/'

df <- pff_find_and_install_pkgs(folder.in = my.dir)

## 
## Searching folder ~/Dropbox/01-Pesquisa/
## Found 32 files in 11 folders
## R Scripts: 32 files
## Rmarkdown files: 0 files
## Sweave files: 0 files
## Checking available pkgs from https://cloud.r-project.org
## Checking and installing missing pkgs
## Installing dplyr Already installed
## Installing stringr Already installed
## Installing GetDFPData Already installed
## Installing xlsx Already installed
## Installing googlesheets Already installed
## Installing dplyr Already installed
## Installing tidyverse Already installed
## Installing purrr Already installed
## Installing dplyr Already installed
## Installing stringr Already installed
## Installing purrr Already installed
## Installing purrr Already installed
## Installing GetDFPData Already installed
## Installing dplyr Already installed
## Installing stringr Already installed
## Installing BatchGetSymbols Already installed
## Installing lubridate Already installed
## Installing purrr Already installed
## Installing GetDFPData Already installed
## Installing dplyr Already installed
## Installing stringr Already installed
## Installing plm Already installed
## Installing stargazer Already installed
## Installing RoogleVision Instalation failed, pkg not in CRAN
## Installing dplyr Already installed
## Installing rvest Already installed
## Installing tidyverse Already installed
## Installing furrr Already installed
## Installing XML Already installed
## Installing tidyverse Already installed
## Installing furrr Already installed
## Installing fst Already installed
## Installing rvest Already installed
## Installing tidyverse Already installed
## Installing purrr Already installed
## Installing XML Already installed
## Installing fst Already installed
## Installing furrr Already installed
## Installing stringr Already installed
## Installing tidyverse Already installed
## Installing furrr Already installed
## Installing fst Already installed
## Installing lubridate Already installed
## Installing ggplot2 Already installed
## Installing GetDFPData Already installed
## Installing genderBR Already installed
## Installing BatchGetSymbols Already installed
## Installing tidyverse Already installed
## Installing furrr Already installed
## Installing fst Already installed
## Installing lubridate Already installed
## Installing ggplot2 Already installed
## Installing GetDFPData Already installed
## Installing tidyverse Already installed
## Installing furrr Already installed
## Installing fst Already installed
## Installing lubridate Already installed
## Installing ggplot2 Already installed
## Installing GetDFPData Already installed
## Installing sandwich Already installed
## Installing stargazer Already installed
## Installing tidyverse Already installed
## Installing furrr Already installed
## Installing fst Already installed
## Installing lubridate Already installed
## Installing ggplot2 Already installed
## Installing plm Already installed
## Installing stargazer Already installed
## Installing lmtest Already installed
## Installing MatchIt Already installed
## Installing stringr Already installed
## Installing tidyverse Already installed
## Installing furrr Already installed
## Installing fst Already installed
## Installing lubridate Already installed
## Installing ggplot2 Already installed
## Installing GetDFPData Already installed
## Installing genderBR Already installed
## Installing BatchGetSymbols Already installed
## Installing tidyverse Already installed
## Installing furrr Already installed
## Installing fst Already installed
## Installing lubridate Already installed
## Installing ggplot2 Already installed
## Installing GetDFPData Already installed
## Installing tidyverse Already installed
## Installing furrr Already installed
## Installing fst Already installed
## Installing lubridate Already installed
## Installing ggplot2 Already installed
## Installing GetDFPData Already installed
## Installing plm Already installed
## Installing stargazer Already installed
## Installing tidyverse Already installed
## Installing purrr Already installed
## Installing furrr Already installed
## Installing tidyverse Already installed
## Installing purrr Already installed
## Installing furrr Already installed
## Installing tidyverse Already installed
## Installing purrr Already installed
## Installing GetLattesData Already installed
## Installing furrr Already installed
## Installing tidyverse Already installed
## Installing purrr Already installed
## Installing GetLattesData Already installed
## Installing furrr Already installed
## Installing tidyverse Already installed
## Installing lubridate Already installed
## Installing lubridate Already installed
## Installing RSelenium Already installed
## Installing stringr Already installed
## Installing XML Already installed
## Installing httr Already installed
## Installing stringr Already installed
## Installing XML Already installed
## Installing dplyr Already installed
## Installing stringr Already installed
## Installing tidyverse Already installed
## Installing GetDFPData Already installed
## Installing xlsx Already installed
## Installing stringr Already installed
## Installing XML Already installed
## 
## Summary:
## Found 126 packages already installed
## Had to install 0 packages
## Instalation failed for 1 packages
## 1 due to package not being found in CRAN
## 0 due to missing dependencies or other problems
## 
## Check output dataframe for more details about failed packages

As you can see, function pff_find_and_install_pkgs will find all Rrelated files recursively in the given folder. In this case, I have allpackages locally so no installation was required. A summary in text isshown at the end of execution.

The output of the function is a dataframe with the details of theoperation. Have a look:

1
2
3
4
5
6
7
8
dplyr::glimpse(df)

## Observations: 127
## Variables: 3
## $ pkg "dplyr", "stringr", "GetDFPData", "xlsx", "goog...
## $ status.message "Already installed", "Already installed", "Alre...
## $ installation TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,...

The package also includes function pff_find_R_files_from_folder, whichwill find all packages used in R related files from a given folder. Itoutputs a dataframe with several information about packages used in thefound scripts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
df.files <- pff_find_R_files_from_folder(folder.in = my.dir)

## 
## Searching folder ~/Dropbox/01-Pesquisa/
## Found 32 files in 11 folders
## R Scripts: 32 files
## Rmarkdown files: 0 files
## Sweave files: 0 files

dplyr::glimpse(df.files)

## Observations: 32
## Variables: 5
## $ files "/home/msperlin/Dropbox/01-Pesquisa//01-Working Pap...
## $ file.names "01-Build_Presidents_Table.R", "02-DownloadPictures...
## $ extensions "R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "...
## $ pkgs "dplyr ; stringr ; GetDFPData ; xlsx", "googlesheet...
## $ n.pkgs 4, 4, 1, 3, 6, 6, 2, 4, 3, 6, 9, 6, 8, 5, 4, 1, 9, ...

I also wrote a simple function for plotting the most used packages for agiven folder:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# target folder
my.dir <- '~/Dropbox/01-Pesquisa/'

# plot most used pkgs
p <- pff_plot_summary_pkgs(folder.in = my.dir)

## 
## Searching folder ~/Dropbox/01-Pesquisa/
## Found 32 files in 11 folders
## R Scripts: 32 files
## Rmarkdown files: 0 files
## Sweave files: 0 files

print(p)

As you can see, I’m a big fan of the tidyverse!

Hope you guys find the package useful! Fell free to send any question tothe comment section of the post or my email ([email protected]).

Related