Download an image

83 views
Skip to first unread message

Shivam Rana

unread,
Jun 27, 2015, 11:37:01 AM6/27/15
to shiny-...@googlegroups.com
I want the download button to work but it just downloads a 0 byte file everytime. Here is my code-

server.R

library(shiny)
library
(wordcloud)
library
(tm)

shinyServer
(function(input, output) {

  datainput
<- reactive({
    words
<- Corpus(DirSource("temp/"))
    words
<- tm_map(words, stripWhitespace)
    words
<- tm_map(words, content_transformer(tolower))
    words
<- tm_map(words, removeWords, stopwords("english"))
   
})

  finalinput
<- reactive({
   
if (input$checkbox1) datainput <- tm_map(datainput(), removeNumbers)
   
if (input$checkbox2) datainput <- tm_map(datainput(), removePunctuation)
   
if (input$checkbox3) datainput <- tm_map(datainput(), stemDocument)
   
})

  make_cloud
<- reactive({
    wordcloud
(finalinput(),
      scale
=c(5, 0.5),
      min
.freq=3,
      max
.words=input$slider,
      random
.order=input$checkbox4,
      rot
.per=0.35,
     
use.r.layout=FALSE,
      colors
=brewer.pal(8, "Dark2"))
   
})

  output$wordcloud_img
<- downloadHandler(
    filename
= "wordcloud.png",
    content
= function(cloud) {
      png
(cloud)
      make_cloud
()
      dev
.off()
   
})

  output$wordcloud
<- renderPlot({
    make_cloud
()
   
})

})

Here, if I comment out the make_cloud() from the renderPlot() then the plot is not being rendered (obviously) and the image can be downloaded at that time. Is there something that a recctive element can't be used 2nd time? I don't that is the case here. But I am not able to figure out the reason. Can anyone give me a solution to this?

Yihui Xie

unread,
Jun 27, 2015, 3:54:31 PM6/27/15
to Shivam Rana, shiny-discuss
Reactive expressions are not like normal R functions. Please read this
article (in particular, the section "Reactive conductors"):
http://shiny.rstudio.com/articles/reactivity-overview.html You should
not rely on the side effects of reactive expressions (such as
generating plots), and reactive expressions should only be used for
their returned values. In your case, my guess is that you can make
make_cloud() create a png file, and return the filename. Then use
renderImage(..., deleteFile = FALSE) to display the file, and
downloadHandler() to download this file.

Regards,
Yihui

Joe Cheng

unread,
Jun 29, 2015, 6:49:21 PM6/29/15
to Yihui Xie, Shivam Rana, shiny-discuss
Or just change make_cloud from a reactive to a function(){...}.
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/CANROs4epNZy6VLVhj%2B2bcQS5x0eze96va%2BXPienrHtOS%2BZKtXg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages