2014-09-25 26 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