Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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