2014-09-25 47 views
0

我有一个名为dash.plot一个绘图功能:R:renderPlot在闪亮没有看到输入数据

dash.plot<-function(var,colors,legend){ 
require(reshape) 
require(ggplot2) 
require(lubridate) 
# Reading in data 
df<-var 
#print(names(df)) 
df_long<-melt(df,id="Date") 
#print(head(df_long)) 
tmp<-ggplot(df_long,aes(x=Date,y=value,color=variable),environment=environment())+geom_line(size=2) 
tmp+ylab("Count of Panelists")+scale_color_manual(values = colors,labels = legend,guide=guide_legend(title=NULL)) 
} 

,当我在自己的运行,其中一期工程闪亮的外(详细了解闪亮here)。我输入的数据是这样的:

>df 
    Date Cumulative TwentyFour SeventyTwo SevenDays 
1 4/1/12   2   5   5   5 
2 4/2/12   9   2   6   6 
3 4/3/12   8   9   14  14 
4 4/4/12   3   8   19  21 
5 4/5/12   3   3   20  23 
6 4/6/12   5   3   14  25 
7 4/7/12   5   5   11  29 
8 4/8/12   5   5   13  33 
9 4/9/12   4   5   15  37 
10 4/10/12   6   4   14  33 
11 4/11/12   1   6   15  31 
12 4/12/12   5   1   11  29 
13 4/13/12   5   5   12  31 
14 4/14/12   8   5   11  31 
15 4/15/12   5   8   18  34 
16 4/16/12   2   5   18  34 
17 4/17/12   5   2   15  32 
18 4/18/12   4   5   12  31 
19 4/19/12   6   4   11  34 
20 4/20/12   4   6   15  35 

我ui.R:

shinyUI(fluidPage(
    titlePanel("User Participation"), 

    sidebarLayout(
     sidebarPanel(h5("Select Parameters:"), 

     dateRangeInput("daterange", "Date range:", 
         start = "2014-01-01", 
         end = Sys.Date(), 
         format = "mm/dd/yy", 
         separator = " - "),    

     selectInput("heartbeats", 
     label = "Choose heartbeats to display:", 
     choices = list("Cumulative Only", "Cumulative, 24 hours", 
        "Cumulative, 24 hours, 72 hours", "Cumulative, 24 hours, 72 hours, 7 days"), 
           selected = "Cumulative Only") 
), 
    mainPanel(h3("Count of Users versus Data Collection Date"), 
      plotOutput("plot")) 


) 
)) 

而且我server.R:我不是在使用输入我的约会

library(reshape) 
library(lubridate) 
library(ggplot2) 

# Sourcing code 
source("../dash.usr.acq.plot.R") 

shinyServer(function(input, output) { 
    # Reading in Data 
    df<-read.csv("/Users/data_location/hb_fdat.csv",header=TRUE) 
    df$Date<-as.Date(df$Date,format="%m/%d/%y") 

    # Rendering plot 
    output$plot<-renderPlot({ 
    data<-switch(input$heartbeats, 
       "Cumulative" = df$Cumulative, 
       "Cumulative, 24 hours" = data.frame(df$Date,df$Cumulative,df$TwentyFour), 
       "Cumulative, 24 hours, 72 hours" = data.frame(df$Date,df$Cumulative,df$TwentyFour,df$SeventyTwo), 
       "Cumulative, 24 hours, 72 hours, 7 days" = data.frame(df$Date,df$Cumulative,df$TwentyFour,df$SeventyTwo,df$SevenDays)) 
    colors<-switch(input$heartbeats, 
       "Cumulative"=c("red"), 
       "Cumulative, 24 hours"=c("red","blue"), 
       "Cumulative, 24 hours, 72 hours"=c("red","blue","green"), 
       "Cumulative, 24 hours, 72 hours, 7 days"=c("red","blue","green","orange")) 
    legend<-switch(input$heartbeats, 
       "Cumulative"=c("Cumulative"), 
       "Cumulative, 24 hours"=c("Cumulative", "24 hours"), 
       "Cumulative, 24 hours, 72 hours"=c("Cumulative", "24 hours","72 hours"), 
       "Cumulative, 24 hours, 72 hours, 7 days"=c("Cumulative", "24 hours","72 hours","7 days")) 

    sdate<-input$daterange[1] 
    edate<-input$daterange[2] 

    dash.plot(var=data, 
         color=colors, 
         legend=legend) 
    }) 

    } 
) 

注范围尚未。再次,当我自己运行dash.plot时,我没有任何问题。但是在Shiny的情况下,我的数据没有被传入。我收到错误消息Error: object 'Date' not found。我尝试了几种不同的解决方案。更改df $日期< -'as.Date(df $ Date,format =“%m /%d/$ y”)'to'df $日期< -as.Date(unclass(unlist(df $ Date)),格式= “%米/%d /%Y”)”,并且还:

在server.RI尝试过使用从dash.plot功能的代码:

