R 如何去除输出中的列表位置/行号

R 如何去除输出中的列表位置/行号,r,list,function,readability,R,List,Function,Readability,我想显示一个函数的许多结果,但我已经读到,您只能返回一个对象,因此,如果您想显示更多,必须使用列表。这可以正常工作,但是有时候输出的可读性不是很好(在这个假例子中,它还不错,但在我的工作中是这样)。如何消除或抑制R自动添加到输出中的这些行/列表位置 电流输出: [[1]] [1] "There are 5 total observations" [[2]] [1] "The mean of these observations is 0.564422113896047" [[3]] [1]

我想显示一个函数的许多结果,但我已经读到,您只能返回一个对象,因此,如果您想显示更多,必须使用列表。这可以正常工作,但是有时候输出的可读性不是很好(在这个假例子中,它还不错,但在我的工作中是这样)。如何消除或抑制R自动添加到输出中的这些行/列表位置

电流输出:

[[1]]
[1] "There are 5 total observations"

[[2]]
[1] "The mean of these observations is 0.564422113896047"

[[3]]
[1] "The observations are shown below:"

[[4]]
[1]  1.0496648  0.4807251  0.8536269  1.7946839 -1.3565901
期望输出:

"There are 5 total observations"


"The mean of these observations is 0.564422113896047"


"The observations are shown below:"


 1.0496648  0.4807251  0.8536269  1.7946839 -1.3565901
我很高兴能够解决能够删除以上每行双括号输出,但保持行号输出。如果我也可以改变各个点的行距,那就更好了,但不是真的需要

用于创建此函数/输出的代码:

test <- function(n_observations) {

  obs <- rnorm(n_observations)

return(list(
  paste0("There are ",n_observations," total observations"),
  paste0("The mean of these observations is ",mean(obs)),
  paste0("The observations are shown below:"),
  obs
))

}

test(n_observations = 5)

如果您想改进输出的打印方式,我们可以使用
cat
,包括在每行后面加“\n”以在不同的行中显示输出

test <- function(n_observations) {
   obs <- rnorm(n_observations)

   return(cat(
     paste0("There are ",n_observations," total observations\n"),
     paste0("The mean of these observations is ",mean(obs), "\n"),
     paste0("The observations are shown below:\n"),
     obs
   ))
}

test(n_observations = 5)

#There are 5 total observations
# The mean of these observations is -0.785794194405614
# The observations are shown below:
# -0.4806757 -0.6366636 0.3147989 -1.873661 -1.25277

最后一个
print(random\u table)
可以避免,我们只能使用
random\u table
,但我假设OP还有很多类似的东西可以
print
,所以在这种情况下它可能会很有用。

感谢您提供了这两个示例,是的,我有许多不同的表要显示,但您的方法可以很好地使用它。
There are 5 total observations

 The mean of these observations is 0.445438123798109
 The observations are shown below:
 1.677665 1.379066 0.3436419 0.4783038 -1.651487 Error in cat(paste0("There are ", n_observations, " total observations\n"),  : 
  argument 6 (type 'list') cannot be handled by 'cat'
test <- function(n_observations) {
   obs <- rnorm(n_observations)

   return(cat(
     paste0("There are ",n_observations," total observations\n"),
     paste0("The mean of these observations is ",mean(obs), "\n"),
     paste0("The observations are shown below:\n"),
     obs
   ))
}

test(n_observations = 5)

#There are 5 total observations
# The mean of these observations is -0.785794194405614
# The observations are shown below:
# -0.4806757 -0.6366636 0.3147989 -1.873661 -1.25277
test <- function(n_observations) {

  obs <- rnorm(n_observations)
  random_table <- as.data.frame(cbind(c(1:n_observations), obs))


  cat(paste0("There are ",n_observations," total observations\n"),
  paste0("The mean of these observations is ",mean(obs), "\n"),
  paste0("The observations are shown below:\n"),
  obs, "\n\n The table is as below : \n\n")
  print(random_table)
}

test(n_observations = 5)

#There are 5 total observations
# The mean of these observations is 0.540141211615552
# The observations are shown below:
# 1.922104 -0.5334201 -0.9881913 1.838563 0.4616504 

# The table is as below : 

#  V1        obs
#1  1  1.9221042
#2  2 -0.5334201
#3  3 -0.9881913
#4  4  1.8385628
#5  5  0.4616504