Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does dput write when diverting output using sink?

Tags:

r

I would like to see the result of dput in the console while redirecting output to a file using sink.

> sink(file = 'test.txt', split = TRUE)
> x <- 2^(1:4)
> x # test.txt now contains: [1] 2 4 8 16
[1]  2  4  8 16
> dput(x) # where does this return value go?
> dput(x, file = 'test.txt') # test.txt is overwritten with: c(2, 4, 6, 8)

Why does x print its value to the console (as expected), but dput(x) does not?

(I am using R 3.4.3 with RStudio Version 1.1.423 on Windows 7)

like image 602
C. Braun Avatar asked Mar 05 '18 17:03

C. Braun


People also ask

What does sink () do in R?

The sink() function in R drives the R output to the external connection. You can export the data in multiple forms such as text and CSV files. You can either print the data into the connection or directly export the entire data to it. After the data transfer, you can unlink the connection to terminate the file.

How do I redirect output to a file in R?

output() functions. One option is to use the sink() function. This redirects your output to a file, while commands and error messages continue to go to the console.

How do I export output from R console?

You can also save the entire R console screen within the GUI by clicking on "Save to File..." under the menu "File." This saves the commands and the output to a text file, exactly as you see them on the screen.


2 Answers

dput is actually writing the output where expected, but it's not writing it when expected. Running the following code shows that dput output remains pending until the next normal output:

sink(file = 'test.txt', split = TRUE)
x <- 2^(1:4)
x
dput(2*x,file="")
3*x

...gives a test.txt with:

[1]  2  4  8 16
c(4, 8, 16, 32)
[1]  6 12 24 48

Alternatively, running the sink() function to close the file will also force the pending output (but will close the connection).

sink(file = 'test.txt', split = TRUE)
x <- 2^(1:4)
x
dput(2*x,file="")
sink()
like image 68
jpd527 Avatar answered Oct 04 '22 02:10

jpd527


It only goes where you tell it to. That's what "returns invisibly" means. You can change this behavior though...

sink(file = "text.txt", split = TRUE)
x <- 2^(1:4)
x
# [1]  2  4  8 16
dput(x)
# returned invisibly, which is why you don't see it.
# but you can assign it to a variable

my_var <- dput(x)
my_var
# [1]  2  4  8 16

# But if you want to make it noisy, wrap it in ()
# When assigning it to a variable
(my_var <- dput(x))
# [1]  2  4  8 16

# This works even without assigning it to a variable 
(dput(x))
# [1]  2  4  8 16


# do this with your dput to sink command
(dput(x, file = "test.txt"))
# [1]  2  4  8 16

So, in summary, if you want to see something happen in the console that is happening behind the scenes (whether as the result of an invisible(<return statement>) or an assignment), wrap it in an outer layer of ()

like image 43
De Novo Avatar answered Oct 04 '22 02:10

De Novo