I have a list called tmp
which contains two elements like below.
> tmp
$a
integer(0)
$b
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[20] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
[39] 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
[58] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
[77] 77 78 79 80 81 82 83 84 85 86 ...
[ reached getOption("max.print") -- omitted 386732 entries ]
I would like to create my own text in console using paste()
or print()
to print all the values like this:
[1] "a contains integer(0)"
[1] "b contains 1, 2, 3, 4, 5, 6, 7, ..., 386732"
(used ...
for the convenience but I want all the values to be printed)
What I have tried is
names <- c('a', 'b')
#1
for (p in seq_along(names)) {
print(paste(names[p], 'contains', as.list(tmp[p])))
}
#2
cat(sprintf("%s contains %s\n", names, tmp))
and the output is
[1] "a contains integer(0)"
[1] "b contains 1:387732"
Does anyone know how I can print the entire list as one? Thank you!
I have a list called tmp
which contains two elements like below.
> tmp
$a
integer(0)
$b
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[20] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
[39] 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
[58] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
[77] 77 78 79 80 81 82 83 84 85 86 ...
[ reached getOption("max.print") -- omitted 386732 entries ]
I would like to create my own text in console using paste()
or print()
to print all the values like this:
[1] "a contains integer(0)"
[1] "b contains 1, 2, 3, 4, 5, 6, 7, ..., 386732"
(used ...
for the convenience but I want all the values to be printed)
What I have tried is
names <- c('a', 'b')
#1
for (p in seq_along(names)) {
print(paste(names[p], 'contains', as.list(tmp[p])))
}
#2
cat(sprintf("%s contains %s\n", names, tmp))
and the output is
[1] "a contains integer(0)"
[1] "b contains 1:387732"
Does anyone know how I can print the entire list as one? Thank you!
Share Improve this question edited Mar 28 at 2:15 Darren Tsai 36.3k5 gold badges25 silver badges57 bronze badges asked Mar 28 at 0:53 MTMTMTMT 611 silver badge3 bronze badges 1 |4 Answers
Reset to default 6You can use sapply
+ toString
, and then paste the result with its name.
x <- list(a = 0, b = c(1:5))
paste(names(x), "contains", sapply(x, toString))
# or
sprintf("%s contains %s", names(x), sapply(x, toString))
# [1] "a contains 0"
# [2] "b contains 1, 2, 3, 4, 5"
The issue you may be having is that you are trying to print a list element, rather than the underlying vector. Here is a working snippet:
x <- list(a=0, b=c(1:5))
unlist(sapply(names(x), function(y) paste(y, "contains", paste(x[[y]], collapse=","))))
a b
"a contains 0" "b contains 1,2,3,4,5"
I iterate the names of the input list implicitly using sapply
. Then I collapse each list element into a CSV string using paste
. Note carefully that I dereference each list element via x[[y]]
, so that I am working with the underlying vector rather than a list element.
Here's two for-loop variations:
tmp <- list(a=NULL, b=c(0,1,1,2,3,5,8,13))
for (p in 1:length(tmp)) {
cat(paste(names(tmp)[p], 'contains'), paste(tmp[[p]], collapse=","))
cat("\n")
}
a contains
b contains 0,1,1,2,3,5,8,13
for (p in 1:length(tmp)) {
print(paste(names(tmp)[p], 'contains', paste(tmp[[p]], collapse=",")))
}
[1] "a contains "
[1] "b contains 1,1,2,3,5,8,13"
Not exactly the same as what you are after, but should help a bit if you use capture.output
+ str
> tmp <- list(a = integer(0), b = 1:10)
> capture.output(str(tmp))[-1]
[1] " $ a: int(0) "
[2] " $ b: int [1:10] 1 2 3 4 5 6 7 8 9 10"
library(listviewer); jsonedit(temp);
– Tim G Commented Mar 28 at 7:42