R/attr_extract.r

Defines functions attr_extract

Documented in attr_extract

#------------------------------------------ attr_extract ------------------------------------------
#' Reads in attributes from an external excel file
#'
#' This function extracts attributes available in a data frame and creates a structured list 
#'
#' @param dfrm data frame containing the attributes
#' @keywords documentation
#' @export
#' @return named list with the attributes
#' @author Richard Hooijmaijers
#' @examples
#' attrl  <- attr_xls(system.file("example/Attr.Template.xlsx",package = "amp.dm"))
#' nm     <- read.csv(system.file("example/NM.theoph.V1.csv",package = "amp.dm"))
#' nmf    <- attr_add(nm, attrl, verbose = FALSE)
#' attrl2 <- attr_extract(nmf)
#' all.equal(attrl,attrl2)
attr_extract <- function(dfrm){

  attrl <- try(lapply(names(dfrm), function(x){
    label   <- attr(dfrm[,x],"label")
    format  <- attr(dfrm[,x],"format")
    remark  <- attr(dfrm[,x],"remark")
    list(label = label, format = format, remark = remark)
  }), silent = TRUE)

  if(inherits(attrl,"try-error")) cli::cli_abort("Could not create attribute list")
  attrl <- stats::setNames(attrl,names(dfrm))
   
  return(attrl)
}

Try the amp.dm package in your browser

Any scripts or data that you put into this service are public.

amp.dm documentation built on March 13, 2026, 5:08 p.m.