2013-08-04 2 views
10

내 작은 반짝이는 앱이 내 ggplot을 표시하지 못하게하는 이유는 무엇입니까? renderPlot()의 코드를 기본 플롯 기능을 사용하는 예제로 바꾸면 함께 나타납니다. 저는 Windows Vista에서 RStudio, R v3.0.1을 사용하여 Chrome 브라우저에 출력하고 있습니다.반짝이는 예상대로 내 ggplot을 표시하지 않습니다.

ui.r

library(ggplot2) 

cities <- c("Anchorage","Fairbanks","Juenau","Wasilla","Homer") 
years <- 2003:2013 
Table <- "Capital Assets" 
Account <- c("Land", "Art", "Buildings", "Equipment") 
dat <- data.frame(City = sort(rep(cities, length(years))), Year = rep(years,length(cities)), Table) 
sampleDat <- rbind(data.frame(dat,Acount = Account[1]), data.frame(dat, Acount = Account[2]), data.frame(dat, Acount = Account[3]), data.frame(dat, Acount = Account[4])) 
finalDat <- data.frame(sampleDat, Value = runif(length(sampleDat[,1]), 1000,10000)) 

shinyUI(pageWithSidebar(

    headerPanel("CAFR Explorer"), 

    selectInput("city","City", as.list(levels(finalDat$City)), selected = NULL, multiple = FALSE), 

    mainPanel(
    h3(textOutput("caption")), 

    plotOutput("CAFRplot") 
))) 

server.r 것은

library(shiny) 
library(ggplot2) 

cities <- c("Anchorage","Fairbanks","Juenau","Wasilla","Homer") 
years <- 2003:2013 
Table <- "Capital Assets" 
Account <- c("Land", "Art", "Buildings", "Equipment") 
dat <- data.frame(City = sort(rep(cities, length(years))), Year = rep(years,length(cities)), Table) 
sampleDat <- rbind(data.frame(dat,Acount = Account[1]), data.frame(dat, Acount = Account[2]), data.frame(dat, Acount = Account[3]), data.frame(dat, Acount = Account[4])) 
finalDat <- data.frame(sampleDat, Value = runif(length(sampleDat[,1]), 1000,10000)) 

shinyServer(function(input, output) { 

    formulaText <- reactive({ 
    paste(input$city) 
    }) 

    output$caption <- renderText({ 
    formulaText() 
    }) 

    output$CAFRplot <- renderPlot({ 

    ## this one isn't working. 
    ggplot(finalDat, aes(x = finalDat[which(finalDat$City == input$city),2], 
         y = finalDat[which(finalDat$City == input$city),5])) + 
    geom_point() 

    ## this one is working 
    #plot(finalDat[which(finalDat$City == input$city),2], y = finalDat[which(finalDat$City == input$city),5]) 


    }) 
}) 
+3

당신이'print' 즉'인쇄 (ggplot (...) + geom_point)에 전화 ggplot 포장하려고 인수를 구성 여기를 유용'당신은을 –

+0

표시해야합니다 오류 메시지 대신 "작동하지 않습니다."라고 말하면됩니다. Jake는 ggplot 호출에서 인쇄물을 감싸는 것이 옳다.하지만 ggplot 호출에 다른 문제가 있다고 생각한다 (범위 지정 문제). – GSee

답변

17

여기에 두 가지 문제가 있습니다.

먼저 서브 세트로 aes을 입력해야합니다. 열 이름이 필요합니다. 대신 ggplot에 제공된 data.frame을 서브셋하십시오 (R 채팅의 @Roland 덕분에)

두 번째로 반짝이는 앱에서 ggplot 객체를 명시 적으로 print해야합니다.

이 시도 :

p <- ggplot(finalDat[finalDat$City == input$city,], aes(x = Year, y = Value)) 
p <- p + geom_point() 
print(p) 
+1

+1 멋지고 우아합니다. 나는 당신의 반응을 보지 못했습니다. 아니면 제가 타이핑하지 않았을 것입니다. –

3

귀하의 코드가 ggplot 렌더링을 얻기 위해 변화의 몇 가지를 필요로했다. 위의 의견으로 print(ggplot)이 필요했습니다. 또한 ggplot 내부의 aes는 부분 집합을 처리 할 수 ​​없습니다.

그래서 관심있는 도시를 별도의 사후 대응책으로 설정하고 ggplot에서 해당 도시를 호출하십시오.

city.df <- reactive({ 
    subset(finalDat, City == input$city) 
    }) 

    output$CAFRplot <- renderPlot({ 
    city <- city.df() 

    print(ggplot(city, aes(x = Year, y=Value)) + geom_point()) 

전체 server.R (이 작품)는 전체 데이터의 서브 세트를 할 수 ggplot2에서

library(shiny) 
library(ggplot2) 

cities <- c("Anchorage","Fairbanks","Juenau","Wasilla","Homer") 
years <- 2003:2013 
Table <- "Capital Assets" 
Account <- c("Land", "Art", "Buildings", "Equipment") 
dat <- data.frame(City = sort(rep(cities, length(years))), Year = rep(years,length(cities)), Table) 
sampleDat <- rbind(data.frame(dat,Acount = Account[1]), data.frame(dat, Acount = Account[2]), data.frame(dat, Acount = Account[3]), data.frame(dat, Acount = Account[4])) 
finalDat <- data.frame(sampleDat, Value = runif(length(sampleDat[,1]), 1000,10000)) 

shinyServer(function(input, output) { 

    formulaText <- reactive({ 
    paste(input$city) 
    }) 

    output$caption <- renderText({ 
    formulaText() 
    }) 

    city.df <- reactive({ 
    subset(finalDat, City == input$city) 
    }) 

    output$CAFRplot <- renderPlot({ 
    city <- city.df() 
    ## this one isn't working. 
# print(ggplot(finalDat, aes(x = finalDat[which(finalDat$City == input$city),2], 
#       y = finalDat[which(finalDat$City == input$city),5])) + geom_point()) 

    print(ggplot(city, aes(x = Year, y=Value)) + geom_point()) 

    ## this one is working 
    #plot(finalDat[which(finalDat$City == input$city),2], y = finalDat[which(finalDat$City == input$city),5]) 


    }) 
}) 
2

이 indivdual 레이어 (GSEE의 대답 @) 모든 레이어에 전달되고 있거나 수행 할 수 있습니다 subset 인수를 사용하여 해당 계층에 대해서만 서브 세트를 작성하십시오. 이것은보다 복잡한 플롯을 구성 할 때 유용 할 수 있습니다. plyr 기능 .를 사용

# required in server.R (along with the other calls to library) 
library(plyr) 

p <- ggplot(finalDat, aes(y =Year, x = Value)) + 
     geom_point(subset = .(City ==input$city)) 
print(p) 
+0

매우 차가워 요. 그 여분의 정보를 보내 주셔서 감사합니다. – cylondude

관련 문제