Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloadhandler doesnt respect the paste function. #2

Closed
davidrajm opened this issue May 6, 2018 · 3 comments
Closed

Downloadhandler doesnt respect the paste function. #2

davidrajm opened this issue May 6, 2018 · 3 comments

Comments

@davidrajm
Copy link

davidrajm commented May 6, 2018

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:


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
      
    } 
  )
  
  
  
}) 

image

When I hit the download button, It shows like the following whereas the file name suppose to be iris.png. Why this behaviour?
image

I have also tried wrapping arguments of the function, downdloadHandler like this

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.

@aagarw30
Copy link
Owner

aagarw30 commented May 7, 2018

Hello David,
Thanks for the details.

Sharing the observation -
Not sure why you have input$var3 in your downloadhandler(). The UI does not have any input widget with var3 input ID.

Nevertheless, I ran the code with the following updates -
output$down <- downloadHandler(
filename = function() {
paste("iris", input$type, sep=".") # replace input$var3 by input$type ty Abi
},
# content is a function with argument file. content writes the plot to the device
content = function(file) {
if(input$type == "png") # replace input$var3 by input$type ty Abi
png(file) # open the png device
else
pdf(file) # open the pdf device
hist(colm()) # draw the plot
dev.off() # turn the device off

} 

)

Code ran without any errors.

Important Point to note -
I am guessing that you are running the app in the window. This would be the "Run in Window" option of the "Run App" icon in the RStudio. If so, then for some reason RStudio would pop up the dialog box ignoring the file name you create using paste and would show the file name as the output ID of download handler which in your case is "down". It is a known behavior.

I suggest you choose "Run External" and then you should be able to download the file with the file name you have defined in the downloadhandler function. I have tested it and should work for you. Let me know how it goes.

I will exclusively make a video as well to show this to you tube viewers so it is easy to understand and they do not take this as a failure on them or an issue per say.

I will wait for your response so can close this item.

Thanks, have a great day!

Regards,
Abi

@davidrajm
Copy link
Author

davidrajm commented May 7, 2018 via email

@aagarw30
Copy link
Owner

Youtube video from my channel reproducing and explaining the behavior of download plot
https://youtu.be/a6UOnYejni8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants