简体   繁体   English

R - 如何将 HTML 代码嵌入到 Jupyter 笔记本输出中?

[英]R - how to embed HTML code into Jupyter notebook output?

How can I do something like this using R?我怎样才能做到像这样,使用R? Actually I want to set Jupyter notebook cells width to 100%.实际上我想将 Jupyter 笔记本单元格宽度设置为 100%。 In python this code works perfectly:在python中,此代码完美运行:

from IPython.core.display import HTML, display
HTML("<style>.container { width: 100%; }</style>")

Is there any equivalent in R? R中是否有任何等价物?

Thank you!谢谢!

You can display html code in R using the display_html function eg ,您可以使用display_html函数在 R 中显示 html 代码,例如

// for full width
IRdisplay::display_html('<style>.container { width:100% !important; }</style>')

// this does not work with most sites due to same-origin policy, but no problem with local files   
IRdisplay::display_html('<iframe src="http://www.w3schools.com" width=1000, height=1000></iframe> ') 

See: SecurityError: Blocked a frame with origin from accessing a cross-origin frame请参阅: SecurityError: Blocked a frame with origin from access a cross-origin frame

Embed html code and html table to IR Kernel jupyter将 html 代码和 html 表嵌入到 IR 内核 jupyter

  library(IRdisplay)

  display_html("your html code")

Some packages in R give tables in html format like "knitr", so if you want to put this tables in the notebook: R 中的一些包以 html 格式提供表格,如“knitr”,所以如果你想把这些表格放在笔记本中:

library(knitr)
library(kableExtra)

dt <- mtcars[1:5, 1:6]
options(knitr.table.format = "html") 
html_table= kable(dt) %>%
   kable_styling("striped") %>%
   add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2, "Group 3" = 2))

display_html(toString(html_table))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM