Skip to content

Lots of object do not display anything: in this case "formattable" #16

Open

Description

@jankatins
#devtools::install_github("renkun-ken/formattable")
library(formattable)

df <- data.frame(
  id = 1:10,
  name = c("Bob", "Ashley", "James", "David", "Jenny", 
    "Hans", "Leo", "John", "Emily", "Lee"), 
  age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
  grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
  test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
  test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
  final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
  registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
  stringsAsFactors = FALSE)

f <- formattable(df, list(
  age = color_tile("white", "orange"),
  grade = formatter("span",
    style = x ~ ifelse(x == "A", style(color = "green", font.weight = "bold"), NA)),
  test1_score = normalize_bar("pink", 0.2),
  test2_score = normalize_bar("pink", 0.2),
  final_score = formatter("span",
    style = x ~ style(color = ifelse(rank(-x) <= 3, "green", "gray")),
    x ~ sprintf("%.2f (rank: %02d)", x, rank(-x))),
  registered = formatter("span", 
    style = x ~ style(color = ifelse(x, "green", "red")),
    x ~ icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No")))
))

f # does not display anything
class(f) # displays "formattable" "data.frame"
print(f) # nothing

I think this happens when print(f) does not print anything and we don't have anything to make a rich display...

In rstudio, the f already triggers something, so we really should implement the same way rstudio handles this...

x = print(f)

this also triggers a display in rstudio and sets x:

> x = print(f)
> class(x)
[1] "formattable_widget" "htmlwidget" 

So it seems we should implement a repr_html.htmlwidget (and maybe repr_html.formattable? I haven't fully understand the knit_print.*() hierarchy/call chain in formattable yet) and also check that we have the same display mechanism as rstudio (do they have a way to deliver plots to the plot area?).

Regarding things we have no rich printing for: I've no clue how to reliably tell if we miss a repr_xxx.class_type (and tell the user that they should bug us or the package owner for such a method) or that the package author didn't want a visible output. If someone has a clue...

CC: @renkun-ken (owner of the formattable package)

Activity

  1. renkun-ken commented on Mar 17, 2016

    @renkun-ken

    Thanks, @JanSchulz! I notice this too using formattable in the notebook and tries ggvis and it opens a new webpage to show the interactive plot.

  2. jankatins commented on Mar 23, 2016

    @jankatins
    ContributorAuthor

    This is the knit_print method, which is

    knit_print.htmlwidget <- function(x, ..., options = NULL) {
      knitr::knit_print(toHTML(x, standalone = FALSE, knitrOptions = options), options = options,  ...)
    }

    So if we want to set width/height we would have to construct a knitrOption structure so that https://github.com/ramnathv/htmlwidgets/blob/3a2e3120ef59260afe9f6e5be08d1cd7612ba903/R/sizing.R#L207-L216 does the right thing...

  3. kcy1019 commented on Sep 27, 2016

    @kcy1019

    Thanks @JanSchulz !
    In IRdisplay_0.4.9000

    x = formattable(...)
    y = print(x)
    y

    worked perfectly :)
    (Howerver, as you mentioned, just calling print(x) directly w/o variable y did not show anything)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions