benjaminrich / table1

79 stars 26 forks source link

An error occurs when using table1 in jupyter notebook with R core #26

Closed HuoJnx closed 3 years ago

HuoJnx commented 4 years ago

It can't activate a page like that the traditional R console does. image

Even that I cat the outputs in a file and then open it, the results are not the same as that created by the console. image image

benjaminrich commented 4 years ago

I've never used jupyter notebook, and I have no idea how it works, unfortunately. Does it work with other R packages that produce HTML output (like flextable, huxtable, htmlTable, ..)?

You are right though that you cannot simply save the output to a file and open it in the browser, because it will not format correctly without the associated CSS. If you paste the output in an HTML file that is linked with or contains the suitable CSS, you will get the same output. This is by design; it is easier and more flexible to customize the table appearance by writing custom CSS than by generating complex HTML code with R.

HuoJnx commented 4 years ago

Thank you for your reply. I found that most of the packages producing HTML output are not at work, so it may be a common problem. It seems that I should ask Jupyter Notebook for help.

HuoJnx commented 4 years ago

I've never used jupyter notebook, and I have no idea how it works, unfortunately. Does it work with other R packages that produce HTML output (like flextable, huxtable, htmlTable, ..)?

You are right though that you cannot simply save the output to a file and open it in the browser, because it will not format correctly without the associated CSS. If you paste the output in an HTML file that is linked with or contains the suitable CSS, you will get the same output. This is by design; it is easier and more flexible to customize the table appearance by writing custom CSS than by generating complex HTML code with R.

OK, I'm still not familiar with the CSS but I will have a try, thank you~

HuoJnx commented 4 years ago

I have solved this problem. The solution is as follows:

  1. Find the CSS file and get its path in table1 package;
  2. Concatenate the CSS-import code with the results from table1;
  3. Write the whole output into an Html file;
  4. Call the shell to open the file.

    The main codes are as follows:

    The underlined code is the path of my package's CSS file, it's in the table1 package. image The reuslt image

    A little problem

    But if I change the "topclass " option, the style will be incorrect again, I don't know how to deal with it yet. image image

benjaminrich commented 4 years ago

It's great that you were able to solve your own problem, and thanks for posting the solution here.

I think I can improve on it a little bit though. I've been playing around with jupyter notebook a bit, and I wrote this function to display the result of table1 directly inside the notebook:

require(htmltools)
require(IRdisplay)
require(table1)

display_jupyter <- function(x) {
  css <- system.file("table1_defaults_1.0/table1_defaults.css", package="table1")
  css <- paste(readLines(css), collapse="\n")
  x <- htmltools::tagList(htmltools::tags$style(css), htmltools::tags$div(class="Rtable1", x))
  IRdisplay::display_html(as.character(x))
}

Here is an example to try it out:

dat <- data.frame(x=rep(LETTERS[1:3], each=20), y=rnorm(60), z=rnorm(60))
x <- table1(~ y + z | x, data=dat)
display_jupyter(x)

Result: Screenshot from 2020-04-04 18 30 52

You get the zebra stripes "for free", because the style is a mix of jupyter css an table1 default css, but it looks pretty good. If there were a way to detect that we running inside a jupyter notebook we could make this all work seamlessly inside the package, but for now I haven't figured out how to do that.

HuoJnx commented 4 years ago

Oh! Thank you for your patient reply and improvement! But I found that the style of the outputs from the Jupyter page will be dismissed if I copied it to the Word. image By the way, it seems that the zebra style is an inherent style from the IRdisplay, I can get it without any extra setting, but I don't know how it works. image

benjaminrich commented 3 years ago

Closing this old issue.