qplot失败了

时间:2014-08-05 02:54:15

标签: r shiny

我使用以下代码绘制数据集:

  activity <- unlist(dActivityProbe$value)
> tail(activity,10)
 [1] "high" "high" "high" "high" "high" "low"  "high" "none" "none" "none"
> head(dActivityProbe$time,10)
      timestamp 
 [1,] 1406613553
 [2,] 1406613585
 [3,] 1406613586
 [4,] 1406613706
 [5,] 1406613706
 [6,] 1406613825
 [7,] 1406613826
 [8,] 1406613945
 [9,] 1406613947
[10,] 1406614065
> date_time <- as.POSIXlt(as.numeric(dActivityProbe$time), origin="1970-01-01", tz = "GMT")
> head(date_time,10)
 [1] "2014-07-29 05:59:12 GMT" "2014-07-29 05:59:45 GMT" "2014-07-29 05:59:46 GMT" "2014-07-29 06:01:45 GMT"
 [5] "2014-07-29 06:01:46 GMT" "2014-07-29 06:03:45 GMT" "2014-07-29 06:03:45 GMT" "2014-07-29 06:05:45 GMT"
 [9] "2014-07-29 06:05:46 GMT" "2014-07-29 06:07:44 GMT"
> p <- qplot(data=dActivityProbe,,color=activity,x=date_time,y=activity)
> print(p)

这就像生成这个图像的情节一样成功 enter image description here

但问题是,当我试图将此代码移动到闪亮时,我收到了错误。

错误看起来像这样 在R控制台中:

Warning in as.POSIXlt(as.numeric(dActivityProbe$time), origin = "1970-01-01",  :
  NAs introduced by coercion
Error in seq.int(0, to0 - from, by) : 'to' cannot be NA, NaN or infinite

闪亮的网页:

'to' cannot be NA, NaN or infinite

闪亮的应用:

ui.R

  tabPanel("Time Series",
                                              titlePanel("Time Series Activity Probe Plot"),
                                              mainPanel(
                                                   plotOutput("Activity_time_series")
                                              )

dActivityProbe:

 head(dActivityProbe)
        time value
1 1406613553  none
2 1406613585  none
3 1406613586  none
4 1406613706  none
5 1406613706  none
6 1406613825  none

server.R

    library(shiny)
    library("RSQLite")
    library("rjson")
    library("ggplot2")
    library("scales")


    activity <- unlist(dActivityProbe$value)
    date_time <- as.POSIXlt(as.numeric(dActivityProbe$time), origin="1970-01-01", tz = "GMT")

shinyServer(function(input, output) {

         output$Activity_time_series <- renderPlot({
          p <- qplot(data=dActivityProbe,,color=activity,x=date_time,y=activity)
          print(p)

     })

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

我尝试重新创建您的问题,但没有问题也没有错误。 模拟数据并使用排列以使其看起来真实 您确定代码中没有缺少值吗?另一个可能的问题可能是您没有读取服务器中的数据?最后但并非最不重要的是你检查了所有的括号?

以下是代码:

runApp(

  list(

    ui = {
      tabPanel("Time Series",
               titlePanel("Time Series Activity Probe Plot"),
               mainPanel(
                 plotOutput("Activity_time_series")
               )
      )         
    },


    server = {
      library(shiny)
      library("RSQLite")
      library("rjson")
      library("ggplot2")
      library("scales")

      ##Similate the data
      similate<-function(n){
        value<-sample(c("high","low" ,"none"),n,replace=TRUE)
        time<-c(1406613553, 1406613585, 1406613586, 1406613706, 1406613706, 1406613825, 1406613826, 1406613945, 1406613947, 1406614065)
        time_base<-(sapply(time,function(x) as.numeric(unlist(strsplit(toString(x),'')))))
        time<-apply(apply(time_base,1,function(x ) sample(x,n,replace=TRUE)),1,function(x) as.numeric(gsub(", ",'',toString(x))))
        return(data.frame(time=time,value=value))
      }

      dActivityProbe <-similate(1000)

      activity <- unlist(dActivityProbe$value)
      date_time <- as.POSIXlt(as.numeric(dActivityProbe$time), origin="1970-01-01", tz = "GMT")

      shinyServer(function(input, output) {

        output$Activity_time_series <- renderPlot({
          p <- qplot(data=dActivityProbe,,color=activity,x=date_time,y=activity)
          print(p)

        })
      }
      )
    }

  )
)

亲切的问候,