Skip to content

I modify my question in "textconnection output"

2 messages · Michael S, Gabor Grothendieck

#
dear ALL-R-helper:
I modify my question in "textconnection output":
I wrote one function in Rgui:
output <- function(y){
	x <- textConnection("foo","w")
	sink(x)
	a <-5
	b <-6
	z <-a*b
	z
	e <-"spss"
	h <-c(1,2,3)
	ls()
	r<-c("s","p","s","s")
	p<-list(1:10)
	p
	y <- foo
	sink()
     	close(x)
	return(y)
}

I want to get resulte is :
[1] "[1] 30"
[2] " [1] \"a\"      \"b\"      \"c\"      \"d\"      \"e\"      \"f\"      
\"foo\"    \"g\"      \"g.p\"    \"h\"      \"interp\" \"m\"      
\"mytest\""
[3] "[14] \"output\" \"p\"      \"r\"      \"var1\"   \"var2\"   \"x\"      
\"y\"      \"z\"     "
[4] "[[1]]"
[5] " [1]  1  2  3  4  5  6  7  8  9 10"
[6] ""

when I copy the command line within the function ,and paste to RGui,result 
is ok .but when I use the output function ,">y" show value of y object.I got 
result "character(0)"

seem to me : I didn't get  value of y within function

thanks
#
Michael S <michael_shen <at> hotmail.com> writes:

: 
: dear ALL-R-helper:
: I modify my question in "textconnection output":
: I wrote one function in Rgui:
: output <- function(y){
: 	x <- textConnection("foo","w")
: 	sink(x)
: 	a <-5
: 	b <-6
: 	z <-a*b
: 	z
: 	e <-"spss"
: 	h <-c(1,2,3)
: 	ls()
: 	r<-c("s","p","s","s")
: 	p<-list(1:10)
: 	p
: 	y <- foo
: 	sink()
:      	close(x)
: 	return(y)
: }
: 
: I want to get resulte is :
: >y
: 
: [1] "[1] 30"
: [2] " [1] \"a\"      \"b\"      \"c\"      \"d\"      \"e\"      \"f\"      
: \"foo\"    \"g\"      \"g.p\"    \"h\"      \"interp\" \"m\"      
: \"mytest\""
: [3] "[14] \"output\" \"p\"      \"r\"      \"var1\"   \"var2\"   \"x\"      
: \"y\"      \"z\"     "
: [4] "[[1]]"
: [5] " [1]  1  2  3  4  5  6  7  8  9 10"
: [6] ""
: 
: when I copy the command line within the function ,and paste to RGui,result 
: is ok .but when I use the output function ,">y" show value of y object.I got 
: result "character(0)"
: 
: seem to me : I didn't get  value of y within function

You have not defined foo within your function.  If you have
a foo outside your function then that is being assigned to
y.  If you haven't a foo anywhere then you should have received 
an error.

You might want to look at ?capture.output  

y <- capture.output({
  x <- 1
  print(x)
})