Skip to content

Commit d9d692a

Browse files
committed
Merge branch 'release/1.22.0'
2 parents a004e70 + a126476 commit d9d692a

7 files changed

+96
-13
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: redcapcustodian
22
Type: Package
33
Title: Data automation for R-centric workflows with a nod towards REDCap
4-
Version: 1.21.0
4+
Version: 1.22.0
55
Authors@R: c(
66
person("Philip", "Chase",
77
email = "pbc@ufl.edu",
@@ -70,6 +70,6 @@ Suggests:
7070
tidyverse
7171
VignetteBuilder: knitr
7272
Config/testthat/edition: 3
73-
RoxygenNote: 7.2.3
73+
RoxygenNote: 7.3.1
7474
Depends:
7575
R (>= 3.5.0)

Dockerfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
FROM --platform=linux/amd64 rocker/verse:4.3.2
1+
FROM --platform=linux/amd64 rocker/verse:4.3.3
22

33
WORKDIR /home/rocker
44

55
RUN apt update -y && apt install -y libmariadb-dev libmariadb-dev-compat
66
RUN apt install -y --no-install-recommends libxt6
77

8+
# returns an error but tlmgr is updated to 2024 regardless
9+
RUN wget ${CTAN_REPO}/update-tlmgr-latest.sh && bash update-tlmgr-latest.sh; exit 0
10+
811
# install necessary libraries
912
RUN R -e "install.packages(c( \
1013
'DBI', \

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# redcapcustodian 1.22.0 (released 2024-03-26)
2+
- Implement hacky fix for tlmgr 2023 being unable to install packages (@ChemiKyle, #156)
3+
- Fix bug that prevented email_body from being included in email (@ljwoodley, @ChemiKyle, #155)
4+
- Add 'Scraping one user's API tokens' section to vignettes/credential-scraping.Rmd (@pbchase, @ChemiKyle, #154)
5+
- Update scrape_user_api_tokens() to tidyselect 1.2 standards (@pbchase, #154)
6+
17
# redcapcustodian 1.21.0 (released 2024-03-15)
28
- Add attachment management to send_email() allowing lists of files or dataframes to be attached to an email (@ljwoodley, #152, #153)
39

R/credential_management.R

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ scrape_user_api_tokens <- function(conn, username_to_scrape = Sys.info()[["user"
2222
# collect super API token if one exists
2323
super_credentials <- dplyr::tbl(conn, "redcap_user_information") %>%
2424
dplyr::filter(.data$username == username_to_scrape) %>%
25-
dplyr::select(.data$username, .data$api_token) %>%
25+
dplyr::select("username", "api_token") %>%
2626
dplyr::collect() %>%
2727
dplyr::mutate(project_id = 0) %>%
2828
dplyr::filter(!is.na(.data$api_token)) %>%
@@ -32,25 +32,25 @@ scrape_user_api_tokens <- function(conn, username_to_scrape = Sys.info()[["user"
3232
dplyr::filter(.data$username == username_to_scrape) %>%
3333
dplyr::filter(!is.na(.data$api_token)) %>%
3434
dplyr::select(
35-
.data$project_id,
36-
.data$username,
37-
.data$api_token
35+
"project_id",
36+
"username",
37+
"api_token"
3838
) %>%
3939
# add project information
4040
dplyr::left_join(
4141
dplyr::tbl(conn, "redcap_projects") %>%
4242
dplyr::select(
43-
.data$project_id,
44-
.data$app_title
43+
"project_id",
44+
"app_title"
4545
),
4646
by = "project_id"
4747
) %>%
4848
dplyr::collect() %>%
4949
# bind_rows used over rbind to avoid need to align column order
5050
dplyr::bind_rows(super_credentials) %>%
5151
dplyr::rename(
52-
project_display_name = .data$app_title,
53-
token = .data$api_token # rename for compatibility with REDCapR credential objects
52+
project_display_name = "app_title",
53+
token = "api_token" # rename for compatibility with REDCapR credential objects
5454
)
5555

5656
return(credentials)

R/logging.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,10 @@ send_email <-
689689
email_to <- unlist(strsplit(email_to, " "))
690690
}
691691

692+
email_content <- email_body
693+
692694
if (!is.null(file_name)) {
693695
output_dir <- tempdir()
694-
email_content <- list()
695696

696697
if (!is.null(df_to_email) && is.data.frame(df_to_email)) {
697698
df_to_email <- list(df_to_email)

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.21.0
1+
1.22.0

vignettes/credential-scraping.Rmd

+73
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,79 @@ dbDisconnect(file_conn)
115115
dbDisconnect(source_conn)
116116
```
117117

118+
### Scraping one user's API tokens
119+
120+
Scraping one user's API tokens and putting them in a new, local sqlite DB for use.
121+
122+
You must provide the username in the API_USER environment variable or on the command line. To specify it on the command line via `Rscript` and specify a REDCap username. e.g.
123+
124+
```sh
125+
Rscript scrape_one_user.R jane_doe
126+
```
127+
128+
```{r, eval = FALSE}
129+
library(redcapcustodian)
130+
library(DBI)
131+
library(tidyverse)
132+
library(dotenv)
133+
134+
# Get the username provided on the command line if one is provided.
135+
# Otherwise, get the username form the environment.
136+
# Otherwise, exit.
137+
args <- commandArgs(trailingOnly = TRUE)
138+
if (length(args) > 0) {
139+
# Use the command line argument
140+
username <- args
141+
} else if (Sys.getenv("API_USER") != "") {
142+
# Use the environment variable
143+
username <- Sys.getenv("API_USER")
144+
} else {
145+
# Exit
146+
warning("Please provide a username that whose API tokens you want to read either on the command line or via the API_USER environment variable. No action taken.")
147+
quit()
148+
}
149+
150+
# Creates the credentials file if one does not exists.
151+
# Otherwise it deletes the credentials file
152+
credentials_db_path <- here::here(paste0("credentials-", username, ".db"))
153+
if (fs::file_exists(credentials_db_path)) fs::file_delete(credentials_db_path)
154+
file_conn <- DBI::dbConnect(RSQLite::SQLite(), credentials_db_path)
155+
156+
# SQLite friendly schema
157+
credentials_sql <- "CREATE TABLE IF NOT EXISTS `credentials` (
158+
`redcap_uri` TEXT NOT NULL,
159+
`server_short_name` varchar(128) NOT NULL,
160+
`username` varchar(191) NOT NULL,
161+
`project_id` int(10) NOT NULL,
162+
`project_display_name` TEXT NOT NULL,
163+
`project_short_name` varchar(128) DEFAULT NULL,
164+
`token` varchar(64) NOT NULL,
165+
`comment` varchar(256) DEFAULT NULL
166+
);
167+
"
168+
169+
dbExecute(file_conn, credentials_sql)
170+
171+
# Connect to the REDCap database specified in the environment
172+
# loaded by the dotenv library
173+
source_conn <- connect_to_redcap_db()
174+
source_credentials <- scrape_user_api_tokens(
175+
conn = source_conn,
176+
username_to_scrape = username
177+
)
178+
179+
# alter credentials to match local schema
180+
source_credentials_upload <- source_credentials %>%
181+
mutate(
182+
redcap_uri = Sys.getenv("URI"),
183+
server_short_name = tolower(Sys.getenv("INSTANCE"))
184+
)
185+
186+
dbAppendTable(file_conn, "credentials", source_credentials_upload)
187+
188+
dbDisconnect(file_conn)
189+
dbDisconnect(source_conn)
190+
```
118191
### Creating API tokens for all local projects
119192

120193
```{r, eval = FALSE}

0 commit comments

Comments
 (0)