从Shiny(R)pt下载png 2 [英] Downloading png from Shiny (R) pt. 2

查看:133
本文介绍了从Shiny(R)pt下载png 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这涉及到我以前问过的一个问题:

This relates to a question I have asked previously at:

从Shiny(R)下载png

我现在已经创建了多个闪亮的地块,并下载了它们,但是对于使用maptools包,我只是得到一个空的png。

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


推荐答案

这与在闪亮的应用程序中保存图表

尝试添加 print(plotInput2()) to downloadHandler而不是 plotInput2()

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

可以找到需要print()的原因来自: http: //cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f

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

看来,ggplot不绘制情节,但只创建一个图形对象。

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

这篇关于从Shiny(R)pt下载png 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