R/roxy_functions.R

Defines functions roxy.func_top_block roxy.func_export roxy.func_returns roxy.func_family roxy.func_params roxy.func_top_line roxy.func_from_ext roxy.func_from_wd

Documented in roxy.func_family roxy.func_from_ext roxy.func_from_wd roxy.func_params roxy.func_top_line

# FUNCTION DOCUMENTATION SNIPPETS ------------------------------
#' Linking to functions in this package
#'
#'
roxy.func_from_wd <- function(func_name){
  roxy.wrap_lines(paste0('\\code{\\link{',func_name,'}}'))
}


#' Links to another package
roxy.func_from_ext <- function(func_name){
  # Find the function in another package
  pckg <- find(func_name) %>% stri_replace_all_regex("package:","")
  roxy.wrap_lines(paste0('\\code{\\link[',pckg,']{',func_name,'}}'))
}




# Formatting Functions-----------------------------
#' Top Block Formatting
#'
#' @param func_name
#'
roxy.func_top_line <- function(func_name){

  a <- readline('Short description of the function')

  a_out <-  roxy.wrap_lines(a)

  b <- readline('Description to append to the code_break line')

  b_out <- roxy.func(func_name,b)

  c <- readline('Long Description')

  c_out <- roxy.wrap_lines(c)

  txt.prep_html(paste(a_out,b_out,c_out,sep = "\n"))
}


#'Formatting Arguments for prebuilt functions
#'
roxy.func_params <- function(fun_name,cat_data = FALSE){

  parms_raw <-
    llply(formals(fun_name) %>% list,function(i)
      cbind(names = names(as.list(i)),
            vals = as.list((i))) %>%
        as.character) %>%
    chain.df('raw') %>%
    mutate(raw = ifelse(nchar(raw)==0,'input variable',raw)) %>%
    transform(raw = raw[1:(length(raw)/2)],
              raw_val = raw[((length(raw)/2)+1):length(raw)]) %>%
    filter(duplicated(raw) == F) %>%
    mutate(roxy_form = paste("@param ",raw," ",raw_val,sep = "") %>%
             stri_pad(side = 'right')) %>%
    select(roxy_form) %>%
    unlist %>% stri_trim_both %>% paste0(collapse = "\n")

  final_data <-
    c(stri_trim_both(
      strsplit(parms_raw,"\n") %>%
        chain.df('raw') %>%
        mutate(raw = stri_trim_both(raw)) %>%
        mutate(infos = llply(raw,function(i)
          paste("", i, " ", readline(cat(i," :")), collapse = "\n"))) %>%
        select(infos) %>%
        unlist %>%
        as.character),sep="\n")

  if(cat_data){
    roxy.wrap_lines(final_data)
  }else{
    outs <- txt.prep_html(final_data)
    return(outs)
  }

}





#' Generate family classes for functions
#'
#'
roxy.func_family <- function(...){
  roxy.wrap_lines(
    lapply(...,function(i)
      paste0("@family ",i," functions"))
    )
}



roxy.func_returns <- function(manual_write = TRUE,print_to_gui = FALSE){

  if(manual_write){
    base_msg <- " @return "

    a_in <- readline(paste0('What does the function Return?',collapse = "\n"))

    if(print_to_gui){
      txt.prep_html(paste0(base_msg,a_in,"\n"))
    }else{
      dat <- roxy.wrap_lines(paste0(base_msg,a_in))

      return(dat)
    }

  }else{
    classes <- class(x)
    lengths <- length(x)
    names <- names(x)
    summary <- summary(x)
    lens <- paste0(base_msg,classes,' structured item length of ',lengths)
    if(!is.null(names)){
      classed <- lapply(x,class)
      cnames <- roxy.list_define(classed)
      lens <- cat(c(lens," and named items ","\n",cnames))
    }
    return(lens)
  }
}


roxy.func_export <- function(manual_write = TRUE,print_to_gui = FALSE){
  export_prompt <- readline('Is this function exported y/n?')

  if(export_prompt == 'y'){
    dat <- txt.prep_html(paste0("\n","#' @export"))
  }else{
    dat <- (' ')
  }

  if(print_to_gui){
    roxy.wrap_lines(dat)
    }else{
      return(dat)
    }

}


roxy.func_top_block <- function(func_name){

  a_out <- roxy.func_top_line(func_name)

  b_out <- roxy.func_params(func_name,cat_data = FALSE)


  see_also_in <- readline('Is there a naming convention this function relates to?')
  c_out <- roxy.see_also(auto_build = see_also_in)

  fam_in <- readline('What family of functions does this belong to?')
  f_out <- roxy.func_family(fam_in)

  ret_in <- roxy.func_returns()

  do_export <- roxy.func_export()

  HTML(c(a_out, b_out, c_out, f_out, ret_in, do_export,sep = "\n"))

}
CarlBoneri/autorox documentation built on June 1, 2017, 10:16 p.m.