<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/new-package-in-cran-pkgsfromfiles/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://msperlin.github.io/2018-10-13-NewPackage-PkgsFromFiles/"> Marcelo S. Perlin</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> – </p>
<p>Its been a while since I write a CRAN package and this weekend I decided<br>
to work on a idea I had some time ago. The result is package<br>
<code>PkgsFromFiles</code>.</p>
<p>When working with different computers at home or work, one of the<br>
problems I have is installing missing packages across different<br>
computers. As an example, a script that works in my <strong>work computer</strong> may<br>
not work in my <strong>home computer</strong>. This is specially annoying when I have a<br>
fresh install of the operating system or R. In this case, I must<br>
manually install all packages, case by case. Instead of focusing on the<br>
script at hand, I spend considerable time finding and installing missing<br>
packages. When using laptops for teaching R, many times I had to wait<br>
for the installation of a package before continuing the class. With my<br>
new package, PkgsFromFiles, I can scan any folder of my computer and<br>
install all necessary packages <strong>before</strong> using them, as we will soon<br>
learn.</p>
<p>One of the available solutions to this problem is to use package<br>
<a rel="nofollow" href="https://cran.r-project.org/package=pacman" target="_blank">pacman</a>. It includes<br>
function <code>p_load</code> that will check if a package is available and, if not,<br>
install it from CRAN. However, for me, I like using <code>library</code> and<br>
<code>require</code> as it is consistent with my code format. Also, in a fresh R<br>
install, I rather install all my required packages in a single run so<br>
that I don’t have to wait later.</p>
<p>Package PkgsFromFiles solves this issue by finding and parsing all R<br>
related files (.R, .Rmd, .Rnw) from a given folder. It finds all calls<br>
to library() and require() and installs all packages that are not<br>
available locally.</p>
<h1>Installation</h1>
<div>
<div>
<pre><code># from cran (soon!)
install.packages('PkgsFromFiles')

# from github
if (!require(devtools)) install.packages('devtools')
devtools::install_github('msperlin/PkgsFromFiles')
</code></pre>
</div>
</div>
<h1>Usage</h1>
<p>The main function of the package is <code>pff_find_and_install_pkgs</code>, which<br>
will search and install missing packages from R files at a given<br>
directory. As an example, we’ll use my research folder from Dropbox. It<br>
contains all R script I have ever used in my research work. Let’s try it<br>
out:</p>
<div>
<div>
<pre><code># 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 &lt;- '~/Dropbox/01-Pesquisa/'

df &lt;- 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 purrr 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 lubridate Already installed
## Installing tidyverse Already installed
## Installing lubridate Already installed
## Installing lubridate Already installed
## Installing RSelenium Already installed
## Installing stringr   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
</code></pre>
</div>
</div>
<p>As you can see, function <code>pff_find_and_install_pkgs</code> will find all R<br>
related files recursively in the given folder. In this case, I have all<br>
packages locally so no installation was required. A summary in text is<br>
shown at the end of execution.</p>
<p>The output of the function is a dataframe with the details of the<br>
operation. Have a look:</p>
<div>
<div>
<pre><code>dplyr::glimpse(df)

## Observations: 127
## Variables: 3
## $ pkg            </code></pre></div></div>