RShiny:ggplot没有显示但没有显示错误

时间:2015-06-11 13:33:23

标签: r ggplot2 shiny

我试图在Shiny中制作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")

1 个答案:

答案 0 :(得分:1)

在ui。 R脚本,替换为

  mainPanel(
    plotOutput("view")
  )
))

在server.R脚本中替换为     输出$ view&lt; - renderPlot({

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

  })
})