Closed HuoJnx closed 3 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.
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.
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~
The underlined code is the path of my package's CSS file, it's in the table1 package. The reuslt
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.
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:
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.
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. 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.
Closing this old issue.