-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.R
More file actions
107 lines (85 loc) · 3.54 KB
/
utils.R
File metadata and controls
107 lines (85 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# First we check that the necessary packages are installed
req.packages=c("shinydashboard","shiny","shinyBS","RODBC","DT","plotly","dplyr")
if(any(! req.packages %in% installed.packages()))
stop(
paste0("Not all dependent packages are installed on your computer.\n Please install: ",
paste(req.packages[!req.packages %in% installed.packages()],collapse=","),
". See '?install.packages' for more information on how to install R packages.")
)
require(shiny)
require(shinyBS)
require(shinydashboard)
require(RODBC)
require(DT)
require(plotly)
require(dplyr)
#' Two custom functions are used in this app (ignoring the simple axis
#' formatting function)
#'
#' The first custom function produces a density plot using plotly.
#'
#' As its required arguments, it takes a data frame (dataset) and the
#' corresponding column name (valueCol) from which the density plot is
#' generated.
#'
#' Additional arguments are the group column name (groupCol- if you want
#' multiple curves on the same plot) and from, which determines starting point
#' of the density plot(s) ( density plots can be negative on the x-axis even
#' for a set of positive numbers, setting from=0 would prevent such negative
#' values)
#'
#' The function returns a plotly graph
densityplotly=function(dataset,valueCol,groupCol="",from=NULL){
if(groupCol==""){
if(is.null(from)){
dens=density(dataset[,valueCol])
}else{dens=density(dataset[,valueCol],from=from)}
plot_ly(dens, x = x, y = y)
}else{
if(is.null(from)){
dens=with(dataset, tapply(eval(parse(text = valueCol)), INDEX = eval(parse(text = groupCol)), density))
}else{dens=with(dataset, tapply(eval(parse(text = valueCol)),
INDEX = eval(parse(text = groupCol)),function(x){density(x,from=from)}))}
df <- data.frame(
x = unlist(lapply(dens, "[[", "x")),
y = unlist(lapply(dens, "[[", "y")),
my_group = rep(names(dens), each = length(dens[[1]]$x))
)
plot_ly(df, x = x, y = y, color = my_group)
}
}
#' The second custom function produces a cumulative distribution plot using
#' plotly.
#'
#' As its required arguments, it takes a data frame (dataset) and the
#' corresponding column name (valueCol) from which the cumulative distribution
#' plot is generated.
#'
#' An additional argument (groupCol) is the group column name (if you want
#' multiple curves on the same plot)
#'
#' Note: To prevent the function being too computationally expensive, it only calculates the
#' cumulative value at 1000 points, sampled evenly between the max and min values.
#' For most scenarios, I imagine this produces a relatively smooth curve without slowing
#' performance.
#'
#' The function returns a plotly graph
cumultplotly=function(dataset,valueCol,groupCol=""){
dataset=arrange_(dataset,valueCol)
plot.min=floor(min(dataset[,valueCol]))
plot.max=ceiling(max(dataset[,valueCol]))
plot.range=seq(plot.min,plot.max,(plot.max-plot.min)/1000)
if(groupCol==""){
cudist=ecdf(dataset[,valueCol])
output=data.frame(x=plot.range,y=cudist(plot.range))
plot_ly(output, x = x, y = y*100)
}else{
cudist=with(dataset, tapply(eval(parse(text = valueCol)), INDEX = eval(parse(text = groupCol)), ecdf))
df <- data.frame(
x = rep(plot.range,length(names(cudist))),
y = unlist(lapply(cudist,function(a){a(plot.range)})),
my_group = rep(names(cudist), each = length(plot.range))
)
plot_ly(df, x = x, y = y*100, color = my_group)
}
}