在R中逐行打印文本向量

时间:2017-07-30 18:26:47

标签: r vector printing line

我偶然得到了一个由capture.output函数创建的对象,它可以逐行打印,例如:

> print(xxx)
[1] "==============================================================================="
[2] "Initial design evaluation"                                                      
[3] ""                                                                               
[4] "Initial OFV = 28.9197"                                                          
[5] ""                                                                               
[6] "Initial design expected parameter "    

但是,当我尝试重现类似的字符串向量时,所有向量元素都打印在同一行上。 e.g:

> a  <- c('a','b','c','d','e')
> print(a)
[1] "a" "b" "c" "d" "e"

我发现它们之间没有任何区别:

> length(a)
[1] 5
> length(xxx)
[1] 5
> 
> class(xxx)
[1] "character"
> class(a)
[1] "character"
> 
> typeof(xxx)
[1] "character"
> typeof(a)
[1] "character"

我想知道是什么导致了印刷品的独特行为。

2 个答案:

答案 0 :(得分:0)

我认为这是控制台中可用水平空间的问题。至少在我的机器中,每当要打印的两个矢量元素足够宽以至于你无法将它们中的两个放在一行中时,矢量中的所有元素都会在每一行上打印一个。我相信这就是你的xxx向量中第一长串等号的含义。我可以编写我用来检查的代码,但这完全取决于我的控制台的宽度,所以我不认为它对其他人有用。

答案 1 :(得分:0)

这是一个试图模仿print()行为的函数:

print_oneperline <- function(x) 
  cat(sprintf(paste0('% ', floor(log10(length(x))) + 3,'s "%s"\n'), 
              paste0("[", seq_along(x), "]"), x), sep = "")

这是它的工作方式:

letters[1:5]
[1] "a" "b" "c" "d" "e"
print_oneperline(letters[1:5])
[1] "a"
[2] "b"
[3] "c"
[4] "d"
[5] "e"

xxx <- rep(c(
  "===============================================================================",
  "Initial design evaluation", "", "Initial OFV = 28.9197", 
  "Initial design expected parameter "), 3)
xxx    # regular print
 [1] "==============================================================================="
 [2] "Initial design evaluation"                                                      
 [3] ""                                                                               
 [4] "Initial OFV = 28.9197"                                                          
 [5] "Initial design expected parameter "                                             
 [6] "==============================================================================="
 [7] "Initial design evaluation"                                                      
 [8] ""                                                                               
 [9] "Initial OFV = 28.9197"                                                          
[10] "Initial design expected parameter "                                             
[11] "==============================================================================="
[12] "Initial design evaluation"                                                      
[13] ""                                                                               
[14] "Initial OFV = 28.9197"                                                          
[15] "Initial design expected parameter "                                             
> print_oneperline(xxx)
 [1] "==============================================================================="
 [2] "Initial design evaluation"
 [3] ""
 [4] "Initial OFV = 28.9197"
 [5] "Initial design expected parameter "
 [6] "==============================================================================="
 [7] "Initial design evaluation"
 [8] ""
 [9] "Initial OFV = 28.9197"
[10] "Initial design expected parameter "
[11] "==============================================================================="
[12] "Initial design evaluation"
[13] ""
[14] "Initial OFV = 28.9197"
[15] "Initial design expected parameter "

此外,您可以按照已接受的答案说明的屏幕宽度进行播放:

options(width=10)