2015-06-11 110 views
0

我試圖讓ggplot在Shiny中,當我運行代碼時沒有顯示錯誤,但圖形也沒有顯示出來。然而,側面板和其他一切確實如此。RShiny:ggplot沒有顯示,但沒有顯示錯誤

這裏有一個情節

c<-ggplot(avgscore,aes(x=V1,y=V2)) 

c+geom_bar(stat="identity", position="identity")+ggtitle('Average Score Difference from TimeID to the Following TimeID')+xlab('TimeID')+ylab('Average Score Difference') 

c<-ggplot(avgcorrect,aes(x=V1,y=V2)) 

c+geom_bar(stat='identity',position = "identity")+ggtitle('Average Correctness Difference from TimeID to the Following TimeID')+xlab('TimeID')+ylab('Average Correctness Difference') 

任何幫助將是巨大的我的非閃亮的代碼!

server.R

library(shiny) 
library(ggplot2) 

avgscore <- read.csv("avgscore.csv") 
avgcorrect <- read.csv("avgcorrect.csv") 
avgscore$V2 <- as.numeric(as.character(avgscore$V2)) 
avgcorrect$V2 <- as.numeric(as.character(avgcorrect$V2)) 

shinyServer(function(input, output) { 
    datasetInput <- reactive({ 
    switch(input$dataset, 
      "Average Score Difference" = avgscore, 
      "Average Correctness Difference" = avgcorrect) 
    }) 

    output$view <- renderPlot({ 

    p <- ggplot(datasetInput(),aes(x=input$V1,y=input$V2)) 
    p <- p + geom_bar(stat="identity", position="identity")+xlab('TimeID')+ylab('Average') 
    print(p) 

    }) 
}) 

ui.R

shinyUI(pageWithSidebar(
    headerPanel("Differences from Time ID to following TimeID"), 
    sidebarPanel(
    selectInput("dataset", "Choose:", 
      choices = c("Average Score Difference", "Average Correctness Difference")) 
), 

    mainPanel(
    htmlOutput("view") 
) 
)) 

這裏是我的數據

dput(avgscore) 

structure(list(V1 = 1:35, V2 = c(0.020736019, 0.012402493, 0.007433475, 
0.008633207, -0.005671281, -0.003544342, 0.009008778, 0.018402863, 
-0.013327797, 0.012969983, -0.014663457, 0.006726486, -0.00515588, 
-0.000418491, -0.017539025, 0, 0.017506009, -0.009978558, 0, 
0, -0.006801574, -0.008229613, 0.023918279, 0, 0, 0, 0, 0, 0, 
0, 0, -0.066688896, 0, 0, 0)), .Names = c("V1", "V2"), row.names = c(NA, 
-35L), class = "data.frame") 


dput(avgcorrect) 

structure(list(V1 = 1:35, V2 = c(0.021733216, 0.013334587, 0.008553191, 
0.00855159, -0.006403273, -0.002158895, 0.00866897, 0.008795382, 
-0.012207984, 0.013456153, -0.018823251, 0.006726212, -0.003551157, 
-0.000418491, -0.017989486, 0, 0.017506009, -0.009978558, 0, 
0, -0.013603148, 0, 0.023918279, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0)), .Names = c("V1", "V2"), row.names = c(NA, -35L), class = "data.frame") 
+0

'renderPlot'應該去'plotOutput',不'htmlOutput '。 –

+0

完美,謝謝 – Una

+0

沒問題。繼續並接受你的回答以關閉這個問題。 –

回答

0

在UI擊穿。 [R腳本,替換該

mainPanel(
    plotOutput("view") 
) 
)) 

而在server.R腳本 輸出$視圖<更換 - renderPlot({

p <- ggplot(datasetInput(),aes(x=V1,y=V2)) 
p <- p + geom_bar(stat="identity", position="identity")+xlab('TimeID')+ylab('Average') 
print(p) 

    }) 
}) 
+1

請解釋_why_這段代碼是否解決了問題,以及問題實際是什麼。任何人來到這個頁面。 –