R:在没有看到输入数据的Shiny中的renderPlot

时间:2014-09-25 21:51:49

标签: r ggplot2 shiny

我有一个名为dash.plot的绘图功能:

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))
}

当我在Shiny之外单独运行它时有效(了解有关Shiny 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"))


  )
))

我的服务器.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 $ Date&lt; - 'as.Date(df $ Date,format =“%m /%d / $ y”)'更改为'df $ Date&lt; -as.Date(unclass(unlist(df $ Date)), format =“%m /%d /%y”)'以及:

在server.R中,我尝试使用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的新手,我不确定如何解决这个问题。谢谢!

1 个答案:

答案 0 :(得分: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而不是TwentyFour。
  • 当您的数据框位于dash.plot函数中时,列名称不再是Date,而是df.Date。要解决此问题,您可以将数据框定义为data.frame(Date = df$Date, df$Cumulative)
  • 在切换声明中,您没有选项&#34;累积&#34;,它是&#34;仅累计&#34;正如你在ui.R
  • 中定义的那样
  • 在&#34;仅累计&#34;的特定情况下您没有返回数据帧,只返回包含累积数据的向量。当然,无法在该向量中找到数据列。
  • 作为指导,我建议您将函数置于R文件顶部的函数之外。