dcomtois / summarytools

R Package to Quickly and Neatly Summarize Data
522 stars 78 forks source link

Escape Characters Causing Ugly Display in Jupyter #39

Closed breckuh closed 6 years ago

breckuh commented 6 years ago

I love the summaries this tool generates in RStudio. Thanks!

My problem is that using this with Jupyter doesn't seem to work. Reproduction below:

jupyter

Inspecting the data frame:

ddd = summarytools::dfSummary(mtcars)
ddd$Variable

Produces this:

[1] "mpg\\\n[numeric]"  "cyl\\\n[numeric]"  "disp\\\n[numeric]" "hp\\\n[numeric]"   "drat\\\n[numeric]"
 [6] "wt\\\n[numeric]"   "qsec\\\n[numeric]" "vs\\\n[numeric]"   "am\\\n[numeric]"   "gear\\\n[numeric]"
[11] "carb\\\n[numeric]"

Which works great in RStudio or the command line, poorly in Jupyter.

I am struggling to figure out if there is simple a parameter I am missing? Or maybe there is a method I can pipe this output through to unescape those characters?

If I figure it out I'll post a solution.

dcomtois commented 6 years ago

I've never been able to set up Junyper Notebook unfortunately... Are you on Windows?

breckuh commented 6 years ago

I am on Ubuntu and Fedora.

I put a test in a Kaggle notebook which you can see/fork here:

https://www.kaggle.com/breckuh/summarytools-issue-39/

dcomtois commented 6 years ago

Thanks for this. Backslashes apart, it seems that one of the issues is that the print,summarytools method is not called while in that environment. That is the reason we see both the img tags for html graphs and the ascii graphs. This makes the table extremely wide and causes the cells with spaces to shrink in width.

I'd suggest trying to explicitly call the print function, first the generic one: print(mySummary)

If not successful, you can try: summarytools:::print.summarytools(mySummary)

Another thing to try out would be to generate a dfSummary with graph.col = FALSE and see if the layout is decent. We'll still have the backslashes issue though. I'll try to set up a Jupyter notebook on my Linux machine to experiment this with.

dcomtois commented 6 years ago

I've managed to get JN up and running. I might be wrong, but my impression is that it just doesn't support line breaks in tables... Tried to gather info on the Web about it but information is scarce. At this point I think it's worth taking a look at the desktop version of Jupyter, which allows editing markdown directly.

dcomtois commented 6 years ago

I confirmed with further testing in Jupyter Lab; multiline cells are not supported in Jupyter, hopefully this will change in the future.

Closing until further notice... In the meantime, the best workaround is to use the plain ascii version that shows up when using print(dfSummary(df)).

breckuh commented 6 years ago
summarytools:::print.summarytools(mySummary)

Is a great workaround. Thanks!!

dcomtois commented 6 years ago

Btw you can do just print(...) no real need for the full sumarytools:::

I've experimented a little bit more with Jupyter, and the best I could achieve is an accurate html sdSummary, but it's displayed in a huge frame, no idea why!

library(IRdisplay)
library(summarytools)
st_options('bootstrap.css', FALSE)
st_options('omit.headings', TRUE)
st_options('footnote', NA)

link <- "https://bit.ly/2Dz1Cx9" # summarytools.css on github
stcss <- paste(readLines(url(link)), collapse = " ")
stcss <- paste0('<style>', stcss, '</style>')
display_html(stcss)

dfs <- paste(capture.output(print(dfSummary(iris, graph.magnif = 0.7), method = 'render')), collapse = "<br>")
display_html(dfs)
demonoff commented 3 years ago

just an update for anybody looking for this functionality, it seems to be working now, anyhow, it works for me on a local jupyter-notebook with R kernel when using the following code:

library(IRdisplay)
library(summarytools)
st_options('headings', TRUE)
style = "multiline"
style = "grid"

display_markdown(
    paste(
        capture.output(
            print(
                dfSummary(
                    iris, 
                    graph.magnif = 0.8,
                    style        = style,
                    plain.ascii  = FALSE,
                    valid.col    = FALSE
                ), 
                method = 'render')), 
        collapse = ""))

ss0

dcomtois commented 3 years ago

@demonoff That's good news, thanks for sharing! It seems not all line breaks are honoured, but it is a big step forward.