Skip to content

Commit 061d5e6

Browse files
committed
Corrected most check warnings and notes
1 parent 2860112 commit 061d5e6

31 files changed

+667
-1
lines changed

.Rbuildignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$
3+
^dev$
4+
^LICENSE\.md$
5+
^data-raw$

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata

DESCRIPTION

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Imports:
2020
rlang,
2121
stringr,
2222
tibble,
23-
tidyr
23+
tidyr,
24+
tidyselect
2425
RoxygenNote: 7.2.3
2526
Depends:
2627
R (>= 2.10)

LICENSE

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
YEAR: 2023
2+
COPYRIGHT HOLDER: importr2 authors

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
Copyright (c) 2023 importr2 authors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

NAMESPACE

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
S3method(wrangle_results,kr_results)
4+
S3method(wrangle_results,l500_results)
5+
S3method(wrangle_results,sk_results)
6+
export("%>%")
7+
export(connect_to_l500_dbi)
8+
export(h_clean_methods)
9+
export(h_l500_format_curve)
10+
export(h_rm_trailing_numbers)
11+
export(imp_l500_results)
12+
export(l500_con_strings)
13+
export(wrangle_results)
14+
importFrom(magrittr,"%>%")
15+
importFrom(rlang,.data)
16+
importFrom(rlang,.env)

R/databases.R

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#' Build connection string for Lida 500 database
2+
#'
3+
#' @param db_file_path path to database file (.mdb)
4+
#'
5+
#' @return A connection string for Lida 500 database.
6+
#' @export
7+
l500_con_strings <- function(db_file_path){
8+
9+
keys <- keyring::key_list()
10+
11+
if(!("L500" %in% keys$service)){
12+
keyring::key_set("L500", prompt = "Lida 500 database password:")
13+
}
14+
15+
dbq_string <- paste0("DBQ=", db_file_path)
16+
driver_string <- "Driver={Microsoft Access Driver (*.mdb, *.accdb)};"
17+
cred_string <- paste0("Pwd=", keyring::key_get("L500"), ";")
18+
paste0(driver_string, cred_string, dbq_string)
19+
}
20+
21+
22+
#' Connect to an instrument database
23+
#'
24+
#' @param db_file_path path to database file (.mdb)
25+
#'
26+
#' @return An S4 object that inherits from DBI::DBIConnection-class.
27+
#' @export
28+
connect_to_l500_dbi <- function(db_file_path = "D:/Analyzer.mdb") {
29+
# make sure that the file exists before attempting to connect
30+
if (!file.exists(db_file_path)) {
31+
stop("DB file does not exist at ", db_file_path)
32+
}
33+
34+
myconn <- DBI::dbConnect(
35+
odbc::odbc(),
36+
.connection_string = l500_con_strings(db_file_path),
37+
timeout = 10
38+
)
39+
40+
return(myconn)
41+
}
42+
43+
#' @param user username (for now, "fguerrero")
44+
#' @rdname connect_to_l500_dbi
45+
connect_to_sk_dbi <- function( user = "fguerrero" ){
46+
47+
keys <- keyring::key_list()
48+
49+
if(!("sekisui" %in% keys$service)){
50+
keyring::key_set("sekisui", prompt = "Sekisui database password:")
51+
}
52+
53+
DBI::dbConnect(odbc::odbc(),
54+
Driver = "SQL Server",
55+
Server = "DESKTOP-NVDH6AU\\SQLEXPRESS,1433",
56+
Database = "48i",
57+
UID = user,
58+
PWD = keyring::key_get("sekisui"))
59+
}
60+
61+
#' @rdname connect_to_l500_dbi
62+
connect_to_kr_dbi <- function(db_file_path = "D:/OppLocal.mdb") {
63+
# make sure that the file exists before attempting to connect
64+
if (!file.exists(db_file_path)) {
65+
stop("DB file does not exist at ", db_file_path)
66+
}
67+
68+
dbq_string <- paste0("DBQ=", db_file_path)
69+
driver_string <- "Driver={Microsoft Access Driver (*.mdb, *.accdb)};"
70+
db_connect_string <- paste0(driver_string, dbq_string)
71+
72+
myconn <- DBI::dbConnect(
73+
odbc::odbc(),
74+
.connection_string = db_connect_string,
75+
timeout = 10
76+
)
77+
78+
return(myconn)
79+
}

R/importr2-package.R

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#' @keywords internal
2+
"_PACKAGE"
3+
4+
## usethis namespace: start
5+
#' @importFrom rlang .data
6+
#' @importFrom rlang .env
7+
## usethis namespace: end
8+
NULL

R/kroma.R

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#' Import results from Kroma or Kroma Plus database.
2+
#'
3+
#' @param conn a connection object like the one returned from connect_to_kr_dbi
4+
#'
5+
#' @return a tibble
6+
imp_kr_results <- function(conn = connect_to_kr_dbi()){
7+
8+
results <- dplyr::collect(dplyr::tbl(conn, "AnalisysArchive"))
9+
10+
class(results) <- c("kr_results", class(results))
11+
12+
results
13+
}

R/lida_500.R

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#' Import results from Lida 500 database.
2+
#'
3+
#' @param conn a connection object like the one returned from connect_to_l500_dbi
4+
#'
5+
#' @return imp_l500_worklisttest imports results currently stored in WorkListTest table.
6+
#' imp_l500_archive imports archived results.
7+
#' imp_l500_results imports results from both tables.
8+
#' @export
9+
imp_l500_results <- function(conn = connect_to_l500_dbi()){
10+
11+
data_wl <- imp_l500_worklisttest(conn)
12+
data_archive <- imp_l500_archive(conn)
13+
14+
common_cols <- c("SampleID", "Method", "Stat", "Result", "Result_Retest", "OD",
15+
"Unit", "ABS_Prim", "ABS_Sec", "Blank")
16+
17+
wl_extra_cols <- c("End_Time", "Repeat_Index")
18+
arch_extra_cols <- c("TestDate", "ResultIndex")
19+
20+
selected_wl <- data_wl %>%
21+
dplyr::select(dplyr::all_of(c(common_cols, wl_extra_cols))) %>%
22+
dplyr::mutate(
23+
TestDate = lubridate::as_date(.data$End_Time)
24+
) %>%
25+
dplyr::select(-.data$End_Time)
26+
27+
selected_archive <- data_archive %>%
28+
dplyr::select(dplyr::all_of(c(common_cols, arch_extra_cols))) %>%
29+
dplyr::rename(Repeat_Index = "ResultIndex") %>%
30+
dplyr::mutate(
31+
TestDate = lubridate::as_date(.data$TestDate)
32+
)
33+
34+
results <- dplyr::bind_rows(
35+
selected_wl, selected_archive
36+
) %>%
37+
dplyr::filter(!is.na(.data$OD))
38+
39+
class(results) <- c("l500_results", class(results))
40+
41+
results
42+
}
43+
44+
#' @rdname imp_l500_results
45+
imp_l500_worklisttest <- function(conn = connect_to_l500_dbi()){
46+
47+
tables <- tibble::tibble(
48+
table = DBI::dbListTables(conn)
49+
) %>%
50+
dplyr::filter(stringr::str_starts(table, "WorkListTest"))
51+
52+
tables %>%
53+
dplyr::mutate(
54+
data = purrr::map(
55+
.x = .data$table,
56+
.f = \(x) dplyr::collect(dplyr::tbl(conn, x))
57+
)
58+
) %>%
59+
tidyr::unnest("data") %>%
60+
dplyr::select(-table) %>%
61+
dplyr::filter(!is.na(.data$OD)) %>%
62+
unique()
63+
}
64+
65+
#' @rdname imp_l500_results
66+
imp_l500_archive <- function(conn = connect_to_l500_dbi()){
67+
68+
tables <- tibble::tibble(
69+
table = DBI::dbListTables(conn)
70+
) %>%
71+
dplyr::filter(stringr::str_starts(table, "Archive"))
72+
73+
tables %>%
74+
dplyr::mutate(
75+
data = purrr::map(
76+
.x = .data$table,
77+
.f = \(x) dplyr::collect(dplyr::tbl(conn, x))
78+
)
79+
) %>%
80+
tidyr::unnest("data") %>%
81+
dplyr::select(-table) %>%
82+
unique() %>%
83+
dplyr::mutate(
84+
Result = as.double(.data$Result),
85+
Result_Retest = as.double(.data$Result_Retest)
86+
)
87+
}
88+
89+
#' Format Lida 500 reaction curves
90+
#'
91+
#' @param ABS_Prim vector of primary wavelength absorbance
92+
#' @param ABS_Sec vector of secondary wavelength absorbance
93+
#'
94+
#' @return A tibble with columns "cycle", "abs1", "abs2" and "abs_dif"
95+
#' @export
96+
h_l500_format_curve <- function(ABS_Prim, ABS_Sec){
97+
tibble::tibble(
98+
abs1 = as.double(stringr::str_split_1(ABS_Prim, ",")),
99+
abs2 = as.double(stringr::str_split_1(ABS_Sec, ","))
100+
) %>%
101+
dplyr::mutate(
102+
abs2 = tidyr::replace_na(.data$abs2, 0),
103+
cycle = 1:dplyr::n(),
104+
abs_dif = .data$abs1 - .data$abs2
105+
)
106+
}
107+

R/sekisui.R

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#' Import results from Sekisui database.
2+
#'
3+
#' @param conn a connection object like the one returned from connect_to_sk_dbi
4+
#'
5+
#' @return a tibble
6+
imp_sk_results <- function(conn = connect_to_sk_dbi()){
7+
8+
results <- dplyr::collect(dplyr::tbl(conn, "ResultLog"))
9+
10+
class(results) <- c("sk_results", class(results))
11+
12+
results
13+
}
14+

R/sysdata.rda

1.38 KB
Binary file not shown.

R/utils-pipe.R

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#' Pipe operator
2+
#'
3+
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
4+
#'
5+
#' @name %>%
6+
#' @rdname pipe
7+
#' @keywords internal
8+
#' @export
9+
#' @importFrom magrittr %>%
10+
#' @usage lhs \%>\% rhs
11+
#' @param lhs A value or the magrittr placeholder.
12+
#' @param rhs A function call using the magrittr semantics.
13+
#' @return The result of calling `rhs(lhs)`.
14+
NULL

R/utils.R

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
h_match_methods <- function(string, method){
2+
stringr::str_detect(string, paste0(method,"\\s?[:digit:]*(?!.)"))
3+
}
4+
5+
#' Remove trailing numbers from a character vector
6+
#'
7+
#' @param x a character vector
8+
#'
9+
#' @return A character vector without trailing numbers
10+
#' @export
11+
h_rm_trailing_numbers <- function(x){
12+
x %>%
13+
stringr::str_remove("\\s?[:digit:]*(?!.)") %>%
14+
stringr::str_remove("\\s*(?!.)")
15+
}
16+
17+
#' Standardize method names from multiple instruments
18+
#'
19+
#' @param x WIP return value from wrangle_results()?
20+
#'
21+
#' @return A character vector without trailing numbers
22+
#' @export
23+
h_clean_methods <- function(x){
24+
x %>%
25+
dplyr::mutate(raw = h_rm_trailing_numbers(.data$method)) %>%
26+
dplyr::left_join(
27+
unique(dplyr::select(methods_dictionary, -"instrument")), # He puesto comillas, a ver si funciona
28+
by = dplyr::join_by(raw)
29+
) %>%
30+
dplyr::mutate(method = .data$clean) %>%
31+
dplyr::select(!tidyselect::all_of(c("clean", "raw"))) # He puesto tidyselect, a ver si funciona
32+
33+
}

0 commit comments

Comments
 (0)