library(shiny)
shinyUI(
fluidPage(
titlePanel(title = h4('Demonstraion of renderplot', align='center')),
sidebarLayout(
sidebarPanel(
selectInput('var', "Select the Variable", choices = c('Sepal.Length' =1 , 'sepal width' = 2, 'Petal Length' = 3 , 'Petal Width' = 4), selected = 1),
br(),
sliderInput('bins', 'Select the number of bins', min = 5, max = 25, value = 15),
br(),
radioButtons('color', 'Color of the bins', choices = c('Green', 'Red', 'Blue'), selected = 'Green'),
br(),
radioButtons('type', 'Choose the type', choices = list('png', 'pdf'), selected = 'png')
),
mainPanel(
plotOutput("myhist"),
downloadButton('down', 'Download the Plot')
)
)
)
)
shinyServer(function(input, output){
colm = reactive({
as.numeric(input$var)
})
output$myhist = renderPlot(
{
hist(iris[,colm()], breaks = seq(0,max(iris[,colm()], l= input$bins+1)),col =input$color, main ="Histogram of irish dataset", xlab = names(iris[colm()]))
}
)
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
hist(colm()) # draw the plot
dev.off() # turn the device off
}
)
})
When I hit the download button, It shows like the following whereas the file name suppose to be iris.png
. Why this behaviour?
![image](https://user-images.githubusercontent.com/15086149/39674186-a85d75b6-5165-11e8-9100-bd756d2b43fd.png)
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
hist(colm()) # draw the plot
dev.off() # turn the device off
}
})
But this gave the error message as follows.
Error in parse(file, keep.source = FALSE, srcfile = src, encoding = enc) :
G:\R_workshop_related_NITTE\Shiny\downloadPlots/server.R:17:6: unexpected ','
16: paste("iris", input$var3, sep=".")
17: },
^
Warning: Error in sourceUTF8: Error sourcing C:\Users\Mahe\AppData\Local\Temp\RtmpIPEtpl\file29805c1e4eca
Stack trace (innermost first):
1: runApp
Error in sourceUTF8(serverR, envir = new.env(parent = globalenv())) :
Error sourcing C:\Users\Mahe\AppData\Local\Temp\RtmpIPEtpl\file29805c1e4eca
I am in windows machine Rstudio Version 1.1.442 with R 3.4.4.