Skip to content

Commit d098ce3

Browse files
committed
only accept .rds files from R
1 parent 18ef918 commit d098ce3

2 files changed

Lines changed: 37 additions & 45 deletions

File tree

server.R

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -161,42 +161,32 @@ nwinit <- reactive({
161161
} else {
162162
filepath <- input$rawdatafile[1,4]
163163
filename <- input$rawdatafile[1,1]
164-
}
165-
if(input$filetype == 1){
166-
loadfile <- ''
167-
if(!is.null(input$rawdatafile)){
168-
nw_var <- tryCatch({
169-
obj <- load(paste(filepath))
170-
}, error = function(err){
171-
return("Chosen file is not an R object")
172-
}, finally = NULL
173-
)
174-
try(nw_var <- get(obj))
175-
}
176-
} else if(input$filetype == 2){
177-
if(!is.null(input$rawdatafile)){
164+
fileext <- substr(filename,nchar(filename)-3,nchar(filename))
165+
166+
if(input$filetype == 1){
167+
if(fileext %in% c(".rds", ".Rds", ".RDs", ".RDS")){
168+
nw_var <- readRDS(paste(filepath))
169+
} else {
170+
return("Upload a .rds file")
171+
}
172+
173+
} else if(input$filetype == 2){
178174
nw_var <- "Upload a .net file"
179-
if(substr(filename,nchar(filename)-3,nchar(filename))==".net" |
180-
substr(filename,nchar(filename)-3,nchar(filename))==".NET"){
175+
if(fileext %in% c(".net", ".NET")){
181176
nw_var <- read.paj(paste(filepath))
182177
}
183-
}
184-
} else if(input$filetype == 3){
185-
if(!is.null(input$rawdatafile)){
178+
} else if(input$filetype == 3){
186179
nw_var <- "Upload a .paj file"
187-
if(substr(filename,nchar(filename)-3,nchar(filename))==".paj" |
188-
substr(filename,nchar(filename)-3,nchar(filename))==".PAJ"){
180+
if(fileext %in% c(".paj",".PAJ")){
189181
nws <- read.paj(paste(filepath))
190182
if(!is.null(pajnws())){
191183
nw_var <- nws$networks[[as.numeric(input$choosepajnw)]]
192184
}
193185
}
194-
}
195-
} else if(input$filetype == 4){
196-
loadfile <- ''
197-
if(!is.null(input$rawdatafile)){
186+
187+
} else if(input$filetype == 4){
198188
nw_var <- "Input the specified type of matrix"
199-
if(substr(filename,nchar(filename)-3,nchar(filename))==".csv"){
189+
if(fileext %in% c(".csv",".CSV")){
200190
header <- TRUE
201191
row_names<-1
202192
if(input$matrixtype == "edgelist"){
@@ -210,24 +200,24 @@ nwinit <- reactive({
210200
ignore.eval=FALSE, names.eval='edgevalue')
211201
})
212202

213-
}
214-
try({
215-
newmx <- load(paste(filepath))
216-
nw_var <- network(get(newmx),
203+
} else if(fileext %in% c(".rds", ".Rds", ".RDs", ".RDS")){
204+
newmx <- readRDS(paste(filepath))
205+
nw_var <- network(newmx,
217206
directed=input$dir, loops=input$loops,
218207
multiple=input$multiple, bipartite=input$bipartite,
219208
matrix.type=input$matrixtype,
220209
ignore.eval=FALSE, names.eval='edgevalue')
221-
})
222-
}
210+
211+
}
223212

224-
} else if(input$filetype ==5){
225-
if(input$samplenet == "None"){
226-
nw_var <- NULL
227-
} else {
228-
nw_var <- eval(parse(text = input$samplenet))
229-
if(!is.element('bipartite',names(nw_var$gal))){
230-
set.network.attribute(nw_var,'bipartite',FALSE)
213+
} else if(input$filetype ==5){
214+
if(input$samplenet == "None"){
215+
nw_var <- NULL
216+
} else {
217+
nw_var <- eval(parse(text = input$samplenet))
218+
if(!is.element('bipartite',names(nw_var$gal))){
219+
set.network.attribute(nw_var,'bipartite',FALSE)
220+
}
231221
}
232222
}
233223
}

ui.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,11 @@ url = {http://statnetproject.org}
237237
fluidRow(
238238
column(6,
239239
selectInput('filetype',label='File type',
240-
choices=c('built-in network'=5, 'statnet network object (R-object)'=1,
241-
'Pajek network (*.net)'=2,'Pajek project (*.paj)'=3,
242-
'matrix of relational data (*.csv or R-object)'=4))
240+
choices=c('built-in network'=5,
241+
'statnet network object (*.rds)'=1,
242+
'matrix of relational data (*.csv or *.rds)'=4,
243+
'Pajek network (*.net)'=2,
244+
'Pajek project (*.paj)'=3))
243245
),
244246
conditionalPanel(condition = 'input.filetype < 5',
245247
column(6,
@@ -312,10 +314,10 @@ url = {http://statnetproject.org}
312314
column(6,
313315
uiOutput('pajchooser'))),
314316
conditionalPanel(condition='input.filetype == 1',
315-
p(class="helper", id="Robjhelp", icon("question-circle"), span("What is an R-object?", style="font-size:0.85em;")),
317+
p(class="helper", id="Robjhelp", icon("question-circle"), span("What is a .rds file?", style="font-size:0.85em;")),
316318
div(class="mischelperbox", id="Robjbox", 'When working in R, an object in your environment',
317-
'can be saved to a file from the command line in the following way:',
318-
code('save(objectname, file="newfilename")'),br(),'By default the file will be saved',
319+
'can be saved to a .rds file from the command line in the following way:',
320+
code('saveRDS(objectname, file="newfilename.rds")'),br(),'By default the file will be saved',
319321
'into the current working directory. The full path to a new location can be',
320322
'specified in the ', code('file='), 'argument, or set', code('file=file.choose(new=TRUE)'),
321323
'to use a save dialog box.')

0 commit comments

Comments
 (0)