-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynapseUtil.R
105 lines (89 loc) · 2.91 KB
/
synapseUtil.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
## This package uses the synapse web platform at http://synapse.org
# you must first register and update your config
##we use the synapse python client and reticulate
##
#####Synapse python client wrapper files
#condaenv='C:\\Users\\gosl241\\AppData\\Local\\r-miniconda\\envs\\synapseclient'
#' @export
loadSynapse<-function(){
#library(reticulate)
#have_synapse <- reticulate::py_module_available("synapseclient")
# if (!have_synapse)
#library(reticulate)
reticulate::use_virtualenv('r-reticulate')
# reticulate::use_condaenv(condaenv)
synapseclient<-reticulate::import('synapseclient')
syn<-synapseclient$login()
return(syn)
# syn_client <<-
# reticulate::import("synapseclient", delay_load = TRUE)$login()
}
#' Logs into Synapse using local information
#' @name synapseLogin
#' @description Logs into synapse using `synapseclient` python library
#' @return Synapse login python entity
#' @export
synapseLogin<-function(){
# library(reticulate)
#reticulate::use_condaenv(condaenv)
reticulate::use_virtualenv('r-reticulate')
# reticulate::use_condaenv(condaenv)
synapseclient<-reticulate::import('synapseclient')
syn<-synapseclient$login()
return(syn)
}
#' Synapse store
#' @name synapseStore
#' @description stores file in synapse
#' @param path to file
#' @param parentId of folder to store
#' @export
synapseStore<-function(path,parentId){
# reticulate::use_condaenv(condaenv)
synapse=reticulate::import('synapseclient')
sync=synapse$login()
sync$store(synapse$File(path,parentId=parentId))
}
#' Synapse table store
#' REQUIRES PANDAS ON YOUR PYTHON PATH
#' @name synTableStore
#' @description Description
#' @param table to store on synapse
#' @param tabname name of table
#' @param parentId id of project
#' @export
synTableStore<-function(tab,tabname,parentId='syn22128879'){
#we have to first write the table to a file, then build it and store it
#library(reticulate)
print(head(tab))
fpath=write.table(tab,file='tmp.csv',sep=',',row.names = FALSE,quote=FALSE)
# reticulate::use_condaenv(condaenv)
synapse=reticulate::import('synapseclient')
tab<-synapse$build_table(tabname,parentId,'tmp.csv')
sync=synapse$login()
sync$store(tab)
}
#' query synapse table
#' This is how you get data from the project
#' @name querySynapseTable
#' @description queries synapse table
#' @param tableid synapse id of table
#' @export
querySynapseTable<-function(tableid){
syn=synapseLogin()
res<-syn$tableQuery(paste('select * from',tableid))$asDataFrame()
return(res)
}
#' query synapse table more selectively
#' This is how you get data from the project
#' @name queryPartSynapseTable
#' @description queries synapse table
#' @param tableid synapse id of table
#' @param query1
#' @param query2
#' @export
queryPartSynapseTable<-function(tableid, query1, query2){
syn=synapseLogin()
res<-syn$tableQuery(paste('select', query1, 'from', tableid, 'where', query2))$asDataFrame()
return(res)
}