The goal of the envi package is manage mulitple R environments
(collections of package). This means providing functionality to retrieve
remote environments, create new environments, and switch between them.
The envi package integrates functionality provided by Kevin Ushey’s
renv package,
which provides individual environment fuctionality, and other packages
including git2r and
piggyback, which provides
source control and access to remote environments.
To draw an analogy from the Python community, just as the renv package
is similar to the virtual
environments
functionality provided in Python, envi is similar to
conda. However, the scope is more limited
in the following ways.
envi package is used from within the R programming
environment. This has the advantage of making it more accessible to
R users, who may not have the system administration experience
needed to tune installations or troubleshoot problems with paths. At
the same time, envi environment configurations and the R
environments they manage can easily be modified by more savvy users.envi package allows
you to configure a Python environment through renv, it is intended
to create and manage R environments. This limitation in scope makes
the management of package dependencies, one of Conda’s core goals,
almost trivial because of the R commuities enforcement of downstream
package compatibility via CRAN.The envi package is still under developement and is not available on
CRAN. However, it can be installed from GitHub
with:
# install.packages("devtools")
devtools::install_github("kaneplusplus/envi")
Suppose you’d like to create a deep learning model with the keras but
you don’t have Python installed, you do have Python installed but you’re
not sure how to get R to talk with Python, or you’re feeling lazy.
Instead of configuring your keras installation, you can download and
manage an enironment on Github with envi on R version 2.6.1. You can
check your version with Sys.getenv()[['R_VERSION']]
library(envi)
# Get the appropriate repo for the platform.
envi_image <- switch(Sys.info()[['sysname']],
Darwin = "keras-environmentx86_64-apple-darwin15.6.0.tar.bz2",
Windows = "keras-environmentx86_64-w64-mingw32.zip",
Linux = "keras-environmentx86_64-pc-linux-gnu.tar.bz2")
# Get the environment and call it keras-env. Note that the environment is big
# and the following can take some time.
envi_pb_install(envi_image, repo = "kaneplusplus/keras-envi", handle = "keras-env")
#> Downloading piggyback'ed environment.
#> Warning in get_token(): Using default public GITHUB_TOKEN.
#> Please set your own token
#> Warning in get_token(): Using default public GITHUB_TOKEN.
#> Please set your own token
# See which environments we have and where they are located.
envi_list()
#> # A tibble: 1 x 2
#> handle path
#> <chr> <chr>
#> 1 keras-env /Users/mike/.envi/environments/keras-environmentx86_64-apple-darwin…
Now that we have the environment, which includes keras and an
installation of Python, let’s use it to build a simple deep learner.
# Activate the enviroment.
envi_activate("keras-env")
# Now we can use the environments packages and other functionality.
library(keras)
# Create a model to predict iris species.
dl_model <- keras_model_sequential() %>%
layer_dense(units = 4) %>%
layer_dense(units = 3, activation = "softmax")
# Compile the model.
dl_model %>% compile(
loss = loss_categorical_crossentropy,
optimizer = optimizer_adadelta(),
metrics = c("accuracy"))
# Create the design matrix and dependent variables.
form <- Species ~ . -1
iris_x <- model.matrix(form, iris)
iris_y <- to_categorical(as.numeric(iris$Species) - 1)
# Fit the model.
dl_model %>% fit(
iris_x,
iris_y,
batch_size = 150,
epoch = 1000,
validation_split = 0.1)
# Calculate the model accuracy.
dl_acc <-
apply(predict(dl_model, iris_x), 1, which.max) == as.numeric(iris$Species)
sum(dl_acc) / length(dl_acc)
#> [1] 0.9866667
Please note that the ‘envi’ project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.