gpt4 book ai didi

r - 使用接收器转移输出时 dput 写在哪里?

转载 作者:行者123 更新时间:2023-12-04 15:30:00 24 4
gpt4 key购买 nike

我想看看dput的结果在控制台中,同时使用 sink 将输出重定向到文件.

> sink(file = 'test.txt', split = TRUE)
> x <- 2^(1:4)
> x # test.txt now contains: [1] 2 4 8 16
[1] 2 4 8 16
> dput(x) # where does this return value go?
> dput(x, file = 'test.txt') # test.txt is overwritten with: c(2, 4, 6, 8)

为什么 x将其值打印到控制台(如预期),但 dput(x)才不是?

(我在 Windows 7 上使用 R 3.4.3 和 RStudio 版本 1.1.423)

最佳答案

dput实际上是在预期的地方写入输出,但没有在预期的时候写入。
运行以下代码显示 dput输出保持挂起,直到下一个正常输出:

sink(file = 'test.txt', split = TRUE)
x <- 2^(1:4)
x
dput(2*x,file="")
3*x

...给出一个 test.txt :
[1]  2  4  8 16
c(4, 8, 16, 32)
[1] 6 12 24 48

或者,运行 sink()关闭文件的函数也将强制挂起输出(但会关闭连接)。
sink(file = 'test.txt', split = TRUE)
x <- 2^(1:4)
x
dput(2*x,file="")
sink()

关于r - 使用接收器转移输出时 dput 写在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49116123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com