I want to write a list to a text file, preserving the names.
This is similar to R: Print list to a text file but with names which I want to print out also, at the start of each line:
> print(head(mylist,2))
$first
[1] 234984 10354 41175 932711 426928
$second
[1] 1693237 13462
mylist.txt
first 234984 10354 41175 932711 426928
second 1693237 13462
Any ideas?
Many thanks.
The
catfunction 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. Thedeparse( substitute())gambit is the way to recover names of lists that were passed to a function. Just usingnames(x)inside the function fails to recover the name of the original argument.This version would output a file (and you could substitute “\t” if you wanted tabs between names and values