-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 17.2 KB
/
.Rhistory
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
write.csv(time_df, path_to_read)
})
}
}
track_time()
track_time()
track_time()
track_time()
track_time <- function(category = "day") {
# Load/Create timesheet
path_to_read <- MyFunctions::my_path("D",name = "project_time_count.csv")
if(category == "day"){
start_time <- Sys.time()
message("Tracking time started.")
on.exit({
end_time <- Sys.time()
time_spent <- end_time - start_time
message("Time spent on the project:", time_spent)
# Create time data frame
df <- data.frame(Start_Time = as.character(start_time), End_Time = as.character(end_time), Time_Spent = as.numeric(time_spent))
if(file.exists(path_to_read)){
time_df <- dplyr::bind_rows(readr::read_csv(path_to_read),
df)
}else{
time_df <- data.frame(Start_Time = start_time, End_Time = end_time, Time_Spent = time_spent)
}
# Save the data frame to a file
write.csv(time_df, path_to_read)
})
}
}
track_time()
track_time <- function(category = "day") {
# Load/Create timesheet
path_to_read <- MyFunctions::my_path("D",name = "project_time_count.csv")
if(category == "day"){
start_time <- Sys.time()
message("Tracking time started.")
on.exit({
end_time <- Sys.time()
time_spent <- end_time - start_time
message("Time spent on the project:", time_spent)
# Create time data frame
df <- data.frame(Start_Time = as.character(start_time), End_Time = as.character(end_time), Time_Spent = as.numeric(time_spent))
if(file.exists(path_to_read)){
time_df <- dplyr::bind_rows(readr::read_csv(path_to_read),
df)
}else{
time_df <- data.frame(Start_Time = start_time, End_Time = end_time, Time_Spent = time_spent)
}
# Save the data frame to a file
write.csv(time_df, path_to_read)
})
}
}
track_time()
track_time()
track_time <- function(category = "day") {
# Load/Create timesheet
path_to_read <- MyFunctions::my_path("D",name = "project_time_count.csv")
if(category == "day"){
start_time <- Sys.time()
message("Tracking time started.")
on.exit({
end_time <- Sys.time()
time_spent <- end_time - start_time
message("Time spent on the project:", time_spent)
# Create time data frame
df <- data.frame(Start_Time = as.character(start_time), End_Time = as.character(end_time), Time_Spent = as.numeric(time_spent))
if(file.exists(path_to_read)){
time_df <- dplyr::bind_rows(read.csv(path_to_read,row.names = F),
df)
}else{
time_df <- data.frame(Start_Time = start_time, End_Time = end_time, Time_Spent = time_spent)
}
# Save the data frame to a file
write.csv(time_df, path_to_read)
})
}
}
track_time()
track_time()
track_time <- function(category = "day") {
# Load/Create timesheet
path_to_read <- MyFunctions::my_path("D",name = "project_time_count.csv")
if(category == "day"){
start_time <- Sys.time()
message("Tracking time started.")
on.exit({
end_time <- Sys.time()
time_spent <- end_time - start_time
message("Time spent on the project:", time_spent)
# Create time data frame
df <- data.frame(Start_Time = as.character(start_time), End_Time = as.character(end_time), Time_Spent = as.numeric(time_spent))
if(file.exists(path_to_read)){
time_df <- dplyr::bind_rows(read.csv(path_to_read,row.names = F),
df)
}else{
time_df <- data.frame(Start_Time = start_time, End_Time = end_time, Time_Spent = time_spent)
}
# Save the data frame to a file
write.csv(time_df, path_to_read)
})
}
}
track_time()
track_time <- function(category = "day") {
# Load/Create timesheet
path_to_read <- MyFunctions::my_path("D",name = "project_time_count.csv")
if(category == "day"){
start_time <- Sys.time()
message("Tracking time started.")
on.exit({
end_time <- Sys.time()
time_spent <- end_time - start_time
message("Time spent on the project:", time_spent)
# Create time data frame
df <- data.frame(Start_Time = as.character(start_time), End_Time = as.character(end_time), Time_Spent = as.numeric(time_spent))
if(file.exists(path_to_read)){
time_df <- dplyr::bind_rows(read.csv(path_to_read,col.names = F),
df)
}else{
time_df <- data.frame(Start_Time = start_time, End_Time = end_time, Time_Spent = time_spent)
}
# Save the data frame to a file
write.csv(time_df, path_to_read)
})
}
}
track_time()
track_time <- function(category = "day") {
# Load/Create timesheet
path_to_read <- MyFunctions::my_path("D",name = "project_time_count.csv")
if(category == "day"){
start_time <- Sys.time()
message("Tracking time started.")
on.exit({
end_time <- Sys.time()
time_spent <- end_time - start_time
message("Time spent on the project:", time_spent)
# Create time data frame
df <- data.frame(Start_Time = as.character(start_time), End_Time = as.character(end_time), Time_Spent = as.numeric(time_spent))
if(file.exists(path_to_read)){
time_df <- dplyr::bind_rows(read.csv(path_to_read,col.names = F),
df)
}else{
time_df <- data.frame(Start_Time = start_time, End_Time = end_time, Time_Spent = time_spent)
}
# Save the data frame to a file
write.csv(time_df, path_to_read)
})
}
}
track_time()
track_time()
track_time <- function(category = "day") {
# Load/Create timesheet
path_to_read <- MyFunctions::my_path("D",name = "project_time_count.csv")
if(category == "day"){
start_time <- Sys.time()
message("Tracking time started.")
on.exit({
end_time <- Sys.time()
time_spent <- end_time - start_time
message("Time spent on the project:", time_spent)
# Create time data frame
df <- data.frame(Start_Time = as.character(start_time), End_Time = as.character(end_time), Time_Spent = as.numeric(time_spent))
if(file.exists(path_to_read)){
time_df <- dplyr::bind_rows(read.csv(path_to_read, row.names = F,col.names = F),
df)
}else{
time_df <- data.frame(Start_Time = start_time, End_Time = end_time, Time_Spent = time_spent)
}
# Save the data frame to a file
write.csv(time_df, path_to_read)
})
}
}
track_time()
track_time()
track_time()
track_time <- function(category = "day") {
# Load/Create timesheet
path_to_read <- MyFunctions::my_path("D",name = "project_time_count.csv")
if(category == "day"){
start_time <- Sys.time()
message("Tracking time started.")
on.exit({
end_time <- Sys.time()
time_spent <- end_time - start_time
message("Time spent on the project:", time_spent)
# Create time data frame
df <- data.frame(Start_Time = as.character(start_time), End_Time = as.character(end_time), Time_Spent = as.numeric(time_spent))
if(file.exists(path_to_read)){
time_df <- dplyr::bind_rows(read.csv(path_to_read),
df)
}else{
time_df <- data.frame(Start_Time = start_time, End_Time = end_time, Time_Spent = time_spent)
}
# Save the data frame to a file
write.csv(time_df, path_to_read,col.names = F)
})
}
}
track_time()
track_time <- function(category = "day") {
# Load/Create timesheet
path_to_read <- MyFunctions::my_path("D",name = "project_time_count.csv")
if(category == "day"){
start_time <- Sys.time()
message("Tracking time started.")
on.exit({
end_time <- Sys.time()
time_spent <- end_time - start_time
message("Time spent on the project:", time_spent)
# Create time data frame
df <- data.frame(Start_Time = as.character(start_time), End_Time = as.character(end_time), Time_Spent = as.numeric(time_spent))
if(file.exists(path_to_read)){
time_df <- dplyr::bind_rows(read.csv(path_to_read),
df)
}else{
time_df <- data.frame(Start_Time = start_time, End_Time = end_time, Time_Spent = time_spent)
}
# Save the data frame to a file
write.csv(time_df, path_to_read, row.names = F)
})
}
}
track_time()
track_time()
track_time()
track_time()
track_time()
track_time <- function(category = "day") {
# Load/Create timesheet
path_to_read <- MyFunctions::my_path("D",name = "project_time_count.csv")
if(category == "day"){
start_time <- Sys.time()
message("Tracking time started.")
on.exit({
end_time <- Sys.time()
time_spent <- end_time - start_time
message("Time spent on the project:", time_spent)
# Create time data frame
df <- data.frame(Start_Time = as.character(start_time), End_Time = as.character(end_time), Time_Spent = as.numeric(time_spent))
if(file.exists(path_to_read)){
time_df <- dplyr::bind_rows(read.csv(path_to_read),
df)
}else{
time_df <- data.frame(Start_Time = start_time, End_Time = end_time, Time_Spent = time_spent)
}
# Save the data frame to a file
write.csv(time_df, path_to_read, row.names = F)
})
}
}
[1] "Perch-likes" "Other fish & inverts" "Sharks & rays" "Tuna & billfishes" "Other fishes & inverts" "Salmon, smelts, etc"
devtools::document()
devtools::document()
#' Creates a project basic structure
#'
#' This function creates all the directories that I normally
#' use in all my projects. It creats them im bothe the Github
#' R project as rell as the data Repository
#'
#' @param Repo Main data repository. Currently set to HALL2000
#' @return Creates a series of directories that follow the basic structure of all my projects
#' @export
my_project <- function(Repo = "one_drive"){
# Get the projects name
Project <- basename(getwd())
# Create base directories in Github Repo
dir.create("scripts")
dir.create("results")
dir.create("results/figures")
dir.create("results/tables")
dir.create("documents")
# Create data repos in Hall2000
if(Repo == "Enterprise"){
if(dir.exists("/Volumes/Enterprise/") == TRUE){
# Main Data repo for project
dir.create(paste("/Volumes/Enterprise/Data/",Project,sep=""))
# Spatial data repo
dir.create(paste("/Volumes/Enterprise/Data/",Project,"/Spatial",sep=""))
# Species data repo
dir.create(paste("/Volumes/Enterprise/Data/",Project,"/Species",sep=""))
# Raw results
dir.create(paste("/Volumes/Enterprise/Data/",Project,"/Raw",sep=""))
}else{
print("Enterprise is not connected. Do you need to change the Repo?")
}
}
if(Repo == "one_drive"){
if(dir.exists("~/Library/CloudStorage/OneDrive-UBC/Data/") == TRUE){
# Main project repo
dir.create(paste("~/Library/CloudStorage/OneDrive-UBC/",Project,sep=""))
# Main Data repo for project
dir.create(paste("~/Library/CloudStorage/OneDrive-UBC/Data/",Project,sep=""))
# Spatial data repo
dir.create(paste("~/Library/CloudStorage/OneDrive-UBC/Data/",Project,"/data/spatial",sep=""))
# Species data repo
dir.create(paste("~/Library/CloudStorage/OneDrive-UBC/Data/",Project,"/data/species",sep=""))
# Raw results
dir.create(paste("~/Library/CloudStorage/OneDrive-UBC/Data/",Project,"/data/results/",sep=""))
# Scripts repo
dir.create(paste("~/Library/CloudStorage/OneDrive-UBC/Data/",Project,"/scripts/",sep=""))
# Specify the file name and location
file_path <- paste0("~/Library/CloudStorage/OneDrive-UBC/Data/",Project,"/scripts/initial_analysis.Rmd",sep="")
}else{
print("No connection with One Drive UBC Data. Do you need to change the Repo? Try Enterprise")
}
}
}
per_cng_fx <- function(v1,v2,limit=NA){
if(limit == NA){
x = ((v2-v1)/abs(v1))*100
}else{
x <- sign(limit) * 100 # Keep the sign of x and cap it at ±100
}
}
per_cng_fx <- function(v1,v2,limit=NA){
{
{
{
{
{
{
{
#' Estimates percentage change
#'
#' This function loads all necessary packages. If the
#' package is not installed, it will install it first
#' and the it will load the recently installed package
#'
#' @param v1 Type of path needed "D" for Data, "R", for Result and "G" for generic. You need those paths to be load in the global environment
#' @param v2 I normally call my data folder the same as the project so it automatically sets to the project name. Alternatives available.
#' @return A numeric value in percentage change from v2 relative to v1
#' @export
per_cng_fx <- function(v1, v2, limit = NA){
if(is.na(limit){
per_cng_fx <- function(v1, v2, limit = NA){
if(is.na(limit)){
x = ((v2-v1)/abs(v1))*100
}else{
x <- sign(limit) * 100 # Keep the sign of x and cap it at ±100
}
return(x)
}
devtools::document()
devtools::document()
devtools::document()
#' Creates a project basic structure
#'
#' This function creates all the directories that I normally
#' use in all my projects. It creats them im bothe the Github
#' R project as rell as the data Repository
#'
#' @param Repo Main data repository. Currently set to HALL2000
#' @return Creates a series of directories that follow the basic structure of all my projects
#' @export
my_project <- function(Repo = "one_drive"){
# Get the projects name
Project <- basename(getwd())
# Create base directories in Github Repo
dir.create("scripts")
dir.create("results")
dir.create("results/figures")
dir.create("results/tables")
dir.create("documents")
# Create data repos in Hall2000
if(Repo == "Enterprise"){
if(dir.exists("/Volumes/Enterprise/") == TRUE){
# Main Data repo for project
dir.create(paste("/Volumes/Enterprise/Data/",Project,sep=""))
# Spatial data repo
dir.create(paste("/Volumes/Enterprise/Data/",Project,"/Spatial",sep=""))
# Species data repo
dir.create(paste("/Volumes/Enterprise/Data/",Project,"/Species",sep=""))
# Raw results
dir.create(paste("/Volumes/Enterprise/Data/",Project,"/Raw",sep=""))
}else{
print("Enterprise is not connected. Do you need to change the Repo?")
}
}
if(Repo == "one_drive"){
if(dir.exists("~/Library/CloudStorage/OneDrive-UBC/Data/") == TRUE){
# Main project repo
dir.create(paste("~/Library/CloudStorage/OneDrive-UBC/",Project,sep=""))
}
# Main Data repo for project
dir.create(paste("~/Library/CloudStorage/OneDrive-UBC/data/",Project,"/data",sep=""))
# Spatial data repo
dir.create(paste("~/Library/CloudStorage/OneDrive-UBC/data/",Project,"/data/spatial",sep=""))
# Species data repo
dir.create(paste("~/Library/CloudStorage/OneDrive-UBC/data/",Project,"/data/species",sep=""))
# Raw results
dir.create(paste("~/Library/CloudStorage/OneDrive-UBC/data/",Project,"/results/",sep=""))
# Specify the file name and location
file_path <- paste0("~/Library/CloudStorage/OneDrive-UBC/data/",Project,"/scripts/initial_analysis.Rmd",sep="")
}else{
print("No connection with One Drive UBC Data. Do you need to change the Repo? Try Enterprise")
}
}
#'
#' @param path_type Type of path needed "D" for Data, "R", for Result and "G" for generic. You need those paths to be load in the global environment
#' @param project I normally call my data folder the same as the project so it automatically sets to the project name. Alternatives available.
#' @param extra_path Any sub-path to add WITHIN the folders. Note, if it is before, include it on the "Folder" parameter -e.g. project/SubFolder -
#' @param name Dataset name in case you want the option to load data
#' @param repo Normally set to jepa88 but if connection to Drobo is available use repo = "FALSE"
#' @param read Default read = FALSE. read = FALSE will only return a path wile read = TURE will read a dataset
#' @param system If you want to override the automatic system identification
#' @return Paths to save and load data within a project
#' @export
my_path <- function(path_type, extra_path= "", name = "", list_files ="NA", repo = TRUE, read=FALSE, header=TRUE, project = TRUE, system = NA){
# Overrride automati path
if(is.na(system)){
user <- Sys.info()[7]
}else{
user <- system
}
# Main path where I store my data
if(user== "juliano"){
Main_Path <- "/Volumes/Enterprise/Data" # jepa88 (pre-selected)
}
if(user == "jepa88"){
Main_Path <- "~/Library/CloudStorage/OneDrive-UBC/Data/"
}
if(user == "drobo"){
Main_Path <- "/Volumes/DATA/JULIANO_NEYMAR" #If Drobo is needed
}
# Automatically sets the project name
if(project == TRUE){
# Get projecet name (only working for carmelia right now)
Project_root <- rprojroot::find_rstudio_root_file()
Project <- sub("^.*([^/]*/[^/]*)", "\\1", Project_root)
}
# Set project Data, Generic Data, Results and Figures paths
if(path_type %in% c("R","r","Result","result")){
Path <- paste(Main_Path,Project,"/Results",sep="")
}
if(path_type %in% c("D","Data","d","data")){
Path <- paste(Main_Path,Project,"/Data",sep="")
}
if(path_type%in% c("G","Generic","g","generic")){
Path <- Main_Path
}
if(path_type%in% c("Spa","Spatial","spa","spatial")){
Path <- paste(Main_Path,"/Spatial",sep="")
}
if(path_type%in% c("Spp","Species","spp","species")){
Path <- paste(Main_Path,"/Species",sep="")
}
if(path_type%in% c("F","f","Figures","Figure","figure","figures")){
Path <- "./Figures"
}
# Error messages for misspelled variables
Options = c("R","Results","D","Data","G","Generic","F","Figures","Spa","Spatial","Spp","Species")
if(!path_type %in% Options){
print("Data type value not accepted. Current acceptable options:")
print(Options)
stop()
}
# If the reading option is TRUE
if(read == TRUE){
My_Path <- paste(Path,extra_path,name,sep="/")
# Fix any double // in the path
My_Path <- gsub("//","/",My_Path)
if(stringr::str_detect(name,".xlsx") == TRUE){
My_Path <- readxl::read_excel(My_Path) %>% janitor::clean_names()
}
if(stringr::str_detect(name,".csv") == TRUE){
My_Path <- data.table::fread(My_Path, header=header) %>% janitor::clean_names()
}
if(stringr::str_detect(name,"txt") == TRUE){
My_Path <- data.table::fread(My_Path, header=header) %>% janitor::clean_names()
}
if(stringr::str_detect(name,".RData") == TRUE){
load(My_Path)
}
# Already load the coords with names
if(name == "DBEM_coords"){
name == "Lon_Lat_DBEM.txt"
colnames(My_Path) <- c("index","lon","lat")
}
}else{
if(list_files == "names"){
My_Path <- paste(Path,extra_path,name,sep="/")
# Fix any double // in the path
My_Path <- gsub("//","/",My_Path)
My_Path <- list.files(My_Path,full.names = F)
}else{
My_Path <- paste(Path,extra_path,name,sep="/")
# Fix any double // in the path
My_Path<- gsub("//","/",My_Path)
}
if(list_files == "paths"){
My_Path <- paste(Path,extra_path,name,sep="/")
# Fix any double // in the path
My_Path <- gsub("//","/",My_Path)
My_Path <- list.files(My_Path,full.names = T)
}else{
My_Path <- paste(Path,extra_path,name,sep="/")
# Fix any double // in the path
My_Path<- gsub("//","/",My_Path)
}
}
# Function returns the selected path
return(My_Path)
}
devtools::document()
devtools::document()
devtools::install_github("jepa/MyFunctions", force = T)