Skip to content

Commit

Permalink
Record help addin use
Browse files Browse the repository at this point in the history
  • Loading branch information
phgrosjean committed Sep 15, 2024
1 parent 511961e commit cfecee3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Authors@R: c(
email = "guyliann.engels@umons.ac.be"))
Maintainer: Philippe Grosjean <phgrosjean@sciviews.org>
Depends: R (>= 4.2.0)
Imports: learnitdown, gert, rstudioapi, shiny, miniUI, shinyAce, methods, utils, svMisc
Imports: learnitdown, gert, rstudioapi, shiny, miniUI, shinyAce, methods, utils, svMisc, httr2
Suggests:
covr,
knitr,
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export(sign_out)
export(switch_repo)
importFrom(gert,git_remote_info)
importFrom(gert,git_remote_set_url)
importFrom(httr2,req_body_json)
importFrom(httr2,req_headers)
importFrom(httr2,req_perform)
importFrom(httr2,request)
importFrom(methods,findFunction)
importFrom(miniUI,gadgetTitleBar)
importFrom(miniUI,miniButtonBlock)
Expand Down
38 changes: 32 additions & 6 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ switch_repo_addin <- function()
#' @importFrom svMisc assign_temp get_temp
#' @importFrom rstudioapi sendToConsole
#' @importFrom utils ? methods
#' @importFrom httr2 request req_body_json req_headers req_perform
.sdd_help_addin <- function() {

get_help <- function(message) {
Expand Down Expand Up @@ -108,12 +109,17 @@ switch_repo_addin <- function()
l <- length(fun_env_names)
if (!l) {# Function not found
# Ask help system directly
cmd <- parse(text = paste0("?", cur_fun))
if (length(eval(cmd))) {# Help page found
fun <- cur_fun
ns <- "" # Unknown
alt_fun_msg <- ""
} else {# Not found
if (cur_fun != "") {
cmd <- parse(text = paste0("?", cur_fun))
if (length(eval(cmd))) {# Help page found
fun <- cur_fun
ns <- "" # Unknown
alt_fun_msg <- ""
} else {# Not found
fun <- ""
ns <- ""
}
} else {# No function?
fun <- ""
ns <- ""
}
Expand Down Expand Up @@ -396,6 +402,26 @@ switch_repo_addin <- function()
#message(help_code)
rstudioapi::sendToConsole(help_code, execute = TRUE, echo = TRUE,
focus = FALSE)

# If help page (not chatbot), record the data in the database
if (context$engine == "SciViews Chatbot")
return(invisible(help_code))

chat_url <- getOption("SciViews.chatbot.url",
Sys.getenv("SCIVIEWS_CHATBOT_URL", ""))
api_key <- Sys.getenv("CONNECT_API_KEY", "")
if (chat_url != "" && api_key != "") {
help_url <- paste0(dirname(chat_url), "/help")
request <- httr2::request(help_url) |>
httr2::req_headers("Accept" = "application/json",
"Authorization" = paste("Key", api_key))
context$code <- help_code
request <- request |>
httr2::req_body_json(context)
res <- try(httr2::req_perform(request), silent = TRUE)
svMisc::assign_temp("sdd_help_result", res, replace.existing = TRUE)
}

invisible(help_code)
}

Expand Down

0 comments on commit cfecee3

Please sign in to comment.