5 min read

how to list loaded packages in R: ramazon gets cleaver

andreacirilloac

It was around midnight here in Italy: I shared the code on Github, published a post on G+, Linkedin and Twitter and then went to bed.

In the next hours things got growing by themselves, with pleasant results like the following:

https://twitter.com/DoodlingData/status/635057258888605696

The R community found ramazon a really helpful package.

And I actually think it is: Amazon AWS is nowadays one of the most common tools for online web applications and websites hosting.

The possibility to get your Shiny App on it just running a function make it even more desirable for the amusing R people.

Therefore, even if I developed ramazon for personal purposes , I now feel some kind of responsibility to further develop it and take it updated.

Well, to be honest: is not such a bad feeling…

giphy (1)

A matter of packages

One of the main issues with the first release was the lack of support for multiple packages:

Shiny Apps often rely on different packages, but ramazon was only able to install the shiny package.

So I developed and just released a new version (0.2.0) able to **detect and install on AWS all required packages**.

You can try this new feature installing ramazon package on your R environment. Find more detailed instruction on the introductory post.

In the following lines I would like to highlights some code specs that could be interesting for the R programmers.

How ramazon lists loaded packages

This is how ramazon detects and installs al required packages.

Broom-100clean the R environment

using the detach() function, with no arguments, we are able to unload all loaded packages,

Doing this, we can be sure that no unneeded package will be installed on the AWS server.

Run Command-100source the app .R files

with this step, ramazon run the app source files in order to load all required packages within the current R environment.

Shiny apps can be written using:

  • two different files: ui.R and server.R

  • one unique file: app.R

ramazon can distinguish the two cases and sourcing only the existing files thank to the following conditional statement:

[code language=“r”] if (file.exists(“ui.R”)){ source(“ui.R”) source(“server.R”) }else { source(“app.R”)} [/code]

.

In the hypothesis of developer using a global.R file in order to make variables available both to UI side and Server side of the app (more on the Shiny help page), one more conditional statement is introduced with consequent sourcing:

[code language=“R”]

if (file.exists(“global.R”)){ source(“global.R”) }

[/code]

Data Sheet-100store all loaded packages within a data.frame

Now is time to get a list of all loaded packages.

To get this done, We use the search() function, assigning the output to a data.frame object.

[code language=“r”] environ <- data.frame(“envs” = search(),stringsAsFactors = FALSE) [/code]

the result will be something like the one below:

[code language=“r”] envs 1        .GlobalEnv 2     package:stats 3  package:graphics 4 package:grDevices 5     package:utils 6  package:datasets 7   package:methods 8         Autoloads 9      package:base [/code]

As you can see, there are some unneeded values (.GlobalEnv and Autoloads), and all packages are preceded by the “package:” wording. We are going to handle this within the next paragraph.

Grid-100clean the data.frame

Leveraging the grepl() function ramazon removes all rows not containing “package”, excluding that way all non-package records:

[code language=“r”] environ <- environ[grepl(“package”,environ[,1]),] [/code]

Finally, using gsub() the “package:” wording is removed:

[code language=“r”] packages <- gsub(“package:“,”“,environ) [/code]

Source Code-100write the system command

The main output of the ramazon() function, as explained in the previous post, is a bash script.

in order to install all the required packages ramazon needs to create a system command similar to the following one:

[code language=“r”] sudo su -\-c \“R -e \“install.packages(c(“shiny”,”ggplot2”), repos = ‘http://cran.rstudio.com/', dep = TRUE)\“
[/code]

First of all we need to create the vector containing the needed packages.

This is obtained with a sequence of paste() functions:

[code language=“r”] packages <- paste(“‘”,packages,“’”,sep =“”) packages <- paste(packages,“,”,collapse = “”) packages <- paste(“c(”,packages,sep =“”) packages <- substr(packages,1,(nchar(packages)-1)) packages <- paste(packages, “)”,sep =“”) [/code]

finally, we have to deal with escape characters contained within the system command.

This is appropriately done using the cat() function, that makes the lower number of manipulations on the given input.

Unfortunately, this function doesn’t give any value as a result, and therefore we need to directly catch the console output and store it within the bash_script file.

Here it is the complete code for this alchemical job:

[code language=“r”] sink(“bash_script.txt”, append = TRUE) message <- cat(”sudo su -\-c \“R -e \\“install.packages(”,packages,“, repos = ‘http://cran.rstudio.com/', dep = TRUE)\\”\“”) sink() [/code]

And here we are: ramazon automatically detects and install all required packages for your Shiny App on the AWS server.

getting further

Current version is somehow complete since it allows to deploy any kind of shiny app on an Amazon AWS server.

Even so there are some further possible developments:

  • I have been requested of a Windows version, and I will try to develop one. that said If anyone out there would like to help with that, just whistle!

  • ramazon is still missing some serious testing, and a log of all the console output would be really useful to help with debugging tasks.

As usual let me know if anything is going wrong &/| you have got suggestion for making ramazon a better word :)

Icon pack by Icons8

comments disclaimer
thank you for taking the time to comment. If the comment you are about to write is related to a piece of code I wrote, please reach its Github respository and place there any request of improvement or report of bugs.