2013-08-04 2 views
10

Che cosa impedisce alla mia piccola app Shiny di visualizzare il mio ggplot? Quando sostituisco il codice in renderPlot() con un esempio utilizzando la funzione di trama di base, viene fornito insieme. Sto usando RStudio, R v3.0.1 su Windows Vista, in uscita su un browser Chrome.Shiny non visualizza il mio ggplot come mi aspetterei

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

tenta avvolgendo si ggplot chiamare in '' print' cioè di stampa (ggplot (...) + geom_point) ' –

+0

si dovrebbe mostrare il messaggio di errore che stai ricevendo invece di dire "non funziona". Jake ha ragione nel ritirare la stampa sulla tua chiamata ggplot, ma penso ci sia qualcos'altro che non va nella tua chiamata ggplot (problema dell'ambito). – GSee

risposta

17

Ci sono due problemi qui.

In primo luogo, non è necessario impostare il sottoinsieme in aes - si aspetta i nomi delle colonne. Invece, imposta sotto il data.frame fornito a ggplot (grazie a @Roland dalla chat R)

In secondo luogo, devi esplicitamente print il tuo oggetto ggplot nella tua app lucida.

Prova questa:

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

+1 Bello ed elegante. Non avevo visto la tua risposta, altrimenti non avrei digitato il mio. –

3

Il codice ha bisogno di un paio di modifiche per ottenere ggplot per il rendering. Come i commenti sopra dichiarano, era necessario print(ggplot). Ma anche, aes dentro ggplot non può occuparsi di subsetting.

Così si imposta la propria città di interesse in un reattivo separato e lo si chiama da 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()) 

Il server.R completo (questo funziona)

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

In ggplot2 si può sottoinsieme dei dati complessivi di essere passato a tutti i livelli (@ risposta di GSEE) o, per i livelli indivdual è possibile utilizzare l'argomento subset per il sottoinsieme solo per quel livello. Questo può essere utile se stai costruendo grafici più complessi.

Utilizzando la funzione .plyr è utile qui per costruire gli argomenti

# 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

Molto interessante. Grazie per quel tocco in più di informazioni. – cylondude