Skip to content

Commit b0deb78

Browse files
committed
Change names for portability. Reworking of 02a_base, and addition of recording/checking functions
1 parent a04c064 commit b0deb78

Some content is hidden

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

53 files changed

+633
-324
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: BioDataScience
22
Type: Package
3-
Version: 0.1.2
3+
Version: 0.2.0
44
Title: A Series of Learnr Documents for Biological Data Science
55
Description: Interactive documents using learnr for studying biological data science.
66
Authors@R: c(
@@ -10,7 +10,7 @@ Authors@R: c(
1010
email = "guyliann.engels@umons.ac.be"))
1111
Maintainer: Philippe Grosjean <phgrosjean@sciviews.org>
1212
Depends: R (>= 3.3.0)
13-
Imports: learnr, httr, jsonlite, remotes
13+
Imports: learnr, httr, jsonlite, remotes, svMisc, mongolite
1414
Suggests: SciViews, ggplot2, ggpubr, data.io, flow, chart, covr, knitr, testthat
1515
License: CC BY-SA 4.0
1616
Encoding: UTF-8

NAMESPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(checker_sdd)
4+
export(record_sdd)
35
export(run)
6+
export(send_mail_sdd)
47
importFrom(httr,GET)
58
importFrom(httr,authenticate)
69
importFrom(httr,content)
710
importFrom(httr,parse_url)
811
importFrom(httr,status_code)
12+
importFrom(jsonlite,base64_dec)
13+
importFrom(jsonlite,base64_enc)
914
importFrom(jsonlite,fromJSON)
1015
importFrom(learnr,run_tutorial)
16+
importFrom(mongolite,mongo)
1117
importFrom(remotes,install_github)
18+
importFrom(svMisc,list_to_json)
1219
importFrom(utils,compareVersion)
20+
importFrom(utils,create.post)
1321
importFrom(utils,packageVersion)
1422
importFrom(utils,select.list)

R/BioDataScience-package.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
#'
1212
#' @importFrom learnr run_tutorial
1313
#' @importFrom httr content status_code parse_url authenticate GET
14-
#' @importFrom jsonlite fromJSON
15-
#' @importFrom utils compareVersion packageVersion select.list
14+
#' @importFrom jsonlite fromJSON base64_dec base64_enc
15+
#' @importFrom utils compareVersion packageVersion select.list create.post
1616
#' @importFrom remotes install_github
17+
#' @importFrom svMisc list_to_json
18+
#' @importFrom mongolite mongo
1719
NULL

R/checker_sdd.R

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#' A default checker that just aknowledge submission
2+
#'
3+
#' @param label The label for the learnr exercice.
4+
#' @param user_code The code submitted by the user.
5+
#' @param check_code The code to check against.
6+
#' @param envir_result The environment for results.
7+
#' @param evaluate_result Result from evaluation of the code.
8+
#' @param ... Additional parameters.
9+
#'
10+
#' @description Check code submitted during an exercice. This version just
11+
#' acknowledges reception of the submission. This function is used internally
12+
#' by the tutorials and is not intended for the end-user.
13+
#'
14+
#' @return A list with components `message`, `correct` and `location`.
15+
#' @export
16+
#' @seealso [run()]
17+
#' @keywords utilities
18+
#' @concept record events from the BioDataScience package
19+
checker_sdd <- function(label, user_code, check_code, envir_result,
20+
evaluate_result, ...) {
21+
list(message = "Your answer is recorded!", correct = TRUE, location = "append")
22+
}
23+
#Use: tutorial_options(exercise.checker = checker_sdd)
24+
#
25+
# Later, we would do something like this:
26+
#library(checkr)
27+
#tutorial_options(exercise.checker = checkr::check_for_learnr)
28+
#check_two_plus_two <- function(USER_CODE) {
29+
# code <- for_checkr(USER_CODE)
30+
# # The messages
31+
# m1 <- "Correct!"
32+
# m2 <- "You should use the '+' operator."
33+
# m3 <- "Another error."
34+
# m4 <- "Again another error message."
35+
#
36+
# result <- line_where(code,
37+
# passif(Z == "+"),
38+
# failif(Z == "", m3),
39+
# failif(TRUE, m4))
40+
# result
41+
#}

