2016-04-17 10 views
2

Ich habe an einem animierten Slider-Projekt in Shiny gearbeitet und ich fast habe, was ich bin nach, aber nicht ganz. Anstatt jeden aufeinanderfolgenden Graphen in der animierten Sequenz anzuzeigen, scheint er den Graphen mit allen Daten zu zeigen (nicht sequenziert). Ich bin nicht ganz sicher, wo mein Fehler ist, aber ich vermute, dass es in der reaktiven Funktionsaufruf oder der RenderPlot Funktionsaufruf in der Server-Sektion ist. Ich habe versucht, das Web zu durchsuchen, und ich habe versucht, verschiedene Codeblöcke an verschiedenen Orten zu platzieren, aber ich kann nicht scheinen, dass die Animation in Shiny funktioniert. Irgendwann möchte ich die numerischen Monate (1,2,3 ...) zu Datum-Objekten für mehr Klarheit ändern, aber ich werde diesen angehen, nachdem die Animation funktioniert.Shiny mit animiertem Scatterplot in R-Basisgrafik oder ggplot möglich?

Bitte beachten - Ich habe ein erfolgreiches Bewegungsdiagramm für diese Daten mit googleVic, gvisMotionChart und Shiny erhalten, aber ich habe mit diesem Ansatz keine Kontrolle über die Bubble Chart Farben oder die Blasengröße (Ich möchte ein konstante Größe, die viel kleiner ist als Google Blasendiagramm Standardgröße aufgrund der Überlappung). Also hoffe ich, diese Animation mit Rs Basisgrafiken oder mit ggplot zu erreichen. Hier

ist kleiner Satz von Daten darzustellen, was ich verwende:

d1 <- data.table( id = 1:21, 
      Region = rep(c("R1","R2","R3"), each=7), 
      Month = 1, 
      Spend = round(runif(21,100,500)), 
      Age = round(runif(21,72,100)), 
      Color = rep(c("#E69F00","#D55E00","#009E73"),each=7)) 
d2 <- copy(d1) 
d2[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))] 
d3 <- copy(d2)    
d3[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))]     
d4 <- copy(d3)    
d4[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))] 
d5 <- copy(d4)    
d5[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))] 
d6 <- copy(d5)    
d6[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))] 
d7 <- copy(d6)    
d7[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))] 
d8 <- copy(d7)    
d8[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))] 
dat <- rbindlist(list(d1,d2,d3,d4,d5,d6,d7,d8)) 

Hier ist ein animiertes GIF zu zeigen, was die Animation wie mit Basis Grafiken aussehen würde

saveGIF({ 
for(i in 1:8){ 
    plot(dat[Month==i,Age],dat[Month==i,Spend],col=dat[Month==i,Color], 
     pch=16, xlim=c(min(dat$Age)*.95,max(dat$Age)*1.1), 
     ylim=c(min(dat$Spend)*.95,max(dat$Spend)*1.1), 
     xlab="Age in Months",ylab="Dollars", las=1, cex.axis=.7) 
    legend("topright",inset=.05,c("Reg 1","Reg 2","Reg 3"), 
     pch=16,col=c("#E69F00","#D55E00","#009E73"), 
     cex=.8) 
    ani.pause() 
} 
}, interval = 0.25, ani.width = 750, ani.height = 550) 

Das ist mein Strom nonworking Shiny Code

library(shiny) 
library(ggplot2) 

# Shiny app with slider and animation 

# ui section 
ui = fluidPage(

    # Title 
    titlePanel("Spend vs Age by Region"), 

    # Sidebar with slider and controls for animation 
    sidebarLayout(

     # sidebar with slider 
     sidebarPanel(
      # Slider with looping 
      sliderInput("theMonth", "Month", 1, 8, 1, step = 1, 
       animate=animationOptions(interval=1000, loop = T, 
        playButton = T, pauseButton = T)) 
     ), 

     # Show the animated graph 
     mainPanel(
      plotOutput(outputId="case_age_plot") 
     ) 
    ) 
) 

# server section 
server = function(input, output) { 

    # Reactive expression to create data frame and graph 
    aniGraph <- reactive({ 

     # subset the data frame into the portion that has the data for the 
     # graph in the animation sequence 
     dat[Month==input$theMonth,] 

     # create the graph 
     plot(dat[,Age],dat[,Spend],col=dat[,Color], 
      pch=16, xlim=c(min(dat$Age)*.95,max(dat$Age)*1.1), 
      ylim=c(min(dat$Spend)*.95,max(dat$Spend)*1.1), 
      xlab="Age in Months",ylab="Dollars", las=1, cex.axis=.7) 
     legend("topright",inset=.05,c("Reg 1","Reg 2","Reg 3"), 
      pch=16,col=c("#E69F00","#D55E00","#009E73"),cex=.8) 
    }) 

    # Show the graph 
    output$case_age_plot <- renderPlot({ 
     aniGraph() 
    }) 
} 

# run the app 
runApp(list(ui = ui, server = server)) 

Wenn jemand eine Lösung oder Gedanken hat, wäre ich dankbar.

Antwort

3

das Problem war, dass Sie die Teilmenge von dat nicht gespeichert haben. Ich habe Ihren Code leicht modifiziert, um die gleiche Animation wie in der GIF-Animation zu erhalten.

library(shiny) 
library(ggplot2) 

# Shiny app with slider and animation 

# ui section 
ui = fluidPage(

    # Title 
    titlePanel("Spend vs Age by Region"), 

    # Sidebar with slider and controls for animation 
    sidebarLayout(

    # sidebar with slider 
    sidebarPanel(
     # Slider with looping 
     sliderInput("theMonth", "Month", 1, 8, 1, step = 1, 
        animate=animationOptions(interval=1000, loop = T, 
              playButton = T, pauseButton = T)) 
    ), 

    # Show the animated graph 
    mainPanel(
     plotOutput(outputId="case_age_plot") 
    ) 
) 
) 

# server section 
server = function(input, output) { 

    # Reactive expression to create data frame and graph 
    aniGraph <- reactive({ 

    # subset the data frame into the portion that has the data for the 
    # graph in the animation sequence 

    # Save subset of 'dat' and pass it to the plot 
    dat_sub <- dat[Month==input$theMonth,] 

    # create the graph 
    plot(dat_sub[,Age],dat_sub[,Spend],col=dat_sub[,Color], 
     pch=16, xlim=c(min(dat$Age)*.95,max(dat$Age)*1.1), 
     ylim=c(min(dat$Spend)*.95,max(dat$Spend)*1.1), 
     xlab="Age in Months",ylab="Dollars", las=1, cex.axis=.7) 
    legend("topright",inset=.05,c("Reg 1","Reg 2","Reg 3"), 
      pch=16,col=c("#E69F00","#D55E00","#009E73"),cex=.8) 
    }) 

    # Show the graph 
    output$case_age_plot <- renderPlot({ 
    aniGraph() 
    }) 
} 

# run the app 
runApp(list(ui = ui, server = server)) 
+1

Wow, danke dafür. –