-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevium.R
349 lines (285 loc) · 9.75 KB
/
devium.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#debugging stopped working/updating?
#-------------------
app.state<-reactive({
obj<-get(input$debug_object)
str(obj[[input$debug_name]])
cat("------------------------\n")
obj[[input$debug_name]]
})
ui_Debug<-function(){
list(
selectInput(inputId = "debug_object", label = "Object", choices = c("input","values"), selected = "input", multiple = FALSE),
selectInput(inputId = "debug_name", label = "Name", choices = "", multiple = FALSE)
)
}
debug.names<-reactive({
tryCatch(names(get(input$debug_object)),error=function(e){return()})
})
observe({
updateSelectInput(session, "debug_name", choices = debug.names() )
})
#debugging print all names and values in input
output$debug<- renderPrint({
if(is.null(input)) return()
# if(input$debug_action==0) return()
# obj<-names(input)
# input.obj<-lapply(1:length(obj), function(i) { input[[obj[i]]]})
# names(input.obj)<-obj
# obj<-names(values)
# values.obj<-lapply(1:length(obj), function(i) { values[[obj[i]]]})
# names(values.obj)<-obj
app.state()
})
#function to copy all contents of input
copy.input<-function(){
obj<-tryCatch(names(get("input")),error=function(e){return()})
input.obj<-lapply(1:length(obj), function(i) { input[[obj[i]]]})
names(input.obj)<-obj
return(input.obj)
}
#helper for data transposing mechanism, which was a horrible idea!
rdy.t<-function(obj){
list<-dimnames(obj)
names<-lapply(seq(list), function(i){
tmp<-check.fix.names(fixlc(list[[i]]),ok.chars=c(".","_"))
test<-!is.na(as.numeric(tmp))
paste(ifelse(test,"X",""),tmp,sep="")
})
out<-as.data.frame(obj)
dimnames(out)<-names
return(data.frame(out))
}
################################################################
# functions used in radyant
################################################################
varnames2 <- function() {
if(is.null(input$datasets)) return()
dat <- getdata2()
cols <- colnames(dat)
names(cols) <- paste(cols, " {", sapply(dat,class), "}", sep = "")
cols
}
changedata <- function(addCol = NULL, addColName = "") {
# function that changes data as needed
if(is.null(addCol) || addColName == "") return()
# We don't want to take a reactive dependency on anything
isolate({
values[[input$datasets]][,addColName] <- addCol
})
}
changedataRows <- function(addRow = NULL, addRowName = "") {
# function that changes data as needed
if(is.null(addRow) || addRowName == "") return()
# We don't want to take a reactive dependency on anything
isolate({
values[[input$datasets]][addRowName,] <- addRow
})
}
getdata <- function(dataset = input$datasets) {
values[[dataset]]
}
getdata2 <- function(dataset = input$datasets) { #used for transposable data fxns
rdy.t(values[[input$datasets]])
}
loadUserData <- function(uFile) {
ext <- file_ext(uFile)
objname <- robjname <- sub(paste(".",ext,sep = ""),"",basename(uFile))
ext <- tolower(ext)
if(ext == 'rda' || ext == 'rdata') {
# objname will hold the name of the object inside the R datafile
objname <- robjname <- load(uFile)
values[[robjname]] <- get(robjname)
}
if(datasets[1] == '') {
datasets <<- c(objname)
} else {
datasets <<- unique(c(objname,datasets))
}
if(ext == 'sav') {
values[[objname]] <- read.sav(uFile)
} else if(ext == 'dta') {
values[[objname]] <- read.dta(uFile)
} else if(ext == 'csv') {
values[[objname]] <- read.csv(uFile)
}
datasets <<- unique(c(robjname,datasets))
}
loadPackData <- function(pFile) {
robjname <- data(list = pFile)
dat <- get(robjname)
if(pFile != robjname) return("R-object not found. Please choose another dataset")
if(is.null(ncol(dat))) {
# values[[packDataSets]] <- packDataSets[-which(packDataSets == pFile)]
return()
}
values[[robjname]] <- dat
if(datasets[1] == '') {
datasets <<- c(robjname)
} else {
datasets <<- unique(c(robjname,datasets))
}
}
#################################################
# reactive functions used in radyant
#################################################
uploadfunc <- reactive({
# if(input$upload == 0) return("")
# fpath <- try(file.choose(), silent = TRUE)
# if(is(fpath, 'try-error')) {
# return("")
# } else {
# return(fpath)
# }
values$fpath <- ""
if(!is.null(input$upload)){ #***
if (interactive() == TRUE) {
if (input$upload != 0) {
fpath <- try(file.choose(), silent=TRUE)
if(is(fpath, 'try-error')) values$fpath <- "" else values$fpath<-fpath
}
} else {
if (!is.null(input$serv_upload) && nrow(input$serv_upload) != 0) {
values$fpath <- input$serv_upload[1,'datapath']
# values$fpath <- ""
}
}
}
values$fpath
})
output$upload_local_server <- renderUI({ # data upload function
if (interactive() == TRUE) {
actionButton("upload", "Choose a file") # doesn't seem to do anything?
} else {
fileInput('serv_upload','')
}
# read.csv(input$file2server$data)
# fpath <- uploadfunc()
# fileInput('file2server', 'Choose a file')
# if (is.null(input$file2server)) return("")
})
output$downloadData <- downloadHandler(
filename = function() { paste(input$datasets[1],'.',input$saveAs, sep='') },
content = function(file) {
ext <- input$saveAs
robj <- input$datasets[1]
assign(robj, getdata())
if(ext == 'rda' || ext == 'rdata') {
save(list = robj, file = file)
}
else if(ext == 'dta') {
write.dta(get(robj), file)
} else if(ext == 'csv') {
write.csv(get(robj), file)
}
}
)
output$datasets <- renderUI({
fpath <- uploadfunc()
# loading user data
if(fpath != "" ) loadUserData(fpath)
# # loading package data
# if(input$packData != "") {
# if(input$packData != lastLoaded) {
# loadPackData(input$packData)
# lastLoaded <<- input$packData
# }
# }
# Drop-down selection of data set
selectInput(inputId = "datasets", label = "Datasets:", choices = datasets, selected = datasets[1], multiple = FALSE)
# selectInput(inputId = "datasets", label = "Datasets:", choices = values$datasetlist, selected = values$datasetlist[1], multiple = FALSE)
})
output$packData <- renderUI({
selectInput(inputId = "packData", label = "Load package data:", choices = packDataSets, selected = '', multiple = FALSE)
})
output$columns <- renderUI({
cols <- varnames()
selectInput("columns", "Select columns to show:", choices = as.list(cols), selected = names(cols), multiple = TRUE)
})
output$nrRows <- renderUI({
if(is.null(input$datasets)) return()
dat <- getdata()
# number of observations to show in dataview
nr <- nrow(dat)
sliderInput("nrRows", "Rows to show (max 50):", min = 1, max = nr, value = min(15,nr), step = 1)
})
################################################################
# Data reactives - view, plot, transform data, and log your work
################################################################
output$dataviewer <- renderTable({
if(is.null(input$datasets) || is.null(input$columns)) return()
dat <- getdata()
# not sure why this is needed when files change ... but it is
# without it you will get errors the invalid columns have been
# selected
if(!all(input$columns %in% colnames(dat))) return()
if(!is.null(input$sub_select) && !input$sub_select == 0) {
isolate({
if(input$dv_select != '') {
selcom <- input$dv_select
selcom <- gsub(" ", "", selcom)
if(nchar(selcom) > 30) q()
if(length(grep("system",selcom)) > 0) q()
if(length(grep("rm\\(list",selcom)) > 0) q()
# use sendmail from the sendmailR package -- sendmail('','[email protected]','test','test')
# first checking if selcom is a valid expression
parse_selcom <- try(parse(text = selcom)[[1]], silent = TRUE)
if(!is(parse_selcom, 'try-error')) {
seldat <- try(eval(parse(text = paste("subset(dat,",selcom,")")[[1]])), silent = TRUE)
if(is.data.frame(seldat)) {
return(seldat[, input$columns, drop = FALSE])
}
}
}
})
}
# Show only the selected columns and no more than 50 rows at a time
nr <- min(input$nrRows,nrow(dat))
data.frame(dat[max(1,nr-50):nr, input$columns, drop = FALSE])
})
################################################################
# Output controls for the Summary, Plots, and Extra tabs
# The tabs are re-used for various tools. Depending on the tool
# selected by the user the appropriate analysis function
# is called.
# Naming conventions: The reactive function to be put in the
# code block above must be of the same name as the tool
# in the tools drop-down. See global.R for the current list
# of tools (and tool-names)
################################################################
### Creating dynamic tabsets - From Alex Brown
# Generate output for the summary tab
# output$summary <- renderUI(function() {
output$summary <- renderPrint({
if(is.null(input$datasets) || input$tool == 'dataview') return()
# get the summary function for currenly selected tool and feed
# it the output from one of the analysis reactives above
# get-function structure is used because there may be a large
# set of tools that will have the same output structure
f <- get(paste("summary",input$tool,sep = '.'))
result <- get(input$tool)()
if(is.character(result)) {
cat(result,"\n")
} else {
f(result)
}
})
# Generate output for the plots tab
output$plots <- renderPlot({
# plotting could be expensive so only done when tab is being viewed
if(input$tool == 'dataview' || input$analysistabs != 'Plots') return()
f <- get(paste("plot",input$tool,sep = '.'))
result <- get(input$tool)()
if(!is.character(result)) {
f(result)
} else {
plot(x = 1, type = 'n', main="No variable selection made", axes = FALSE, xlab = "", ylab = "")
}
}, width=700, height=700)
#controls for plotting tab
# Generate output for the plots tab
output$plot_control <- renderUI({
# plotting could be expensive so only done when tab is being viewed
if(input$tool == 'dataview' || input$analysistabs != 'Plots') return()
out<-get(paste("plot_control",input$tool,sep = '.'))()
if(exists("out")){out}
})