Reputation: 524
My ur.R file goes as follows:
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')
)
)
)
)
and my server.R goes as follows:
library(shiny)
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?
I have also tried wrapping arguments of the function, downdloadHandler
like this as suggested by Abinav in his comments of this video.
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.
Upvotes: 0
Views: 1180
Reputation: 4072
The problem is that you don't have an input element called var3
, so input$var3
returns NULL
and thus the filename becomes invalid ("iris."
)
You named the input type
, so use input$type
:
output$down <- downloadHandler(
filename = function() {
paste("iris", input$type, sep=".")
},
# content is a function with argument file. content writes the plot to the device
content = function(file) {
if(input$type == "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
}
)
(Note that you're saving hist(colm())
, that is not the same plot as you rendered)
When I hit the download button, It shows like the following whereas the file name suppose to be iris.png. Why this behaviour?
The result from filename=function()
is not used by the RStudio Viewer to save the file. This is how it was for a long time. You can download the file if you name it though and it will work.
The result from filename=function()
works as you would expect.
Upvotes: 2