모든 콘솔 출력을 R의 파일에 저장하는 방법은 무엇입니까?
모든 콘솔 텍스트를 파일 로 리디렉션하고 싶습니다 . 내가 시도한 것은 다음과 같습니다.
> sink("test.log", type=c("output", "message"))
> a <- "a"
> a
> How come I do not see this in log
Error: unexpected symbol in "How come"
test.log에서 얻은 내용은 다음과 같습니다.
[1] "a"
test.log에서 원하는 것은 다음과 같습니다.
> a <- "a"
> a
[1] "a"
> How come I do not see this in log
Error: unexpected symbol in "How come"
내가 도대체 뭘 잘못하고있는 겁니까? 감사!
"출력"과 "메시지"를 별도로 싱크해야합니다 ( sink
함수는의 첫 번째 요소 만 확인 type
).
이제 입력 도 기록되도록하려면 스크립트에 입력하십시오.
script.R
1:5 + 1:3 # prints and gives a warning
stop("foo") # an error
그리고 프롬프트에서 :
con <- file("test.log")
sink(con, append=TRUE)
sink(con, append=TRUE, type="message")
# This will echo all input and not truncate 150+ character lines...
source("script.R", echo=TRUE, max.deparse.length=10000)
# Restore output to console
sink()
sink(type="message")
# And look at the log...
cat(readLines("test.log"), sep="\n")
명령 줄에 액세스 할 수있는 경우 R CMD BATCH를 사용하여 명령 줄에서 스크립트를 실행하는 것을 선호 할 수 있습니다.
== 스크립트 내용 시작 R ==
a <- "a"
a
How come I do not see this in log
== 스크립트의 끝 내용 R ==
명령 프롬프트 (많은 un * x 변형에서는 "$", Windows에서는 "C :>")에서 다음을 실행합니다.
$ R CMD BATCH script.R &
후행 "&"는 선택 사항이며 백그라운드에서 명령을 실행합니다. 로그 파일의 기본 이름은 확장자에 "out"이 추가되어 있습니다 (예 : script.Rout).
== 스크립트 내용 시작 .Rout ==
R version 3.1.0 (2014-04-10) -- "Spring Dance"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: i686-pc-linux-gnu (32-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Previously saved workspace restored]
> a <- "a"
> a
[1] "a"
> How come I do not see this in log
Error: unexpected symbol in "How come"
Execution halted
== 스크립트의 끝 내용 .Rout ==
당신은 할 수 없습니다. 기껏해야 출력을로 저장 sink
하고로 입력 할 수 있습니다 savehistory
. 또는 script
, screen
또는 같은 외부 도구를 사용하십시오 tmux
.
ESS (Emacs Speaks Statistics) r- 모드로 emacs에서 R을 실행합니다. 내 스크립트와 R 코드로 하나의 창이 열려 있습니다. 다른 하나는 R이 실행 중입니다. 구문 창에서 코드가 전송되고 평가됩니다. 명령, 출력, 오류 및 경고는 모두 실행중인 R 창 세션에 나타납니다. 작업 기간이 끝나면 모든 출력을 파일에 저장합니다. 내 이름 지정 시스템은 스크립트의 경우 * .R이고 저장 출력 파일의 경우 * .Rout입니다. 다음은 예제가있는 스크린 샷입니다.
bash 쉘을 사용할 수있는 경우 bash 스크립트 내에서 R 코드를 실행하고 stdout 및 stderr 스트림을 파일로 파이프하는 것을 고려할 수 있습니다. heredoc를 사용한 예는 다음과 같습니다.
파일: test.sh
#!/bin/bash
# this is a bash script
echo "Hello World, this is bash"
test1=$(echo "This is a test")
echo "Here is some R code:"
Rscript --slave --no-save --no-restore - "$test1" <<EOF
## R code
cat("\nHello World, this is R\n")
args <- commandArgs(TRUE)
bash_message<-args[1]
cat("\nThis is a message from bash:\n")
cat("\n",paste0(bash_message),"\n")
EOF
# end of script
그런 다음 로그 파일에 파이프 된 stderr 및 stdout을 모두 사용하여 스크립트를 실행할 때 :
$ chmod +x test.sh
$ ./test.sh
$ ./test.sh &>test.log
$ cat test.log
Hello World, this is bash
Here is some R code:
Hello World, this is R
This is a message from bash:
This is a test
Other things to look at for this would be to try simply pipping the stdout and stderr right from the R heredoc into a log file; I haven't tried this yet but it will probably work too.
To save text from the console: run the analysis and then choose (Windows) "File>Save to File".
Set your Rgui preferences for a large number of lines, then timestamp and save as file at suitable intervals.
If you want to get error messages
zz <- file("Errors.txt", open="wt")
sink(zz, type="message")
output will be:
Error in print(errr) : object 'errr' not found
Execution halted
This output will be saved in a file named Errors.txt
In case you want printed values of console to a file you can use 'split' argument:
zz <- file("console.txt", open="wt")
sink(zz, split=TRUE)
print("cool")
print(errr)
output will be:
[1] "cool"
in console.txt file. So all your console output will be printed in a file named console.txt
참고URL : https://stackoverflow.com/questions/7096989/how-to-save-all-console-output-to-file-in-r
'Program Tip' 카테고리의 다른 글
동일한 컴퓨터에 여러 Python 버전이 있습니까? (0) | 2020.11.08 |
---|---|
정수 합산 파랑, 짧은 + = 짧은 문제 (0) | 2020.11.08 |
requirejs와 함께 reactjs 사용 (0) | 2020.11.08 |
Git 숨김 메시지 변경 (0) | 2020.11.08 |
Spring Websocket에서 특정 사용자에게 메시지 보내기 (0) | 2020.11.08 |