Skip to content

Commit a6deba0

Browse files
authored
Merge pull request #96 from diyabc/refactor_app
Application refactoring to improve performance, robustness and stability (training set simulation and random forest analysis only)
2 parents 11222f0 + 7f4b9a9 commit a6deba0

54 files changed

Lines changed: 14628 additions & 7352 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

R-pkg/DESCRIPTION

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: diyabcGUI
22
Type: Package
33
Title: Graphical User Interface for DYIABC-RF software
4-
Version: 1.0.14
5-
Date: 2021-03-12
4+
Version: 1.1.0
5+
Date: 2021-08-24
66
Authors@R: c(
77
person(
88
"Ghislain", "Durif", comment="diyabcGUI main developer",
@@ -59,11 +59,14 @@ Imports:
5959
lubridate,
6060
jsonlite,
6161
magrittr,
62+
markdown,
63+
mime,
6264
parallel, pbapply, processx,
65+
readr,
6366
rlang,
6467
shiny,
6568
shinybusy (>= 0.2.2),
66-
shinydashboard, shinyFiles,
69+
shinydashboard, shinyFeedback, shinyFiles,
6770
shinyhelper, shinyjs, shinyWidgets,
6871
stringr,
6972
tibble,

R-pkg/NAMESPACE

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export(get_option)
1313
export(redirect_output)
1414
export(set_diyabcGUI_options)
1515
export(standalone_run_app)
16+
import(markdown)
1617
import(shiny)
1718
importFrom(dplyr,arrange)
1819
importFrom(dplyr,bind_rows)
@@ -53,16 +54,21 @@ importFrom(ggplot2,ylim)
5354
importFrom(jsonlite,fromJSON)
5455
importFrom(lubridate,now)
5556
importFrom(magrittr,"%>%")
57+
importFrom(mime,guess_type)
5658
importFrom(parallel,detectCores)
5759
importFrom(parallel,makeCluster)
5860
importFrom(parallel,stopCluster)
5961
importFrom(pbapply,pblapply)
6062
importFrom(processx,process)
63+
importFrom(readr,read_file)
6164
importFrom(rlang,duplicate)
65+
importFrom(shinyFeedback,feedbackWarning)
66+
importFrom(shinyFeedback,useShinyFeedback)
6267
importFrom(shinyWidgets,actionBttn)
6368
importFrom(shinyWidgets,actionGroupButtons)
6469
importFrom(shinyWidgets,ask_confirmation)
6570
importFrom(shinyWidgets,downloadBttn)
71+
importFrom(shinyWidgets,execute_safely)
6672
importFrom(shinyWidgets,progressBar)
6773
importFrom(shinyWidgets,radioGroupButtons)
6874
importFrom(shinyWidgets,show_alert)
@@ -74,7 +80,9 @@ importFrom(shinydashboard,dashboardHeader)
7480
importFrom(shinydashboard,dashboardPage)
7581
importFrom(shinydashboard,dashboardSidebar)
7682
importFrom(shinydashboard,menuItem)
83+
importFrom(shinydashboard,renderMenu)
7784
importFrom(shinydashboard,sidebarMenu)
85+
importFrom(shinydashboard,sidebarMenuOutput)
7886
importFrom(shinydashboard,tabItem)
7987
importFrom(shinydashboard,tabItems)
8088
importFrom(shinydashboard,updateTabItems)
@@ -85,7 +93,6 @@ importFrom(shinyjs,disabled)
8593
importFrom(shinyjs,enable)
8694
importFrom(shinyjs,hidden)
8795
importFrom(shinyjs,hide)
88-
importFrom(shinyjs,onclick)
8996
importFrom(shinyjs,reset)
9097
importFrom(shinyjs,show)
9198
importFrom(shinyjs,useShinyjs)
@@ -100,6 +107,7 @@ importFrom(stringr,str_pad)
100107
importFrom(stringr,str_replace)
101108
importFrom(stringr,str_replace_all)
102109
importFrom(stringr,str_split)
110+
importFrom(stringr,str_to_lower)
103111
importFrom(stringr,str_to_upper)
104112
importFrom(stringr,str_trim)
105113
importFrom(tibble,lst)

R-pkg/R/01_directory.R

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,44 @@ mk_proj_dir <- function(tag = "diyabc") {
7575
# output
7676
return(tmp_dir)
7777
}
78+
79+
80+
#' Clean project directory
81+
#' @keywords internal
82+
#' @description Remove all files and sub-folders in a given project directory.
83+
#' @author Ghislain Durif
84+
#' @param proj_dir character string, path to project directory.
85+
clean_proj_dir <- function(proj_dir) {
86+
# check if project directory exists
87+
if(length(proj_dir) != 1 && !dir.exists(proj_dir)) {
88+
stop("Input argument should be a valid path to a project directory")
89+
}
90+
# sub-folders
91+
subdir_list <- list.dirs(proj_dir, full.names = FALSE, recursive = FALSE)
92+
subdir_list <- subdir_list[subdir_list != ""]
93+
if(length(subdir_list) > 0) {
94+
fs::dir_delete(file.path(proj_dir, subdir_list))
95+
}
96+
# files
97+
file_list <- list.files(proj_dir)
98+
if(length(file_list) > 0) {
99+
fs::file_delete(file.path(proj_dir, file_list))
100+
}
101+
}
102+
103+
104+
#' Clean binary directory
105+
#' @keywords internal
106+
#' @author Ghislain Durif
107+
clean_bin_dir <- function() {
108+
# bin directory
109+
path <- bin_dir()
110+
# existing binary file
111+
existing_bin_files <- list.files(path)
112+
existing_bin_files <- existing_bin_files[str_detect(existing_bin_files,
113+
"diyabc|abcranger|dll")]
114+
# delete diyabc/abcranger files
115+
if(length(existing_bin_files) > 0) {
116+
fs::file_delete(file.path(path, existing_bin_files))
117+
}
118+
}

R-pkg/R/02_regex.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ num_regex <- function() {
1212
return("[0-9]+\\.?[0-9]*")
1313
}
1414

15+
#' return numerical (xxEyy notation) regex
16+
#' @keywords internal
17+
#' @author Ghislain Durif
18+
numexp_regex <- function() {
19+
return("[0-9]+\\.?[0-9]*((E|e)\\-?[0-9]+)?")
20+
}
21+
1522
#' return event numerical rate regex
1623
#' @keywords internal
1724
#' @author Ghislain Durif

R-pkg/R/03_utils.R

Lines changed: 55 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,6 @@ find_bin <- function(prog = "diyabc") {
4242
return(bin_file)
4343
}
4444

45-
#' Clean binary directory
46-
#' @keywords internal
47-
#' @author Ghislain Durif
48-
clean_bin_dir <- function() {
49-
# bin directory
50-
path <- bin_dir()
51-
# existing binary file
52-
existing_bin_files <- list.files(path)
53-
existing_bin_files <- existing_bin_files[str_detect(existing_bin_files,
54-
"diyabc|abcranger|dll")]
55-
# delete diyabc/abcranger files
56-
if(length(existing_bin_files) > 0) {
57-
fs::file_delete(file.path(path, existing_bin_files))
58-
}
59-
}
6045

6146
#' Find which OS is running
6247
#' @keywords internal
@@ -85,6 +70,8 @@ get_os <- function() {
8570
#' @author Ghislain Durif
8671
#' @param prog string, name of the program to download, eligible name are
8772
#' `"diyabc-RF"` and `"abcranger"`.
73+
#' @return integer value, `0` if download succeeded, `1` if download failed,
74+
#' `-1` if latest version is already here.
8875
#' @importFrom fs file_chmod
8976
#' @importFrom jsonlite fromJSON
9077
#' @export
@@ -107,6 +94,7 @@ dl_latest_bin <- function(prog = "diyabc") {
10794
"releases/latest",
10895
sep = "/"
10996
)
97+
11098
# check latest release info
11199
release_info <- fromJSON(release_url)
112100

@@ -115,94 +103,91 @@ dl_latest_bin <- function(prog = "diyabc") {
115103

116104
# check if file in release
117105
if(nrow(release) < 1) {
118-
stop(str_c("Issue with available files at ", release_url, ". ",
119-
"Contact DIYABC-RF support.",
120-
sep = ""))
106+
stop(str_c(
107+
"Issue with files available at", release_url, ".",
108+
"Please contact DIYABC-RF support.",
109+
sep = " "
110+
))
111+
}
112+
113+
# select release for current OS
114+
release <- subset(release, str_detect(release$name, os_id))
115+
116+
# check if release available for current OS
117+
if(nrow(release) != 1) {
118+
stop(str_c(
119+
prog, "binary file is not available for", os_id, "OS at",
120+
release_url, ".",
121+
"Please contact DIYABC-RF support.",
122+
sep = ""
123+
))
121124
}
122125

123126
# already existing binary file
124127
existing_bin_files <- list.files(path)
125128

126129
# download release
127-
out <- lapply(
128-
split(release, seq(nrow(release))),
129-
function(single_file) {
130-
# output
131-
# check: 0 if dl is ok or latest version already here,
132-
# 1 if dl failed
133-
# -1 if no binary files was available
134-
check <- 1
135-
bin_name <- single_file$name
136-
bin_url <- single_file$browser_download_url
137-
138-
# abcranger/diyabc binary files
139-
if(str_detect(bin_name, str_c(prog_name(prog), "-",
140-
os_id, sep = ""))) {
141-
if(!bin_name %in% existing_bin_files) {
142-
# avoid blacklisting
143-
Sys.sleep(2)
144-
# dl
145-
check <- download.file(
146-
bin_url,
147-
destfile = file.path(path, bin_name),
148-
mode = "wb"
149-
)
150-
} else {
151-
check <- 0
152-
warning(str_c(
153-
"The latest release", bin_name,
154-
"was already downloaded.", sep = " "
155-
))
156-
}
157-
} else {
158-
check <- -1
159-
}
160-
return(check)
161-
}
162-
)
130+
check <- 1
131+
bin_name <- release$name
132+
bin_url <- release$browser_download_url
163133

164-
# binary files not available at all among files in latest release
165-
if(all(out == -1)) {
166-
warning(str_c("No binary file available for ", prog, " on ",
167-
os_id, ". ",
168-
"Contact DIYABC-RF support.",
169-
sep = ""))
170-
}
171-
172-
# no download success among files in latest release
173-
if(!any(out == 0)) {
174-
stop("Issue with download")
134+
# check if bin file already available locally
135+
if(!bin_name %in% existing_bin_files) {
136+
# avoid blacklisting
137+
Sys.sleep(2)
138+
# dl
139+
check <- download.file(
140+
bin_url,
141+
destfile = file.path(path, bin_name),
142+
mode = "wb"
143+
)
144+
} else {
145+
check <- -1
146+
warning(str_c(
147+
"The latest release", bin_name,
148+
"was already downloaded.", sep = " "
149+
))
175150
}
176151

177152
# zip extraction for diyabc on Windows
178153
zip_files <- list.files(path, pattern = "\\.zip$")
179154
if(length(zip_files) > 0) {
180155
latest_zip <- which.max(file.info(file.path(path, zip_files))$mtime)
181-
tmp <- utils::unzip(file.path(path, zip_files[latest_zip]), exdir = path)
156+
tmp <- utils::unzip(
157+
file.path(path, zip_files[latest_zip]),
158+
exdir = path
159+
)
182160
if(length(tmp) == 0) {
183-
stop(str_c("Issue when unzipping ", zip_files[latest_zip]))
161+
stop(str_c(
162+
"Issue when unzipping", zip_files[latest_zip], sep = " "
163+
))
184164
}
185165
fs::file_delete(file.path(path, zip_files))
186166
}
187167

188168
# set up rights
189169
bin_files <- list.files(path, pattern = prog)
190170
fs::file_chmod(file.path(path, bin_files), "a+rx")
171+
172+
# output
173+
return(check)
191174
}
192175

193176
#' Download all latest diyabcGUI related binary files if missing
194177
#' @keywords internal
195178
#' @author Ghislain Durif
196179
#' @export
197180
dl_all_latest_bin <- function() {
198-
dl_latest_bin("diyabc")
199-
dl_latest_bin("abcranger")
181+
check_diyabc <- dl_latest_bin("diyabc")
182+
check_abcranger <- dl_latest_bin("abcranger")
183+
return(lst(check_diyabc, check_abcranger))
200184
}
201185

202186
#' Custom print
203187
#' @keywords internal
204188
#' @author Ghislain Durif
205189
pprint <- function(...) {
190+
print(str_c("--- content of ",deparse(substitute(...))))
206191
# message(as.character(...))
207192
print(...)
208193
}
@@ -222,7 +207,7 @@ reset_sink <- function() {
222207
#' @author Ghislain Durif
223208
logging <- function(...) {
224209
if(getOption("diyabcGUI")$verbose)
225-
pprint(str_c(..., sep = " ", collapse = " "))
210+
print(str_c(..., sep = " ", collapse = " "))
226211
}
227212

228213
#' Enable logging verbosity

0 commit comments

Comments
 (0)