1

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)

enter image description here

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

user63230
  • 4,095
  • 21
  • 43

2 Answers2

2

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:

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • +1 this is very clever, thanks. The follow up question is how/can I apply one of the `RStudio` themes to this? Lets say I wanted `tomorrow.rstheme`, see code [here](https://github.com/rstudio/rstudio/blob/master/src/cpp/session/resources/themes/tomorrow.rstheme). This looks like `css` code to me so I copied it to `my.css` but nothing happened. Is this a correct approach or do I need to edit the `css` code in some way? – user63230 Oct 14 '20 at 19:10
  • 1
    @user63230 the `rstheme` files specify code highlighting etc within the rstudio console, and though they are similar to css, I don't think they include the basic html tags that would style the help files in the viewer window. You would probably need to copy across the colors, fonts etc to write css that emulated the look you want. – Allan Cameron Oct 19 '20 at 14:48
  • thanks I actually asked a follow up question today [here](https://stackoverflow.com/questions/64424229/apply-rstudio-editor-themes-to-help-files) that expands on my question. So you think you can't colour special words etc. without doing it manually? – user63230 Oct 19 '20 at 14:52
1

As of RStudio v1.2 you can style RStudio's integrated help pane by creating a custom user theme (essentially an .rstheme file).

I've given help pane styling a try in extending the rscodeio theme (without colored syntax highlighting, though). The latest CSS code is found here.

The help pane styling is currently only available in the optional Tomorrow Night Bright (rscodeio) editor theme.

To use it right away, you can either

  • install the current rscodeio master branch using remotes:

    remotes::install_github("anthonynorth/rscodeio")
    

    And then activating the editor theme named Tomorrow Night Bright (rscodeio) under ToolsGlobal Options…AppearanceEditor theme. A first attempt of the help pane CSS code is included.

  • or – recommended – install my fork's interim-merge branch which contains all my latest work[1] overhauling the package, including a new apply_theme parameter to activate the desired editor theme right away:

    remotes::install_github("salim-b/rscodeio@interim-merge")
    rscodeio::install_themes(apply_theme = "Tomorrow Night Bright (rscodeio)")
    

[1]: This has also been proposed upstream a while ago (1, 2) but I haven't heard back from the author since.

The result looks as follows (example for ?pal::as_string):

Salim B
  • 2,409
  • 21
  • 32
  • this looks great! I need to play around with it some more. I had actually asked a similar question on the `thematic` page only a few days ago [here](https://github.com/gadenbuie/rsthemes/issues/54) as I thought it might fit in with their project – user63230 Jan 24 '21 at 20:09
  • cool, I've left a comment linking to my CSS code in the issue you mentioned :) – Salim B Jan 24 '21 at 22:13