Download Png/Jpg with R

Download png/jpg with R

So it looks you are under Windows. When you download binary files, you have to specify the mode to be binary, e.g.

download.file(link, ..., mode = 'wb')

see ?download.file for details.

R: Why downloaded PNG file using download.file() can not be opened?

Dropbox provides the way to download contents directly.

https://zapier.com/learn/how-to/generate-direct-dropbox-link/

Your code should be

URL <- "https://dl.dropboxusercontent.com/s/cwqr0dxqmgjkna4/third_logo.png"
download.file(URL, destfile = "test.png", mode = 'wb')

how to download and display an image from an URL in R?

If I try your code it looks like the image is downloaded. However, when opened with windows image viewer it also says it is corrupt.
The reason for this is that you don't have specified the mode in the download.file statement.

Try this:

download.file(y,'y.jpg', mode = 'wb')

For more info about the mode is see ?download.file

This way at least the file that you downloaded is working.

To view the image in R, have a look at

jj <- readJPEG("y.jpg",native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(jj,0,0,1,1)

or how to read.jpeg in R 2.15
or Displaying images in R in version 3.1.0

R Downloading Pictures from URL

I couldn't find a duplicate, so I'll just answer.

The download.file help is a bit fragmented on this, but if you read it all carefully you can find that, on Windows, the default mode = "w" is only suitable for text files. For binary files (pretty much everything but text) you need mode = "wb". This will be done automagically if the URL ends in any of gz, .bz2, .xz, .tgz, .zip, .rda or .RData, however for anything else you need to specify mode = "wb" yourself.

Unable to download a .png file from shiny

This seems to be an issue of scope. Your code does work if you put the download button in the sidebar panel, if you remove the tabsetPanel, or if you put the download button in the same tabPanel. The last solution is shown below:

mainPanel(
tabsetPanel( type = "tabs", #Open panel
tabPanel("Distributions",
plotOutput("hist.plot"),
downloadButton('downloadhist', 'Download Plot')
)
)
) # close mainPanel

Downloading wordcloud2 output as png/jpg on shiny

I managed to make my download work by using an example of download handler function posted on LeafletMaps here: Why is webshot not working with leaflets in R shiny?

My updated code is as below:

  library(shiny)
library(htmlwidgets)
library(webshot)
library(wordcloud2)
#webshot::install_phantomjs()

ui <- shinyUI(fluidPage(mainPanel(
wordcloud2Output("wordcl"),
downloadButton(outputId = "savecloud")
)))

server <- shinyServer(function(input, output, session) {
wordcl <- reactive ({
wordcloud2(demoFreq, color = "random-light", backgroundColor = "grey")
})
output$wordcl <- renderWordcloud2({
wordcl()
})
output$savecloud <- downloadHandler(
filename = paste("wordcloud", '.png', sep=''),
content = function(file) {
owd <- setwd(tempdir())
on.exit(setwd(owd))
saveWidget(wordcl(), "temp.html", selfcontained = FALSE)
webshot("temp.html", delay =15, file = file, cliprect = "viewport")
})
})

shinyApp(ui = ui, server = server)

The solution given on the link seems to combine the solutions I was trying to implement in my original post.

The only issue is that it does not work when the app is deployed on shiny.io

Downloading png from Shiny (R)

A workaround for this strange scenario was discussed on the shiny-discuss google group. What you can do is simply change your reactive plotInput statement into a normal function. Not sure why downloadHandler doesn't play nice with reactive objects.

# change
plotInput <- reactive({...})

# into this
plotInput <- function(){...}

You can also remove the print statement in the downloadHandler call:

output$downloadPlot <- downloadHandler(
filename = "Shinyplot.png",
content = function(file) {
png(file)
plotInput()
dev.off()
})


Related Topics



Leave a reply



Submit