file io - questions about sink, in R -
lets want use sink writing file in r.
sink("outfile.txt") cat("line one.\n") cat("line two.") sink()
question 1. have seen people writing sink() @ end, why need this? can go wrong when not have this?
question 2. best way write many lines 1 one file for-loop, need format each line? might need have different number in each line, in python use outfile.write("line number %.3f",1.231) etc.
question 1: sink
function redirects text going stdout
stream file handler give sink
. means anything print out interactive r session, instead written file in sink, in case "outfile.txt". when call sink
again without arguments telling resume using stdout
instead of "outfile.txt". no, nothing go wrong if don't call sink()
@ end, need use if want start seeing output again in r session/
as @roman has pointed out though, better explicitly tell cat
output file. way want, , expect see in file, while still getting rest ouf output in r session.
question 2: answers question two. r (as far aware) not have direct file handling in python
. instead can use cat(..., file="outfile.txt")
inside loop.
Comments
Post a Comment