Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Shiny is a great tool for interactive exploration (and not only for that). But, due to its architecture, all objects/results that are generated are stored in a separate R process so you cannot access them easily from your R console.
In some cases you may wish to retrieve a model or a plot that you have just generated. Or maybe just wish to store all R objects (plots, data sets, models) that have been ever generated by your Shiny application. Or maybe you would like to do some further tuning or validation of a selected model or plot. Or maybe you wish to collect and compare all lm() models ever generated by your app? Or maybe you would like to have an R code that will recover given R object in future.
So, how to do this?
Here I am going to present a solution based on a combination of archivist + shiny.
Archivist is a package for management of R objects. It allows you to create a repository (local folder or git repo) and push all interesting R objects to it. Each object will be stored along with its metadata. Each object will get it’s unique key, i.e. md5hash.
Then you or your collaborators may access these objects from any R process, local or remote.
So how to extract an R object from shiny to your local R console?
1. In the app create an archivist repository with archivist::createLocalRepo().
2. In the app push selected objects from shiny to this repository with archivist::saveToLocalRepo().
3. In the app print links to these objects somewhere in the shiny UI.
4. In the console, read the R object from the repository with archivist::aread().
Below you will find an example shiny application, that creates a repo and stores all plots in this repo. Also it’s printing hooks to selected objects in a figure caption.
library(shiny) library(archivist) library(ggplot2) # [a] archivist repo # create if not exist, or set as default if exists if (!file.exists("arepo")) { createLocalRepo("arepo", default = TRUE) } else { setLocalRepo("arepo") } # UI with plot and archivist hooks ui <- shinyUI(fluidPage( titlePanel("Example integration of shiny and archivist"), sidebarLayout( sidebarPanel( selectInput("var1", "Variable on OX:", colnames(iris), "Sepal.Length"), selectInput("var2", "Variable on OY:", colnames(iris), "Sepal.Width") ), mainPanel( plotOutput("plotIt"), uiOutput("printArchivistHooks") ) ) )) server <- shinyServer(function(input, output) { # create a plot createPlot <- reactive({ var1 <- input$var1 var2 <- input$var2 ggplot(iris, aes_string(var1, var2, color="Species")) + geom_point() + geom_smooth(method="lm", se=FALSE) }) # just plot the plot output$plotIt <- renderPlot({ createPlot() }) # add the plot to repository and print it's link output$printArchivistHooks <- renderUI({ pl <- createPlot() hash <- saveToLocalRepo(pl) HTML(paste0("<i>Link to the plot:</i><br/><code>archivist::aread('",hash,"')</code>")) }) }) # Run the application shinyApp(ui = ui, server = server)
In order to read a single R object from default repo you can use aread()
function, like
archivist::setLocalRepo('arepo') archivist::aread('82560bb87decd327edcd1c875e87aec9')
Since the repository stores all data related to stored objects, you may ask for R objects that match some pattern. The instruction below downloads all R objects of the class ‘gg’ and plot them all.
library("gridExtra") allPlots <- asearch("class:gg") do.call(grid.arrange, allPlots)
If you wish to learn more about the archivist package, come to the useR 2016 archivist talk or see the archivist webpage.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.