Print() 函数不在循环/函数(R 代码)中打印变量

Jan*_*ane 1 printing loops r function

我正在运行一个 while 循环并添加了一个 print(Variable) 以便我的代码在函数运行时显示该变量但它没有打印。这是我的代码的样子:

Variable = matrix[ncol=4, nrow=3]
Variable[,1] <- c(1,1,1,1)
Variable[,2] <- c(2,2,2,2)
Variable[,3] <- c(3,3,3,3)

x=1
while(x==1)
{x <- readline("Do any columns need to be combined? Yes=1 No=0  Ans= ")
 y <- headline("First column to combine:  " )
 z <- headline("Second column to combine:  ")
Variable[,y] <- Variable[,y] + Variable[,z]
Variable = as.data.table(Variable)
Variable = Variable[,as.numeric(z):=NULL]
print(Variable)
x <- headline("Do any more columns need to be combined? Yes=1 No=0 Ans= ")
}
Run Code Online (Sandbox Code Playgroud)

我在“print(Variable)”行之前和之后都尝试过flush.console()

我试过ctr + W应该调出 Misc 菜单(这只是基本的 R 吗?我正在使用 R Studio。我假设你应该在 ctrl + W 之前点击控制台,但这只是关闭我打开的脚本)。

我试过message()函数,它将变量打印为c(1,1,1,1)c(2,2,2,2)c(3,3,3,3)

我试过cat()得到了我的错误(Error in cat(list(...), file, sep, fill, labels, append) : argument 1 (type 'list') cannot be processing by 'cat' ))

我已经尝试了return()函数,它给了我错误Error: no function to return from, jumping to top level

我从类似的帖子中发现的:

在函数内部打印或显示变量

https://stat.ethz.ch/pipermail/r-help/2002-November/027348.html

但我还没有找到解决我的问题的方法。任何人都有任何其他想法或可能知道为什么它不打印以及我能做些什么?先感谢您!

Jan*_*ane 5

print.data.frame(Variable) 作品。当然,我在发布后两秒钟就想通了……但是一小时又一小时地搞砸了。

  • [rubber-ducking](https://en.wikipedia.org/wiki/Rubber_duck_debugging) 得一分! (2认同)