2

这与我之前提出的一个问题有关:

从 Shiny (R) 下载 png

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

这是 Shiny 中的错误还是我的代码有问题?

以下是我的服务器文件的相关摘录:

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

1 回答 1

1

这与在闪亮的应用程序中制作的保存图高度相关

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

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

似乎 ggplot 没有绘制绘图,而只是创建了一个图形对象。

于 2014-11-19T11:33:47.770 回答