Reputation: 240
I have a plotting function called 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))
}
Which works when I run it on its own, outside of Shiny (learn more about Shiny here ). My input data look like this:
>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
My 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"))
)
))
And my 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)
})
}
)
Note that I am not using the input for my date range yet. Again, when I run dash.plot on its own, I have no trouble. But in the context of Shiny, my data is not being passed in. I receive the error message Error: object 'Date' not found
. I've tried several different solutions. Changing df$Date<-'as.Date(df$Date,format="%m/%d/$y")' to 'df$Date<-as.Date(unclass(unlist(df$Date)),format="%m/%d/%y")' and also:
In server.R I tried just using the code from the dash.plot function:
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)))
})
And received the same error message. I'm new to Shiny and am not sure how to trouble shoot this. Thank you!
Upvotes: 1
Views: 1479
Reputation: 553
Paste this in your server.R and it will work:
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)
})
}
)
Comments:
data.frame(Date = df$Date, df$Cumulative)
.Upvotes: 1