-
Notifications
You must be signed in to change notification settings - Fork 3
/
dynamicplot.R
131 lines (124 loc) · 5.28 KB
/
dynamicplot.R
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
library(tidyverse) #use dev version to prevent reactive dplyr grouping crashes
#create the inputs for the section modules
boxUI <- function(ID) {
ns <- NS(ID) #generate namespace
wellPanel(
#section title is abbreviated to namespace-t
textInput(ns("t"),"Section Title",placeholder = "Section Title"),
#sction comment is namespace-c
textAreaInput(ns("c"),"Comments",placeholder="Add commments here (markdown accepted)"),
actionLink(ns("modalBtn"),"Plot Builder",icon("wrench")),
#section code is namespace-e
tags$div(id=ns("e"),name=ns("e"),class="editor form-group shiny-input-container"),
plotOutput(ns("plot"))
)
}
#create the outputs for the section modules
dynPlot <- function(input,output,session,ID) {
#plot builder modal, see plotbuilder.js for more details
observeEvent(input$modalBtn, {
showModal(modalDialog(
title = paste(input$t,"Plot Builder"),
fluidRow(
column(4,
sliderInput("filter.voltage","Select a voltage range:",filter.min()[1],filter.max()[1],
c(filter.min()[1],filter.max()[1]),0.1),
sliderInput("filter.totaltime","Select a total time range:",filter.min()[4],filter.max()[4],
c(filter.min()[4],filter.max()[4]),1)
),
column(4,
sliderInput("filter.current","Select a current range:",filter.min()[2],filter.max()[2],
c(filter.min()[2],filter.max()[2]),0.1),
sliderInput("filter.steptime","Select a step time range:",filter.min()[5],filter.max()[5],
c(filter.min()[5],filter.max()[5]),1)
),
column(4,
sliderInput("filter.amphours","Select a capacity range:",filter.min()[3],filter.max()[3],
c(filter.min()[3],filter.max()[3]),0.1),
sliderInput("filter.step","Is there a step to isolate? (0 for no)",0,
tryCatch(max(db()$Step[db()$Step<=ceiling(quantile(
db()$Step,.99))]),error=function(e) 1),0,1)
)
),
tabsetPanel(
tabPanel(
"Quick",
br(),
fluidRow(
column(4,radioButtons("modalq","Capacity Checks",
c("Discharge Capacity","Reserve Capacity","Ah In During Charge","%Ah In During Charge","Time to 100% Recharge Factor"))),
column(4,radioButtons("modalq","Cold Cranking/Charge Acceptance",
c("CCA Duration","Voltage After 30s","Time to 6V","Current at 10m","Ah In at 10m"))),
column(4,radioButtons("modalq","Cycling",
c("Minimum Discharge Voltage","BOC Voltage","EOC Voltage","BOC Current","EOC Current")))
)
),
tabPanel(
"Continuous",
br(),
fluidRow(
column(6,selectInput("modalcx","X-axis",colnames(db()))),
column(6,selectInput("modalcy","Y-axis",colnames(db())))
)
),
tabPanel(
"Discrete",
br(),
fluidRow(
column(4,selectInput("modaldx","Known variable",colnames(db()))),
column(4,textInput("modaldv","Value of known variable:",placeholder="Numeric, or min/max")),
column(4,selectInput("modaldy","Unknown variable",colnames(db())))
)
)
),
footer=fluidRow(
column(2,actionButton("modalbar","Bar Graph",icon("bar-chart"),class="btn-primary")),
column(2,actionButton("modalbox","Box Plot",icon("archive"),class="btn-primary")),
column(2,actionButton("modalline","Line (All)",icon("line-chart"),class="btn-primary")),
column(2,actionButton("modalline2","Line (Avg)",icon("area-chart"),class="btn-primary")),
column(4,modalButton("Dismiss",icon("remove")))
),
#this variable will be used to identify the code editor to modify. also initializing buttons.
tags$script(paste0("var modalID='",ID,"-e';document.getElementById('modalbar').onclick=modalBar;document.getElementById('modalbox').onclick=modalBox;document.getElementById('modalline').onclick=modalLine;document.getElementById('modalline2').onclick=modalLine2;radioSet('modalq','Discharge Capacity');")),
size="l",
easyClose = T
))
})
#the output plot
output$plot <- renderPlot(if(!is.null(input$e)){
x <- db()
eval(parse(text=input$e))
} else(ggplot())
)
}
#merge all outputs in a markdown format
dynReport <- function(input,output,session) {
if(nchar(input$t)) chunkTitle <- gsub("[[:punct:]]","",make.names(input$t))
else chunkTitle <- ""
code <- {
if(!length(input$e)) "x %>% ggplot()"
else input$e
}
sprintf(
"
##%s
%s
```{r %s}
%s
```
",input$t,input$c,chunkTitle,input$e
)
}
aceBuilder <- function(ID,cont) {
runjs(paste0("aceBuilder('",ID,"-e',",jsQuote(cont),");"))
}
runjs <- function(js) session$sendCustomMessage("run",js)
#correctly escape string before sending to javascript
jsQuote <- function(x) gsub("\n","\\\\n",shQuote(x))
colorline <- function(dataset) {
dataset <- dataset[!duplicated(dataset$Battery.ID),c("Type","Battery.ID")] %>%
arrange(Battery.ID)
spl <- split(dataset$Battery.ID,dataset$Type)
dataset$Line <- unsplit(lapply(spl,seq_along),dataset$Type)
dataset
}