Created
November 28, 2017 18:57
-
-
Save aagarw30/425b5630564816e10769a2759725feb6 to your computer and use it in GitHub Desktop.
Download plot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
shinyServer(function(input,output)({ | |
# 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)]) | |
}) | |
# render the plot so could be used to display the plot in the mainPanel | |
output$plot <- renderPlot({ | |
plot(x=x(), y=y(), main = "iris dataset plot", xlab = xl(), ylab = yl()) | |
}) | |
# downloadHandler contains 2 arguments as functions, namely filename, content | |
output$down <- downloadHandler( | |
filename = function() { | |
paste("iris", input$var3, sep=".") | |
}, | |
# content is a function with argument file. content writes the plot to the device | |
content = function(file) { | |
if(input$var3 == "png") | |
png(file) # open the png device | |
else | |
pdf(file) # open the pdf device | |
plot(x=x(), y=y(), main = "iris dataset plot", xlab = xl(), ylab = yl()) # draw the plot | |
dev.off() # turn the device off | |
} | |
) | |
})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | |
radioButtons(inputId = "var3", label = "Select the file type", choices = list("png", "pdf")) | |
), | |
mainPanel( | |
plotOutput("plot"), | |
downloadButton(outputId = "down", label = "Download the plot") | |
) | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment