Downloading png from Shiny (R) pt. 2

你说的曾经没有我的故事 提交于 2019-12-08 05:34:25

问题


This relates to a question I have asked previously at:

Downloading png from Shiny (R)

I have now created multiple shiny plots and downloaded them, but for a plot using the maptools package, I just get an empty png back.

Is this a bug in Shiny or is something wrong with my code here?

Here are the relevant excerpts from my server file:

plotInput2 <- function(){


  my.data<-DataInput()
  sub <- subset(DataInput(), as.character(DataInput()[,2])==input$var1)
  a = which(names(sub)==input$var2)
  x_lab <- as.numeric(sub[,a])   
  Country <- as.character(sub[,1])
  mapdata <- data.frame(Country=Country,Perc=x_lab)

  percent_map <- function(data) {
    # world <- map_data("world")
    data(wrld_simpl)
    world <- fortify(wrld_simpl,region='NAME')

    names(world) <- list("long","lat","order","hole","piece","group", "Country")
    world$Country <- tolower(world$Country)
    data$Country <- tolower(data$Country)
    world$Country <- tolower(world$Country)
    choro <- merge(world, data, by=c("Country"),all=TRUE)
    choro <- choro[order(choro$order), ]
    choro$Perc <-as.numeric(as.character(choro$Perc)) 

    ## PLOT MAP IN GREY ##
    ggplot() + geom_polygon(aes(long,lat,group=group),data=world, fill=NA) +


      ## PLOT DATA ##
      geom_polygon(aes(long, lat, group = group,  fill=Perc),data = choro)       

  }

  percent_map(mapdata)

    }    


output$mapjoon <- renderPlot({
  print(plotInput2())
})


output$downloadPlot2 <- downloadHandler(
  filename = "Shinyplot2.png",
  content = function(file) {
    png(file)
    plotInput2()
    dev.off()
  })

回答1:


This is highly related to Save plots made in a shiny app

Try adding print(plotInput2()) to downloadHandler instead of plotInput2()

Reason for the need of print() can be found from: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f

It seems that ggplot doesn't draw the plot but only creates a graph object.



来源:https://stackoverflow.com/questions/27008434/downloading-png-from-shiny-r-pt-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!