Skip to content

Instantly share code, notes, and snippets.

@pbiecek
Created June 26, 2016 08:03
Show Gist options
  • Save pbiecek/f8a644497d3ed378d9a73f65be629240 to your computer and use it in GitHub Desktop.
Save pbiecek/f8a644497d3ed378d9a73f65be629240 to your computer and use it in GitHub Desktop.
shiny + archivist - examples
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>Get this object:</i><br/><code>archivist::aread('",hash,"')</code>"))
})
})
# Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment