From eb9c70312bf38801a5a3e7a75c8038fd72a0f4d1 Mon Sep 17 00:00:00 2001 From: Randy Lai Date: Sun, 11 Sep 2016 00:26:54 -0400 Subject: [PATCH] allow .Last.value for non-visible commands --- R/execution.r | 50 ++++++++++++++++++++++++++------------------------ R/utils.r | 2 +- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/R/execution.r b/R/execution.r index 42421612..87f7a23d 100644 --- a/R/execution.r +++ b/R/execution.r @@ -9,10 +9,10 @@ plot_builds_upon <- function(prev, current) { if (is.null(prev)) { return(TRUE) } - + lprev <- length(prev[[1]]) lcurrent <- length(current[[1]]) - + lcurrent >= lprev && identical(current[[1]][1:lprev], prev[[1]][1:lprev]) } @@ -30,29 +30,29 @@ ask <- function(prompt = '') { format_stack <- function(calls) { line_refs <- rep('', length(calls)) - + tb <- lapply(seq_along(calls), function(cl) { call <- calls[[cl]] - + # first_line, first_byte, last_line, last_byte, first_column, last_column, first_parsed, last_parsed ref <- attr(call, 'srcref') - + filename <- attr(ref, 'srcfile')$filename - + if (!is.null(ref)) { f <- ref[[1]] l <- ref[[3]] lines <- if (f == l) f else paste0(f, '-', l) line_refs[[cl]] <<- paste0(' # at line ', lines, ' of file ', filename) } - + white <- paste(rep(' ', nchar(format(cl))), collapse = '') - + f.call <- format(call) line.prefix <- c(cl, rep(white, length(f.call) - 1)) paste(paste0(line.prefix, '. ', f.call), collapse = '\n') }) - + paste0(tb, line_refs) } @@ -91,7 +91,7 @@ display_data = function(data, metadata = NULL) { source = 'R display func', data = data, metadata = metadata)) - + invisible(TRUE) }, @@ -118,9 +118,9 @@ quit = function(save = 'default', status = 0, runLast = TRUE) { handle_error = function(e) tryCatch({ log_debug('Error output: %s', toString(e)) calls <- head(sys.calls()[-seq_len(nframe + 1L)], -3) - + calls <- skip_repeated(calls) - + msg <- paste0(toString(e), 'Traceback:\n') stack_info <- format_stack(calls) @@ -160,16 +160,18 @@ handle_display_error = function(e) { send_error_msg(msg) }, -handle_value = function(obj) { +handle_value = function(obj, visible) { log_debug('Value output...') mimebundle <- prepare_mimebundle_kernel(obj, .self$handle_display_error) if (length(intersect(class(obj), getOption('jupyter.pager_classes'))) > 0) { log_debug('Showing pager: %s', paste(capture.output(str(mimebundle$data)), collapse = '\n')) page(mimebundle) } else { - log_debug('Sending display_data: %s', paste(capture.output(str(mimebundle$data)), collapse = '\n')) set_last_value(obj) - send_response('display_data', current_request, 'iopub', mimebundle) + if (visible) { + log_debug('Sending display_data: %s', paste(capture.output(str(mimebundle$data)), collapse = '\n')) + send_response('display_data', current_request, 'iopub', mimebundle) + } } }, @@ -209,13 +211,13 @@ execute = function(request) { send_response('execute_input', request, 'iopub', list( code = request$content$code, execution_count = execution_count)) - + # Make the current request available to other functions current_request <<- request # reset ... payload <<- list() err <<- list() - + # shade base::quit add_to_user_searchpath('quit', .self$quit) add_to_user_searchpath('q', .self$quit) @@ -226,7 +228,7 @@ execute = function(request) { 'stop()', stop_on_error = 1L, output_handler = new_output_handler(error = function(e) nframe <<- sys.nframe()))) - + oh <- if (is_silent()) { new_output_handler( text = identity, @@ -244,13 +246,13 @@ execute = function(request) { error = .self$handle_error, value = .self$handle_value) } - + interrupted <<- FALSE last_recorded_plot <<- NULL log_debug('Executing code: %s', request$content$code) - + warn_unicode_on_windows(request$content$code, .self$send_error_msg) - + tryCatch( evaluate( request$content$code, @@ -259,11 +261,11 @@ execute = function(request) { stop_on_error = 1L), interrupt = function(cond) interrupted <<- TRUE, error = .self$handle_error) # evaluate does not catch errors in parsing - + if (!is_silent() && !is.null(last_recorded_plot)) { send_plot(last_recorded_plot) } - + if (interrupted) { reply_content <- list( status = 'abort') @@ -278,7 +280,7 @@ execute = function(request) { payload = payload, user_expressions = namedlist()) } - + send_response('execute_reply', request, 'shell', reply_content) if (interrupted || !is.null(err$ename)) { diff --git a/R/utils.r b/R/utils.r index 305f7398..1bbd548c 100644 --- a/R/utils.r +++ b/R/utils.r @@ -4,7 +4,7 @@ ellip_h <- repr:::.char_fallback('\u22EF', '...') skip_repeated <- function(vec) { if (length(vec) == 0L) return(vec) - + if (is.language(vec[[1]])) { # rle does not work on language items ctb <- as.character(vec) enc <- rle(ctb)