output$plot<-renderPlot({ 
    data<-switch(input$heartbeats, 
       "Cumulative" = df$Cumulative, 
       "Cumulative, 24 hours" = data.frame(df$Date,df$Cumulative,df$Twentyfour), 
       "Cumulative, 24 hours, 72 hours" = data.frame(df$Date,df$Cumulative,df$TwentyFour,df$SeventyTwo), 
       "Cumulative, 24 hours, 72 hours, 7 days" = data.frame(df$Date,df$Cumulative,df$TwentyFour,df$SeventyTwo,df$SevenDays)) 
    colors<-switch(input$heartbeats, 
       "Cumulative"=c("red"), 
       "Cumulative, 24 hours"=c("red","blue"), 
       "Cumulative, 24 hours, 72 hours"=c("red","blue","green"), 
       "Cumulative, 24 hours, 72 hours, 7 days"=c("red","blue","green","orange")) 
    legend<-switch(input$heartbeats, 
       "Cumulative"=c("Cumulative"), 
       "Cumulative, 24 hours"=c("Cumulative", "24 hours"), 
       "Cumulative, 24 hours, 72 hours"=c("Cumulative", "24 hours","72 hours"), 
       "Cumulative, 24 hours, 72 hours, 7 days"=c("Cumulative", "24 hours","72 hours","7 days")) 

    sdate<-input$daterange[1] 
    edate<-input$daterange[2] 

    df_long<-melt(data,id="Date") 

    tmp<-ggplot(df_long,aes(x=Date,y=value,color=variable),environment=environment())+geom_line(size=2) 
    print(tmp+ylab("Count of Panelists")+scale_color_manual(values = colors,labels = legend,guide=guide_legend(title=NULL))) 

    }) 

并得到相同的错误消息。我是Shiny新手,不确定如何解决这个问题。谢谢!

回答

0

粘贴此在你server.R,它会工作:

require(reshape) 
require(ggplot2) 
require(lubridate) 

dash.plot<-function(var,colors,legend){ 
    # Reading in data 
    df<-var 
    #print(names(df)) 
    df_long<-melt(df,id="df.Date") 
    #print(head(df_long)) 
    tmp<-ggplot(df_long,aes(x=df.Date,y=value,color=variable),environment=environment() )+geom_line(size=2) 
    tmp+ylab("Count of Panelists")+scale_color_manual(values = colors,labels = legend,guide=guide_legend(title=NULL)) 
} 

shinyServer(function(input, output) { 
     # Reading in Data 
     df<-read.table("data.tsv",header=TRUE) 
     df$Date<-as.Date(df$Date,format="%m/%d/%y") 

     # Rendering plot 
     output$plot<-renderPlot({ 
        browser() 
        dataframe <- df 
        date <- df$Date 
        cum <- df$Cumulative 
        data<-switch(input$heartbeats, 
          # "Cunulative" is not in the list of your selectInput 
          "Cumulative Only" = data.frame(df$Date,df$Cumulative), 
          "Cumulative, 24 hours" = data.frame(df$Date,df$Cumulative,df$TwentyFour), 
          "Cumulative, 24 hours, 72 hours" = data.frame(df$Date,df$Cumulative,df$TwentyFour,df$SeventyTwo), 
          "Cumulative, 24 hours, 72 hours, 7 days" = data.frame(df$Date,df$Cumulative,df$TwentyFour,df$SeventyTwo,df$SevenDays)) 
        colors<-switch(input$heartbeats, 
          "Cumulative Only"=c("red"), 
          "Cumulative, 24 hours"=c("red","blue"), 
          "Cumulative, 24 hours, 72 hours"=c("red","blue","green"), 
          "Cumulative, 24 hours, 72 hours, 7 days"=c("red","blue","green","orange")) 
        legend<-switch(input$heartbeats, 
          "Cumulative Only"=c("Cumulative"), 
          "Cumulative, 24 hours"=c("Cumulative", "24 hours"), 
          "Cumulative, 24 hours, 72 hours"=c("Cumulative", "24 hours","72 hours"), 
          "Cumulative, 24 hours, 72 hours, 7 days"=c("Cumulative", "24 hours","72 hours","7 days")) 

        sdate<-input$daterange[1] 
        edate<-input$daterange[2] 

        dash.plot(var=data, 
          color=colors, 
          legend=legend) 
       }) 

    } 
) 

评论:

  • 你做了几个讨厌错字:有二十四个,而不是为TwentyFour。
  • 列名不再是Date,而是当你的数据框在你的dash.plot函数中时,df.Date。为了解决这个问题,你可以定义你的数据帧为data.frame(Date = df$Date, df$Cumulative)
  • 在switch语句中,您没有“Cumulative”选项,它只是在ui中定义的“仅累积”.R
  • 在“仅累积”特定情况下,您没有返回数据帧但只是一个具有累积数据的向量。当然,无法在该向量中找到Data列。
  • 作为指导,我建议你把你的需求调用放在R函数顶部的函数之外。
+0

非常感谢您的解决方案。在我的文章后,我发现我的拼写错误,但不明白从Date更改为df.Date的列名称。感谢您编写更好的server.R代码的指导方针。 – Archimeow 2014-09-26 16:45:59