Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write contents of help to a file from within R?

Tags:

r

I'd like to be able to write the contents of a help file in R to a file from within R. The following works from the command-line:

  • R --slave -e 'library(MASS); help(survey)' > survey.txt
    • This command writes the help file for the survey data file
    • --slave hides both the initial prompt and commands entered from the resulting output
    • -e '...' sends the command to R
    • > survey.txt writes the output of R to the file survey.txt

However, this does not seem to work:

library(MASS)
sink("survey.txt")
help(survey)
sink()
  • How can I save the contents of a help file to a file from within R?
like image 648
Jeromy Anglim Avatar asked Sep 21 '11 02:09

Jeromy Anglim


People also ask

How do I save output to a file in R?

You can also save the entire R console screen within the GUI by clicking on "Save to File..." under the menu "File." This saves the commands and the output to a text file, exactly as you see them on the screen.

Which function is used to write data to file in R?

In R, we can write data frames easily to a file, using the write. table() command.


1 Answers

Looks like the two functions you would need are tools:::Rd2txt and utils:::.getHelpFile. This prints the help file to the console, but you may need to fiddle with the arguments to get it to write to a file in the way you want.

For example:

hs <- help(survey)
tools:::Rd2txt(utils:::.getHelpFile(as.character(hs)))

Since these functions aren't currently exported, I would not recommend you rely on them for any production code. It would be better to use them as a guide to create your own stable implementation.

like image 160
Joshua Ulrich Avatar answered Nov 16 '22 01:11

Joshua Ulrich