2013-08-04 25 views
10

Điều gì khiến ứng dụng Sáng bóng nhỏ của tôi hiển thị ggplot của tôi? Khi tôi thay thế mã trong hàm renderPlot() bằng một ví dụ bằng cách sử dụng hàm cốt truyện cơ sở, nó đi kèm với nhau. Tôi đang sử dụng RStudio, R v3.0.1 trên Windows Vista, xuất ra trình duyệt Chrome.Sáng bóng không hiển thị ggplot của tôi như tôi mong đợi

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

thử gói bạn ggplot gọi trong 'print' tức là' in (ggplot (...) + geom_point) ' –

+0

Bạn nên hiển thị thông báo lỗi bạn nhận được thay vì chỉ nói "không hoạt động". Jake là đúng mà bạn nên bọc in xung quanh cuộc gọi ggplot của bạn, nhưng tôi nghĩ rằng có cái gì khác sai với cuộc gọi ggplot của bạn (scoping vấn đề). – GSee

Trả lời

17

Có hai vấn đề ở đây.

Trước tiên, bạn không nên đặt trong số aes - nó dự kiến ​​tên cột. Thay vào đó, hãy đặt data.frame mà bạn cung cấp cho ggplot (nhờ @Roland từ R trò chuyện)

Thứ hai, bạn phải rõ ràng đối tượng ggplot trong ứng dụng sáng bóng của mình.

Hãy thử điều này:

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

+1 Đẹp và thanh lịch. Tôi đã không nhìn thấy phản ứng của bạn, hoặc tôi sẽ không đánh máy của tôi. –

3

Mã của bạn cần một vài thay đổi để có được ggplot để render. Như ý kiến ​​ở trên, cần có print(ggplot). Nhưng cũng có, aes bên trong ggplot không thể đối phó với subsetting.

Vì vậy, bạn đặt thành phố của bạn quan tâm trong một phản ứng riêng biệt và gọi từ 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()) 

Các server.R đầy đủ (chỉ hoạt động này)

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

Trong ggplot2 bạn có thể tập hợp con các dữ liệu tổng thể được truyền cho tất cả các lớp (@ câu trả lời GSee của) hoặc, đối với lớp indivdual bạn có thể sử dụng đối số subset để chỉ đặt cho lớp đó. Điều này có thể hữu ích nếu bạn đang xây dựng các ô phức tạp hơn.

Sử dụng chức năng plyr. rất hữu ích vào đây để xây dựng các đối số

# 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

Rất hay. Cảm ơn bạn đã thêm chút thông tin. – cylondude