如何缩进多行输出(如摘要)的输出,使用print或cat,并保持列对齐?

问题描述 投票:2回答:1

该问题可能会概括为打印任何类型的多行输出,并保持列对齐。我的实际问题与summary.default的输出有关。

我正在尝试向cat函数的summary.default输出添加2的缩进。我已经尝试过使用catprint,还参考了一些相关的答案,但到目前为止它们都失败了。

f <- function(x) {
  s <- summary.default(x)
  liner <- Reduce(paste0, rep("-", 70))
  cat("\n", liner, "\n")  ## ATT 1. -------------------------------
  cat("\n", "foo first attempt", "\n")
  cat("\n--|\n")

  print(round(s, 3))  #

  cat("\n----|\n")
  cat("\n    *additional information\n")
  cat("\n", liner, "\n")  ## ATT 2. -------------------------------
  cat("\n", "foo second attempt", "\n")
  cat("\n--|\n")

  print(unname(as.data.frame(cbind("   ", t(attr(s, "names"))))), row.names=F)  #
  print(unname(as.data.frame(cbind(" ", t(round(unclass(s), 3))))), row.names=F)  #

  cat("\n----|\n")
  cat("\n    *additional information\n")
  cat("\n", liner, "\n")  ## ATT 3. -------------------------------
  cat("\n", "foo third attempt", "\n")
  cat("\n--|\n")
  cat("\n  ", attr(s, "names"), "\n")

  cat("\n  ", round(s, 3), "\n")  #

  cat("\n----|\n")
  cat("\n    *additional information\n")
}

> x <- rnorm(100)
> f(x)

---------------------------------------------------------------------- 

 foo first attempt 

--|
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 -2.069  -0.654  -0.092  -0.075   0.696   1.997 

----|

    *additional information

 ---------------------------------------------------------------------- 

 foo second attempt 

--|

     Min. 1st Qu. Median Mean 3rd Qu. Max.

   -2.069 -0.654 -0.092 -0.075 0.696 1.997

----|

    *additional information

 ---------------------------------------------------------------------- 

 foo third attempt 

--|

   Min. 1st Qu. Median Mean 3rd Qu. Max. 

   -2.069 -0.654 -0.092 -0.075 0.696 1.997 

----|

    *additional information

---|表示所需的缩进。 首次尝试没有任何缩进。 第二次尝试更好,但是两行之间还有额外的空间,并且在四舍五入到不同的数字时不能一概而论。在attatt 3处,列不再对齐。

如何使用R附带的功能获得所需的输出?

所需的输出:

---------------------------------------------------------------------- 

foo some text 

    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  -2.069  -0.654  -0.092  -0.075   0.696   1.997 

    *additional information
r printing indentation cat summary
1个回答
0
投票

我唯一想出的就是手动重新组合“摘要”输出以解决不同的“单元格”宽度:

add_indent_and_right_align <- function(summry, indent=2, minDist= 2, rounding = 3) {

  header <- attr(summry, "names")
  value <- round(summry, 3)     
  r = list()
  for (i in seq_along(header)) {
    max_len <- max(nchar(header[i]), nchar(value[i]))       
    if (i == 1) {
      cell_width <- max_len + indent
      r[1] <- paste0(paste0(rep(" ", cell_width - nchar(header[i])), collapse=""), header[i])
      r[2] <- paste0(paste0(rep(" ", cell_width - nchar(value[i])), collapse=""), value[i])
    } else {
      cell_width <- max_len + minDist
      r[1] <- paste0(r[1], paste0(rep(" ", cell_width - nchar(header[i])), collapse=""), header[i])
      r[2] <- paste0(r[2], paste0(rep(" ", cell_width - nchar(value[i])), collapse=""), value[i])
    }
  }
  return(paste0(r, collapse="\n"))
}

f <- function(x) {
  indent <- 4 # how many spaces shoud the whole summary output be indented?
  s <- summary.default(x)

  cat(paste0(rep("-", 70), collapse=""), "\n")
  cat("\n")
  cat("foo some text", "\n")
  cat("\n")
  cat(add_indent_and_right_align(summry=s, indent=4, minDist= 1, rounding = 3), "\n")
  cat("\n")
  cat(paste0(paste0(rep(" ", indent), collapse=""), "*additional information"), "\n")
  cat("\n")
}

set.seed(1)
x <- rnorm(100)
f(x)


Returns:

---------------------------------------------------------------------- 

foo some text

    Min. 1st Qu. Median  Mean 3rd Qu.  Max.
  -2.215  -0.494  0.114 0.109   0.692 2.402

  *additional information
© www.soinside.com 2019 - 2024. All rights reserved.