Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Last active July 31, 2016 16:11
Show Gist options
  • Save aagarw30/66092a624d8eb15c337d1c3d6aab6d04 to your computer and use it in GitHub Desktop.
Save aagarw30/66092a624d8eb15c337d1c3d6aab6d04 to your computer and use it in GitHub Desktop.
Plots2PDF
library(shiny)
shinyServer(function(input,output,session)({
# x contains all the observations of the x variable selected by the user. X is a reactive function
x <- reactive({
iris[,as.numeric(input$var1)]
})
# x contains all the observations of the y variable selected by the user. Y is a reactive function
y <- reactive({
iris[,as.numeric(input$var2)]
})
# xl contains the x variable or column name of the iris dataset selected by the user
xl <- reactive({
names(iris[as.numeric(input$var1)])
})
# yl contains the y variable or column name of the iris dataset selected by the user
yl <- reactive({
names(iris[as.numeric(input$var2)])
})
plota = function(){
plot(x=x(), y=y(), main = "iris dataset plot a", xlab = xl(), ylab = yl())
}
plotb = function(){
plot(x=x(), y=y(), main = "iris dataset plot b", xlab = xl(), ylab = yl())
}
plotc = function(){
plot(x=x(), y=y(), main = "iris dataset plot c", xlab = xl(), ylab = yl())
}
plotd = function(){
plot(x=x(), y=y(), main = "iris dataset plot d", xlab = xl(), ylab = yl())
}
output$plot1 <- renderPlot({
plota()
})
output$plot2 <- renderPlot({
plotb()
})
output$plot3 <- renderPlot({
plotc()
})
output$plot4 <- renderPlot({
plotd()
})
# downloadHandler contains 2 arguments as functions, namely filename, content
output$down <- downloadHandler(
# content is a function with argument file. content writes the plot to the device
filename = "plot.pdf",
content = function(file) {
pdf(file)
plota()
plotb()
plotc()
plotd()
dev.off()
}
)
}))
library(shiny)
shinyUI(fluidPage(
titlePanel("Download base plot in Shiny - an example"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "var1", label = "Select the X variable", choices = c("Sepal.Length" = 1, "Sepal.Width" = 2, "Petal.Length" = 3, "Petal.Width" = 4)),
selectInput(inputId = "var2", label = "Select the Y variable", choices = c("Sepal.Length" = 1, "Sepal.Width" = 2, "Petal.Length" = 3, "Petal.Width" = 4), selected = 2)
),
mainPanel(
downloadButton(outputId = "down", label = "Download all the base plot"),
plotOutput("plot1"),
plotOutput("plot2"),
plotOutput("plot3"),
plotOutput("plot4")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment