2014-11-19 3 views
1

이것은 내가에 이전에 요구했다 질문에 관련 : 지금 여러 반짝 플롯을 생성하고이를 다운로드 한Shiny (R) pt에서 png 다운로드 중입니다. 2

Downloading png from Shiny (R)

하지만 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() 
    }) 

답변

1

이것은 매우 Save plots made in a shiny app 관련이

보십시오 (인쇄의 필요성 대신 plotInput2()

이유의 downloadHandler하는 print(plotInput2()) 추가)는 다음에서 찾을 수 있습니다. http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f

ggplot은 플롯을 그리지는 않지만 그래프 객체 만 생성하는 것으로 보입니다.

+0

@Mikael Jumppanen, 고맙습니다. 나는 여전히 왜 그런지 잘 모르겠다. 위의 코드는 다른 플롯에서 잘 작동했다. 막대 그래프, 막대 그래프 또는 산점도. 그 이유에 대해 통찰력이 있다면, 나는 알고 싶어 할 것입니다. – sba

+0

원래 답변에 설명을 추가했습니다. –