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
--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()
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.
In R, we can write data frames easily to a file, using the write. table() command.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With