The cat
function will print to a device (console by default) and not add any of the usual annotations, but it cannot accept a list as an argument, so everything needs to be an atomic vector. The deparse( substitute())
gambit is the way to recover names of lists that were passed to a function. Just using names(x)
inside the function fails to recover the name of the original argument.
mylist <- list(first =c( 234984, 10354, 41175, 932711, 426928),
second =c(1693237, 13462))
fnlist <- function(x){ z <- deparse(substitute(x))
cat(z, "\n")
nams=names(x)
for (i in seq_along(x) ) cat(nams[i], x[[i]], "\n")}
fnlist(mylist)
mylist
second 234984 10354 41175 932711 426928
first 1693237 13462
This version would output a file (and you could substitute "\t" if you wanted tabs between names and values
fnlist <- function(x, fil){ z <- deparse(substitute(x))
cat(z, "\n", file=fil)
nams=names(x)
for (i in seq_along(x) ){ cat(nams[i], "\t", x[[i]], "\n",
file=fil, append=TRUE) }
}
fnlist(mylist, "test")