print vs. cat
https://stat.ethz.ch/pipermail/r-help/2004-September/056974.html
https://stackoverflow.com/questions/36699272/why-is-message-a-better-choice-than-print-in-r-for-writing-a-package
https://stackoverflow.com/questions/31843662/what-is-the-difference-between-cat-and-print
what is th diffence among “cat”,”print”and”format”
비슷한 기능의 함수들
- print()
- cat()
- message()
- warning()
- stop()
- alert()
print()
대부분은 print()를 많이 사용하게 될것이다. print(obj, “문장”, quote=F)
cat 과 달리, invisible(x)를 통해 parameter(변수)의 argument(값) 를 return할 수 있다.
(cat은 NULL을 return한다.)
> k <- 16
> print(k) #[1] 16
> cat(k) #16
# 2개 이상의 obj를 print하는 경우,
# paste()를 사용해야 원하는 대로 출력된다.
> print(str_c("k==>",k))
[1] "k==>16"
> attributes(k)$class <- "foo"
> print(k)
[1] 16
attr(,"class")
[1] "foo"
> cat('k==>', k, '\
')
k==> 16 > k <- 16
# print는 제일 앞에 obj, String만 콘솔 출력된다.
# 앞과 뒤의 형식이 다른 경우,
# 처음이 문자인 경우에는 첫 문자만 찍히지만
# 처음이 숫자이고 뒤가 문자인 경우에는 오류 발생
> print("k==>",k)
[1] "k==>"
> print(k,"k==>")
Error in print.default(k, "k==>") : invalid printing digits -2147483648
In addition: Warning message:
In print.default(k, "k==>") : NAs introduced by coercion
> cat(str_c('k==>', k), '\
')
k==>16 argument를 return할수 있는 기능이 있기 때문에, pipe와 함께 사용하면 유용할 때가 있다.
coefs <- lm(Sepal.Width ~ Petal.Length, iris) %>% print() %>% coefficients()
cat()
cat은 기본적인 type(logical, integer, real, complex, character) 을 바로 console에 찍을때 유용하다.
object 따위는 알지못한다.
cat(“문장”, 오브젝트, 수식, sep=””, paste())
> cat(" foo \
")
foo
> cat(' "foo"\
')
"foo"
> cat(" 'foo'\
")
'foo'
http://wolfpack.hannam.ac.kr/Stat_Notes/softwares/software_index.html
print()와 달리
- 개행(줄바꿈)이 없다.
- “” 출력 없다.
- 여러인자를(문자, 숫자..) 구별없이 화면에 잘 찍힌다.
sink("foobar.txt")
cat(' foo \
')
cat(' bar ')
sink()
#또는
cat('foo', 'bar', file="foobar.txt", sep="\
")
#또는
cat('foo\
', file="foobar.txt", append=T)
cat('bar\
', file="foobar.txt", append=T) 
foobar.txt 파일 생성