-
Notifications
You must be signed in to change notification settings - Fork 0
/
metis.R
229 lines (187 loc) · 6.43 KB
/
metis.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
################################################################
# functions used in metis
################################################################
varnames <- function() {
if(is.null(input$datasets)) return()
dat <- getdata()
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
})
}
getdata <- function(dataset = input$datasets) {
values[[dataset]]
}
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)
}
}
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 metis
#################################################
uploadfunc <- reactive({
if(input$upload == 0) return("")
fpath <- try(file.choose(), silent = TRUE)
if(is(fpath, 'try-error')) {
return("")
} else {
return(fpath)
}
})
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)
})
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 appropropriate analaysis 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)