Reputation: 13103
I would like to format output of a print statement so that elements in the print statement are separated by a small uniform gap, a gap of either 1, 3 or 5 empty spaces. Is that possible?
In the example below, I think all of the elements in the print statement are assumed to be
as long as the first element in the print statement (here either as long as aa1
or aa2
).
The print.gap
statement does not result in the desired format. It simply adds an
additional constant number of spaces between each element.
aa1 <- 'abcdefghijklmnopqrstuvwxyz'
aa2 <- 'abc'
bb <- 10
cc <- 222
print(c(aa1,bb,cc))
# [1] "abcdefghijklmnopqrstuvwxyz" "10" "222"
print(c(aa2,bb,cc))
# [1] "abc" "10" "222"
# desired output:
# [1] "abcdefghijklmnopqrstuvwxyz" "10" "222"
#
# or
#
# [1] "abcdefghijklmnopqrstuvwxyz" 10 222
Thank you for any advice. Perhaps I must use a cat
statement and sprintf
instead of a print
statement. Maybe some variation of this: How to print R variables in middle of String
Upvotes: 3
Views: 15317
Reputation: 162311
This will get you partway there:
x <- c(aa1,bb,cc)
options(useFancyQuotes = FALSE)
cat(paste(dQuote(x), collapse=" "), "\n")
# "abcdefghijklmnopqrstuvwxyz" "10" "222"
I say "partway" because the code above doesn't take care of line-wrapping: if you hand it a very long vector of characters strings, they will all be cat'd to a single very long non-wrapping line...
Edited to show one method of wrapping the lines:
You can get line-wrapping by using a combination of strwrap()
(thanks @mnel) and gsub()
, :
xs <- c(x,x,x,x)
cat(gsub("\" \"", "\" \"",
strwrap(paste(dQuote(xs), collapse=" "), 70)), sep="\n")
# "abcdefghijklmnopqrstuvwxyz" "10" "222" "abcdefghijklmnopqrstuvwxyz"
# "10" "222" "abcdefghijklmnopqrstuvwxyz" "10" "222"
# "abcdefghijklmnopqrstuvwxyz" "10" "222"
(The call to gsub()
is needed if more than one space is needed between the vector elements, because strwrap()
destroys whitespace in the input.)
Upvotes: 4
Reputation: 115382
I'll post my comment as an answer
you can create your own print.character
method and use strwrap
to wrap to sensible line length.
Unfortunately strwrap
destroys all whitespace and replaces it with a single space
print.character <- function(x, width = getOption('width'), .sep = ' '){
cat(paste(strwrap(paste(dQuote(x), collapse = .sep, sep =.sep), width),
collapse = '\n'), '\n')
}
x <- c(aa1,bb,cc)
print(x)
## "abcdefghijklmnopqrstuvwxyz" "10" "222"
print(rep(x,10))
## "abcdefghijklmnopqrstuvwxyz" "10" "222" "abcdefghijklmnopqrstuvwxyz" "10" "222"
## "abcdefghijklmnopqrstuvwxyz" "10" "222" "abcdefghijklmnopqrstuvwxyz" "10" "222"
## "abcdefghijklmnopqrstuvwxyz" "10" "222" "abcdefghijklmnopqrstuvwxyz" "10" "222"
## "abcdefghijklmnopqrstuvwxyz" "10" "222" "abcdefghijklmnopqrstuvwxyz" "10" "222"
## "abcdefghijklmnopqrstuvwxyz" "10" "222" "abcdefghijklmnopqrstuvwxyz" "10" "222"
It will not work as expected with whitespace within the character variables
print('Does not work with strings with variable spaces that you \n might \t \t \t want to keep')
"Does not quite work with strings with variable spaces that you might want to keep"
Upvotes: 2