diff --git a/DESCRIPTION b/DESCRIPTION index da2fd6a9..daad80b2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,7 @@ Imports: httr, jsonlite, purrr, + R.utils, R6, rlang, tibble diff --git a/R/Instance.R b/R/Instance.R index 2444258d..346cf521 100644 --- a/R/Instance.R +++ b/R/Instance.R @@ -225,7 +225,12 @@ Instance <- R6::R6Class( # nolint object_name_linter py_lamin <- self$get_py_lamin(check = TRUE, what = "Tracking") if (is.null(path)) { - cli::cli_abort("The {.arg path} argument must be provided") + path <- detect_path() + if (is.null(path)) { + cli::cli_abort( + "Failed to detect the path to track. Please set the {.arg path} argument." + ) + } } if (is.null(transform)) { @@ -233,6 +238,7 @@ Instance <- R6::R6Class( # nolint object_name_linter py_lamin$track(path = path), error = function(err) { py_err <- reticulate::py_last_error() + # please don't change the below without changing it in lamindb if (py_err$type != "MissingContextUID") { cli::cli_abort(c( "Python {py_err$message}", @@ -242,8 +248,7 @@ Instance <- R6::R6Class( # nolint object_name_linter uid <- gsub(".*\\(\"(.*?)\"\\).*", "\\1", py_err$value) cli::cli_inform(paste( - "Got UID {.val {uid}} for path {.file {path}}.", - "Run this function with {.code transform = \"{uid}\"} to track this path." + "To track this notebook, run: db$track(\"{uid}\")" )) } ) @@ -260,7 +265,21 @@ Instance <- R6::R6Class( # nolint object_name_linter #' @description Finish a tracked run finish = function() { py_lamin <- self$get_py_lamin(check = TRUE, what = "Tracking") - py_lamin$finish() + tryCatch( + py_lamin$finish(), + error = function(err) { + py_err <- reticulate::py_last_error() + if (py_err$type != "NotebookNotSaved") { + cli::cli_abort(c( + "Python {py_err$message}", + "i" = "Run {.run reticulate::py_last_error()} for details" + )) + } + # please don't change the below without changing it in lamindb + message <- gsub(".*NotebookNotSaved: (.*)$", "\\1", py_err$value) + cli::cli_inform(paste("NotebookNotSaved: {message}")) + } + ) }, #' @description #' Print an `Instance` diff --git a/R/InstanceAPI.R b/R/InstanceAPI.R index 6c621433..d503ee12 100644 --- a/R/InstanceAPI.R +++ b/R/InstanceAPI.R @@ -199,7 +199,6 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter registry_name, id_or_uid, verbose = FALSE) { - url <- paste0( private$.instance_settings$api_url, "/instances/", diff --git a/R/utils.R b/R/utils.R index f6de6691..57f2eaa5 100644 --- a/R/utils.R +++ b/R/utils.R @@ -53,3 +53,52 @@ is_knitr_notebook <- function() { # check if we are in a notebook !is.null(knitr::opts_knit$get("out.format")) } + +#' Detect path +#' +#' Find the path of the file where code is currently been run +#' +#' @return If found, path to the file relative to the working directory, +#' otherwise `NULL` +#' @noRd +detect_path <- function() { + # Based on various responses from https://stackoverflow.com/questions/47044068/get-the-path-of-current-script + + current_path <- NULL + + # Get path if in a running RMarkdown notebook + if (is_knitr_notebook()) { + current_path <- knitr::current_input() + } + + # Get path if in a script run by `source("script.R")` + source_trace <- R.utils::findSourceTraceback() + if (is.null(current_path) && length(source_trace) > 0) { + current_path <- names(source_trace)[1] + } + + # Get path if in a script run by `Rscript script.R` + if (is.null(current_path)) { + cmd_args <- R.utils::commandArgs(asValues = TRUE) + current_path <- cmd_args[["file"]] + } + + # Get path if in a document in RStudio + if ( + is.null(current_path) && + requireNamespace("rstudioapi", quietly = TRUE) && + rstudioapi::isAvailable() + ) { + doc_context <- rstudioapi::getActiveDocumentContext() + if (doc_context$id != "#console") { + current_path <- doc_context$path + } + } + + # Normalise the path relative to the working directory + if (!is.null(current_path)) { + current_path <- R.utils::getRelativePath(current_path) + } + + return(current_path) +} diff --git a/vignettes/laminr.Rmd b/vignettes/laminr.Rmd index 151ae0df..6d437522 100644 --- a/vignettes/laminr.Rmd +++ b/vignettes/laminr.Rmd @@ -64,13 +64,13 @@ LaminDB tracks which code is used to create data. To track the current source code, run: ```{r track, eval = submit_eval} -db$track("I8BlHXFXqZOG0000", path = "laminr.Rmd") +db$track("I8BlHXFXqZOG0000") ``` # Connect to other instances