Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printing in Jupyter notebook #8

Open
egnha opened this issue Jun 14, 2018 · 4 comments
Open

Printing in Jupyter notebook #8

egnha opened this issue Jun 14, 2018 · 4 comments

Comments

@egnha
Copy link
Owner

egnha commented Jun 14, 2018

Executing a cell like

abs %>>>% log

ignores the class and prints

structure(function (x) 
`__2__`(`__1__`(x)), class = c("CompositeFunction", "function"
))

whereas

print(abs %>>>% log)

applies the correct print method

<Function Composition>
In order of application:

[[1]]
  function (x)  .Primitive("abs")

[[2]]
  function (x, base = exp(1))  .Primitive("log")

Recover the list of functions with 'as.list()'.

Similar problem occurs for other custom gestalt classes ("PartialFunction", etc.).

Is this an issue with Jupyter and/or gestalt?

@egnha
Copy link
Owner Author

egnha commented Jun 16, 2018

This is an issue in the repr package, which circumvents dispatching print to the proper method when the object class is "function". As a workaround, one can explicitly invoke print().

@egnha egnha closed this as completed Jun 16, 2018
@egnha
Copy link
Owner Author

egnha commented Aug 6, 2018

Implement and export repr::repr_html methods for Gestalt's function classes.

@egnha egnha reopened this Aug 6, 2018
@egnha
Copy link
Owner Author

egnha commented Aug 6, 2018

Initial implementation:

# repr/R/utils.r:
html_escape <- local({
  html_specials <- c(
    `&` = "&amp;",
    `<` = "&lt;",
    `>` = "&gt;"
  )
  pre_wrap <- "<span style=white-space:pre-wrap>%s</span>"

  function(text) {
    for (chr in names(html_specials))
      text <- gsub(chr, html_specials[[chr]], text, fixed = TRUE)
    consec_spaces <- grepl("\ \ ", text)
    text[consec_spaces] <- sprintf(pre_wrap, text[consec_spaces])
    text
  }
})

#' @importFrom utils capture.output
as_repr_html <- function(class) {
  printer <- get(paste0("print.", class))
  wrap <- "<pre class=language-r><code>%s</code></pre>"

  function(obj, ...) {
    code <- capture.output(printer(obj))
    sprintf(wrap, paste(html_escape(code), collapse = "\n"))
  }
}

#' @importFrom repr repr_html
NULL

#' @export
repr_html.CompositeFunction <- as_repr_html("CompositeFunction")

#' @export
repr_html.PartialFunction <- as_repr_html("PartialFunction")

@egnha
Copy link
Owner Author

egnha commented Aug 6, 2018

Enable option to syntax-highlight.

@egnha egnha added this to the 0.1.5 milestone Oct 15, 2018
@egnha egnha removed this from the 0.1.5 milestone Feb 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant