自定义R帮助文件-字体着色 [英] customise R help files - font colouring

查看:45
本文介绍了自定义R帮助文件-字体着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以自定义R帮助文件,以便某些文本采用颜色编码且更易于阅读. rdoc 已经执行了此操作,只是将输出发送到控制台.相反,我希望将其发送到帮助面板(我正在使用 Rstudio ).有什么解决方法吗?

如果我们正常运行?lm ,我们可以在下面右侧的帮助面板中看到通常的帮助文件,但是当您在 Rstudio ,我们获得了帮助文件的颜色编码,虽然颜色不错,但是已发送到控制台输出(左侧).理想情况下,我们希望在运行代码时将其保留在帮助面板中.现在的样子-它在您运行某些操作时便消失了.

 ?lm#devtools :: install_github(" mdequeljoe/rdoc")库(rdoc)选项(rdoc.by_section = FALSE)rdoc(lm) 

我想将代码放入@ code.rprofile 中,类似于@csgillespie

I'm wondering is it possible to customise R help files so that certain text is colour coded and easier to read. rdoc already does this except that it sends the output to the console. I would instead, like it to be sent to the help panel (i'm using Rstudio). Is there any workaround for this?

If we run ?lm normally, we can see the usual help file in the help panel on the right below but when you do it again after using rdoc in Rstudio we get the help file colour coded which is great but its sent to the console output (left side). Ideally, we would like it to remain on display in the help panel as we are running code. The way it is now - it disappears the minute you run something.

?lm
#devtools::install_github("mdequeljoe/rdoc")
library(rdoc)
options(rdoc.by_section = FALSE)
rdoc(lm)

I want to put the code into my .rprofile similar to @csgillespie .rprofile. Note, if you follow his code you can use ?lm instead of having to call rdoc(lm) directly to produce the colour coded console output.

I have a feeling this can't be done easily (if at all?) but interested to hear any suggestions.

Thanks

解决方案

This is possible, but a little involved. You'll need your own css file defined to do it, though it would be possible to create a function that writes appropriate css.

As a proof of concept, I have used a copy of the "R.css" file defined inside every package's "/html" folder, and just changed the h2 color to red, with the file saved locally as "my.css".

Anyway, once you have the css file, this function will show the appropriate help file with the appropriate styling in your R viewer window:

help_css <- function(func, css_file)
{
  pack <- sub("^.*:(.*)>$", "\\1", capture.output(environment(func)))
  func <- deparse(substitute(func))
  
  x <- readLines(paste0(find.package(pack), "/html/", func, ".html"))
  x[3] <- paste("<style>", 
                paste(readLines(css_file), collapse = "\n"), 
                "</style>")
  writeLines(x, file.path(tempdir(), "test.html"))
  
  myViewer <- getOption("viewer")
  myViewer(file.path(tempdir(), "test.html"))
}

So, for example, if I do:

help_css(lm, "my.css")

I get:

这篇关于自定义R帮助文件-字体着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