3

How can we get the source of R Documentation for an existing function (the contents of the *.Rd), say plot or lubridate::ymd. Something for documentations that is similar to fix for functions?

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
d.b
  • 32,245
  • 6
  • 36
  • 77
  • Maybe use `system` or `shell` to open it? Maybe something like `shell("start package/man/function_doc.rd")`(Windows) assuming I understood what you would like to do. – NelsonGon Aug 25 '19 at 15:50
  • Although `Rd` files are apparently read-only if roxygen generated. Then perhaps using `shell` on the corresponding `R` file might be a better idea. – NelsonGon Aug 25 '19 at 15:56
  • 1
    @NelsonGon, how would you modify `shell("start package/man/function_doc.rd")` for `lubridate::ymd`. read-only is fine. – d.b Aug 25 '19 at 16:00
  • 1
    @d.b I was thinking in a custom package's point of view. However, looking at the answer below(and the function's) source code shows that these files are saved in paths. So you would need to go to `.libpaths()` and then to the function's `help/paths`, `readRDS` which has already been automated. :) – NelsonGon Aug 25 '19 at 16:18

1 Answers1

8

tools::Rd_db creates a named list of parsed .Rd files for an installed package. For instance, to print the contents of lubridate's .Rd file ymd.Rd, you could use:

## create list of parsed .Rd files
db <- tools::Rd_db("lubridate")

## names of .Rd files
names(db)
#>  [1] "DateCoercion.Rd"      "DateTimeUpdate.Rd"    "Deprecated.Rd"       
#>  [4] "Duration-class.Rd"    "Interval-class.Rd"    "Period-class.Rd"     
#>  [7] "Timespan-class.Rd"    "am.Rd"                "as.duration.Rd"      
#> [10] "as.interval.Rd"       "as.period.Rd"         "as_date.Rd"          
#> [13] "date.Rd"              "date_decimal.Rd"      "day.Rd"              
#> [16] "days_in_month.Rd"     "decimal_date.Rd"      "dst.Rd"              
#> [19] "duration.Rd"          "fit_to_timeline.Rd"   "force_tz.Rd"         
#> [22] "guess_formats.Rd"     "hidden_aliases.Rd"    "hms.Rd"              
#> [25] "hour.Rd"              "interval.Rd"          "is.Date.Rd"          
#> [28] "is.POSIXt.Rd"         "is.difftime.Rd"       "is.instant.Rd"       
#> [31] "is.timespan.Rd"       "lakers.Rd"            "leap_year.Rd"        
#> [34] "local_time.Rd"        "lubridate-package.Rd" "make_datetime.Rd"    
#> [37] "make_difftime.Rd"     "minute.Rd"            "month.Rd"            
#> [40] "mplus.Rd"             "now.Rd"               "origin.Rd"           
#> [43] "parse_date_time.Rd"   "period.Rd"            "period_to_seconds.Rd"
#> [46] "pretty_dates.Rd"      "quarter.Rd"           "reclass_date.Rd"     
#> [49] "reclass_timespan.Rd"  "rollback.Rd"          "round_date.Rd"       
#> [52] "second.Rd"            "stamp.Rd"             "time_length.Rd"      
#> [55] "timespan.Rd"          "today.Rd"             "tz.Rd"               
#> [58] "week.Rd"              "with_tz.Rd"           "within-interval.Rd"  
#> [61] "year.Rd"              "ymd.Rd"               "ymd_hms.Rd"

## print ymd.Rd
db[["ymd.Rd"]]
#> \title{Parse dates with \strong{y}ear, \strong{m}onth, and \strong{d}ay components}\name{ymd}\alias{ymd}\alias{ydm}\alias{mdy}\alias{myd}\alias{dmy}\alias{dym}\alias{yq}\keyword{chron}\description{
#> Transforms dates stored in character and numeric vectors to Date or POSIXct
#> objects (see \code{tz} argument). These functions recognize arbitrary
#> non-digit separators as well as no separator. As long as the order of
#> formats is correct, these functions will parse dates correctly even when the
#> input vectors contain differently formatted dates. See examples.
#> }\usage{
#> ymd(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"),
#>   truncated = 0)
#> 
#> ydm(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"),
#>   truncated = 0)
#> 
#> mdy(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"),
#>   truncated = 0)
#> 
#> myd(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"),
#>   truncated = 0)
#> 
#> dmy(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"),
#>   truncated = 0)
#> 
#> dym(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"),
#>   truncated = 0)
#> 
#> yq(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"))
#> }
#> ... ETCETERA ...
Joris C.
  • 5,721
  • 3
  • 12
  • 27