Skip to content

Commit

Permalink
Fix displaying
Browse files Browse the repository at this point in the history
  • Loading branch information
jankatins committed Mar 29, 2016
1 parent d3c3407 commit a0d8a85
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions R/execution.r
Original file line number Diff line number Diff line change
Expand Up @@ -203,41 +203,48 @@ execute = function(request) {
# to convert itself to something else (and don't output anything to the console) and the
# returned obj is then printed again.
print_return <<- list(visible=TRUE, value=obj)
send_even_if_no_print <- FALSE
while(print_return$visible) {
data[['text/plain']] <- paste(capture.output(print_return <<- withVisible(print(print_return$value))), collapse = '\n')
if (print_return$visible) repr_even_if_no_print <- TRUE
if (print_return$visible){
log_debug("Found visible return from print (cls: %s)", paste(class(obj), collapse=", "))
obj <- print_return$value
}
}

# Sometimes, print(obj) does not print anything and we try to mimic that by not
# displaying anything in that case, even if our own repr_xxx functions would show
# something for the object.
if (nchar(data[['text/plain']]) > 0){
log_debug("Nothing printed for value")
return()
}
obj <- print_return$value

if (send_even_if_no_print || nchar(data[['text/plain']]) > 0) {
if (getOption('jupyter.rich_display')) {
for (mime in getOption('jupyter.display_mimetypes')) {
# Use withCallingHandlers as that shows the inner stacktrace:
# https://stackoverflow.com/questions/15282471/get-stack-trace-on-trycatched-error-in-r
# the tryCatch is still needed to prevent the error from showing
# up outside further up the stack :-/
tryCatch(withCallingHandlers({
r <- mime2repr[[mime]](obj)
if (!is.null(r)) {
data[[mime]] <- r
# Isolating full html pages (putting them in an iframe)
if (identical(mime, 'text/html')) {
if (grepl("<html.*>", r, ignore.case = TRUE)){
log_debug("Found full html page: %s", strtrim(r, 100))
metadata[[mime]] <- list(isolated = TRUE)
}
if (getOption('jupyter.rich_display')) {
for (mime in getOption('jupyter.display_mimetypes')) {
# Use withCallingHandlers as that shows the inner stacktrace:
# https://stackoverflow.com/questions/15282471/get-stack-trace-on-trycatched-error-in-r
# the tryCatch is still needed to prevent the error from showing
# up outside further up the stack :-/
tryCatch(withCallingHandlers({
r <- mime2repr[[mime]](obj)
if (!is.null(r)) {
data[[mime]] <- r
# Isolating full html pages (putting them in an iframe)
if (identical(mime, 'text/html')) {
if (grepl("<html.*>", r, ignore.case = TRUE)){
log_debug("Found full html page: %s", strtrim(r, 100))
metadata[[mime]] <- list(isolated = TRUE)
}
}
}, error = handle_display_error),
error=function(x){})
}
}
}, error = handle_display_error),
error=function(x){})
}
log_debug("Sending display_data: %s", paste(capture.output(str(data)), collapse = "\n"))
send_response('display_data', request, 'iopub', list(
data = data,
metadata = metadata))
}
log_debug("Sending display_data: %s", paste(capture.output(str(data)), collapse = "\n"))
send_response('display_data', request, 'iopub', list(
data = data,
metadata = metadata))
}

stream <- function(output, streamname) {
Expand Down

0 comments on commit a0d8a85

Please sign in to comment.