R/record_sdd.R

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#' Record results on a centralized database
2+
#'
3+
#' @param tutorial_id The identifier of the tutorial.
4+
#' @param tutorial_version The version of the tutorial.
5+
#' @param user_id The user identifier for this learnr process.
6+
#' @param event The event that triggers the record, like `exercise_submission`
7+
#' or `question_submission`
8+
#' @param data A JSON field with event-dependent data content.
9+
#'
10+
#' @description Record tutorial submissions in a centralized database. The
11+
#' function is used by learnr tutorials and is not for end-users.
12+
#'
13+
#' @return Nothing. The function is used for its side-effects.
14+
#' @export
15+
#' @seealso [run()]
16+
#' @keywords utilities
17+
#' @concept record events from the BioDataScience package
18+
record_sdd <- function(tutorial_id, tutorial_version, user_id, event, data) {
19+
bds_file <- "~/.local/share/R/learnr/biodatascience"
20+
add_file_base64 <- function(entry, file) {
21+
str <- gsub("\n", "", base64_enc(serialize(entry, NULL)))
22+
dir.create(dirname(file), showWarnings = FALSE, recursive = TRUE)
23+
cat(str, "\n", file = file, append = TRUE)
24+
}
25+
user_name <- suppressWarnings(system("git config user.name",
26+
intern = TRUE, ignore.stderr = TRUE))
27+
user_email <- suppressWarnings(system("git config user.email",
28+
intern = TRUE, ignore.stderr = TRUE))
29+
#user_full_id <- paste(user_id, user_name, user_email, sep = "/")
30+
date <- Sys.time()
31+
label <- data$label
32+
if (is.null(label)) label <- ""
33+
data$label <- NULL
34+
correct <- data$correct
35+
if (is.null(correct)) {
36+
correct <- data$feedback$correct
37+
if (is.null(correct))
38+
correct <- ""
39+
}
40+
data$correct <- NULL
41+
entry <- data.frame(date = date, tutorial = tutorial_id,
42+
version = tutorial_version, user = user_id, user_name = user_name,
43+
user_email = user_email, label = label, correct = correct, event = event,
44+
data = list_to_json(data))
45+
if (correct == "") {
46+
add_file_base64(entry, file = bds_file)
47+
return()
48+
}
49+
# Once http request with stitch will be available, we could do something like
50+
#https://stitch.mongodb.com/api/client/v2.0/app/sdd-relay-aizkd/service/sdd-http/incoming_webhook/webhook0
51+
m <- try(mongo("ex01",
52+
#url = "mongodb://sdd:sdd@ds125388.mlab.com:25388/sdd-test")$insert(entry)
53+
url = "mongodb://sdd:sdd@sdd-umons-shard-00-00-umnnw.mongodb.net:27017,sdd-umons-shard-00-01-umnnw.mongodb.net:27017,sdd-umons-shard-00-02-umnnw.mongodb.net:27017/test?ssl=true&replicaSet=sdd-umons-shard-0&authSource=admin"),
54+
silent = TRUE)
55+
if (!inherits(m, "try-error") &&
56+
m$run(command = "{\"ping\": 1}", simplify = TRUE)$ok == 1) {
57+
m$insert(entry)
58+
# If there is something in the biodatascience file, inject it also now
59+
if (file.exists(bds_file)) {
60+
dat <- readLines(bds_file)
61+
unlink(bds_file)
62+
if (length(dat))
63+
for (i in 1:length(dat))
64+
m$insert(unserialize(base64_dec(dat[i])))
65+
}
66+
m$disconnect()
67+
} else {# MongoDB database not available... save locally
68+
add_file_base64(entry, file = bds_file)
69+
}
70+
}
71+
# Use: options(tutorial.event_recorder = record_sdd)

R/run.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
#' @description Start the learnr R engine in the current R session with the
1111
#' selected tutorial.
1212
#'
13-
#' @return If `tutorial` is not provided, a list of available tutorials.
13+
#' @return If `tutorial` is not provided, in interactive mode with `ask = TRUE`,
14+
#' you have to select one in a list, and in non interactive mode, or
15+
#' `ask = FALSE`, it returns the list of all available tutorials.
1416
#' @export
1517
#' @seealso [run_tutorial()]
1618
#' @keywords utilities
1719
#' @concept run interactive learnr documents from the BioDataScience package
1820
#' @examples
19-
#' # To list the available tutorials:
21+
#' # To start from a list of available tutorials:
2022
#' run()
2123
#' \dontrun{
22-
#' run("module02a_nuage_de_points")
24+
#' run("02b_nuage_points")
2325
#' }
2426
run <- function(tutorial, ..., update = ask, ask = interactive()) {
2527
# devtools:::github_GET() and dependencies are not exported.

R/send_mail_sdd.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#' Send your BioDataScience submissions by email
2+
#'
3+
#' @param file The file that contains your submissions information.
4+
#'
5+
#' @description Your submissions are send to a central database. However, in
6+
#' case that database is not accessible, the data is stored locally. This
7+
#' function uses your plain email to send your records. Note that, once the
8+
#' email is created, the local version of your records is reset. So, if you
9+
#' finally decide to NOT send the eamil, these records are lost (in this case,
10+
#' call your teachers to recover them, if you have to.)
11+
#'
12+
#' @return The data are returned invisibly.
13+
#' @export
14+
#' @seealso [run()]
15+
#' @keywords utilities
16+
#' @concept run interactive learnr documents from the BioDataScience package
17+
#' @examples
18+
#' # To start from a list of available tutorials:
19+
#' run()
20+
#' \dontrun{
21+
#' run("02b_nuage_points")
22+
#' }
23+
send_mail_sdd <- function(file = "~/.local/share/R/learnr/biodatascience") {
24+
if (file.exists(file)) {
25+
data <- readLines(file)
26+
file.rename(file, paste0(file, ".bak")) # One backup, just in case!
27+
create.post("Dear BioDataScience user,
28+
29+
Send this email without changing recipient, title and bottom of the message
30+
to record your tutorial activities.
31+
32+
Thanks,
33+
34+
The BioDataScience Team
35+
",
36+
description = "post", info = data,
37+
subject = "sdd-umons", address = "sdd@sciviews.org")
38+
invisible(data)
39+
} else {
40+
message("Nothing to send to the BioDataScience database")
41+
invisible(character(0))
42+
}
43+
}

0 commit comments

Comments
 (0)