Skip to content

Commit

Permalink
allow .Last.value for non-visible commands
Browse files Browse the repository at this point in the history
  • Loading branch information
randy3k committed Sep 11, 2016
1 parent 69fef4e commit eb9c703
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
50 changes: 26 additions & 24 deletions R/execution.r
Expand Up @@ -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])
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -91,7 +91,7 @@ display_data = function(data, metadata = NULL) {
source = 'R display func',
data = data,
metadata = metadata))

invisible(TRUE)
},

Expand All @@ -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)

Expand Down Expand Up @@ -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)
}
}
},

Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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')
Expand All @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion R/utils.r
Expand Up @@ -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)
Expand Down

0 comments on commit eb9c703

Please sign in to comment.