Search code examples
rrstudioroxygen2package-development

rStudio autocompletion description and usage


I am pretty new in package development for R.

I develop a small package in R using the manual from Hadley Wickem http://adv-r.had.co.nz/Package-development-cycle.html

I switched to dev_mode(), install() my package and load it with library(package name)

So one example:

#' logging function
#' 
#' This function tries to use logging functionality. If the logging package 
#' isn't installed, it uses a normal print command
#' 
#' @param string the string to print
#' @param level the level of the log output, for example \code{WARN}, 
#' \code{INFO} or \code{ERROR}
#' @examples
#' \dontrun{
#' mylog('for your information')
#' mylog('this is a warning','WARN')
#' }
mylog <- function(string,level='INFO') {
  tryCatch(
    switch(level,
           WARN=logwarn(string),
           ERROR=logerror(string),
           INFO=loginfo(string),
           {logwarn(sprintf('warnlevel "%s" is not defined!',level))
            loginfo(string)}),
    error=function(condition) {
      cat(sprintf('%s: %s\n',level,string))
    })
}

When I now type ?mylog I get my help in the Help Window inside rStudio. But when I try to use auto complete with Tab, there is no information in this little pop-up window...

All the other packages have a little information there, how to use the function.


Solution

  • I found the solution... Or at least I hope so....

    Adding a @export tag in the documentation helps and provides the help in the autocompletion...

    #' logging function
    #' 
    #' This function tries to use logging functionality. If the logging package 
    #' isn't installed, it uses a normal print command
    #' 
    #' @param string the string to print
    #' @param level the level of the log output, for example \code{WARN}, 
    #' \code{INFO} or \code{ERROR}
    #' @export
    #' @examples
    #' \dontrun{
    #' mylog('for your information')
    #' mylog('this is a warning','WARN')
    #' }
    mylog <- function(string,level='INFO') {
      tryCatch(
        switch(level,
               WARN=logwarn(string),
               ERROR=logerror(string),
               INFO=loginfo(string),
               {logwarn(sprintf('warnlevel "%s" is not defined!',level))
                loginfo(string)}),
        error=function(condition) {
          cat(sprintf('%s: %s\n',level,string))
        })
    }