2

EDIT:

In my shiny app, I need to download/image using URL in "www" folder and display the same using renderImage function in the shiny user Interface. Though download.file and write_disk works at R Studio side but as @JorisMeys mentioned these functions must be called on the user side, not the server side. Looking for alternative to write/download images in "www" folder at shiny server end.

I am getting strange issue, When I am running my shiny App from R Studio , I am able to download image from the Url using "download.file" and render the image. But When I am deploying the app in shiny server, the image is not getting downloaded. Getting clueless, looking for suggestions to resolve this issue. Just to clarify its not that "download.file" is not downloading the Image.Its working for the shiny App when opened through R Studio but not from from Shiny Server Side. As suggested in this link - how to download and display an image from an URL in R?

Code which has been used:

filename <- imagename.jpg
downloadURL<-http://username:password@dev:9090/project_name/fileContents/imagepath/imagename.jpg #URL format    
download.file(URLencode(downloadURL),paste(getwd(),"/www", "/", filename, sep = ""),quiet = TRUE)
Community
  • 1
  • 1
string
  • 787
  • 10
  • 39
  • That's because you need to use `downloadHandler` instead of `download.file` when you want to download a file in a Shiny app. See also https://stackoverflow.com/questions/44504759/shiny-r-download-the-result-of-a-table/44505065#44505065 – Joris Meys Jun 13 '17 at 14:12
  • And in case it's an existing file, you can get around by copying it using `file.copy()` inside the `downloadHandler()`. – Joris Meys Jun 13 '17 at 14:19
  • @JorisMeys Thank you for the suggestions. Its not that "download.file" is not downloading the Image.Its working for the shiny App when opened through R Studio but not from from Shiny Server Side. Do you have any idea why this unusual behavior. – string Jun 14 '17 at 09:20
  • 1
    That's because 'download.file' must be called on the user side, not the server side. If the runs in RStudio, the user and the server are on the same machine, being your own computer. So that's why `download.file` still works there. If you run it through a Shiny Server otoh, the user and the server are on two different "machines". (you might have a server simulation running on your own machine, but it's still two virtual machines). And in that case you're calling `download.file` on the server side, so the file is downloaded ... to the server, not from the server. – Joris Meys Jun 14 '17 at 09:45
  • @JorisMeys Thank you for the detail explanation. In my shiny app, I need to write (download) image dynamically in "www" folder using action button and display the same in shiny app. Can't use download button as multiple events needs to triggered using the button along with download of the Image. I tried GET method (write_disk) also as per this link - https://stackoverflow.com/questions/34441684/r-downloading-pictures-from-url but failing at shiny server end. Any other way would you suggest to write/download images in "www" folder at shiny server end. – string Jun 14 '17 at 13:06
  • what are you trying to do? I understood you want the user to be able to download the image. But your extra explanation makes me believe you actually want to download an image from another URL to be displayed in the app. Is that correct? You should still be able to edit your question. If I misinterpreted it, please clarify with a bit more elaborate example, and tag me in a comment so I can reopen the question. – Joris Meys Jun 14 '17 at 13:14
  • @JorisMeys I have edited the question, please reopen it. – string Jun 14 '17 at 13:30
  • I reopened, but please be a bit more clear. You download the image from the internet. It has to end up in the www folder on the server. It has to be displayed in the app using what code exactly? Did you check this question and its second solution? https://stackoverflow.com/questions/21996887/embedding-image-in-shiny-app – Joris Meys Jun 14 '17 at 13:35
  • @JorisMeys the problem is in downloading Image from URL in "www" and that is not happening at shiny server side. Once the image is there we can use renderImage or img option to display it. Every time based on user selection image is different and thats why its need to downloaded dynamically. – string Jun 14 '17 at 13:41

1 Answers1

1

The real issue was with the restricted set of permissions with the deployed shiny app in the shiny server. To write a file/Image in "www" folder at shiny server, we need to grant write permission to the specified folder in the shiny app at server end. To do so, we can change the permission using root user. In this case, I used chmod 677 www to achieve the same and after changing the permission, below code worked perfectly. So, No issue with download.file function. For more details, please refer this link : https://groups.google.com/forum/#!topic/shiny-discuss/srWETT6uL-I

filename <- imagename.jpg
downloadURL<-http://username:password@dev:9090/project_name/fileContents/imagepath/imagename.jpg #URL format    
download.file(URLencode(downloadURL),paste(getwd(),"/www", "/", filename, sep = ""),quiet =
string
  • 787
  • 10
  • 39