2015-06-11 95 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_这段代码是否解决了问题,以及问题实际是什么。任何人来到这个页面。 –