有没有办法记录或记录R函数的输出?

Ido*_*tam 6 r rstudio

假设我在R中运行一些代码.例如:

x <- 1

if (x == 0.1){
    print('X = 0.1')
    } else if (x > 0.1){
    print('X is bigger than 0.1 ')
    } else {
    print('X is smaller than 0.1')
    }
Run Code Online (Sandbox Code Playgroud)

如果我查看R studio中的历史文件,它会告诉我运行此条件语句但它不会显示结果(即X大于0.1).

有没有办法在R或R studio中自动记录输出?

ddu*_*801 8

直接输出到日志文件和屏幕:

sink("myfilename", append=FALSE, split=TRUE)  # for screen and log
Run Code Online (Sandbox Code Playgroud)

仅将输出返回到屏幕:

sink()
Run Code Online (Sandbox Code Playgroud)

来自Quick-R

  • 运行一次,然后将其后的所有内容保存到日志文件中,直到再次键入sink(). (2认同)