From dput() documentation:
dput: Writes an ASCII text representation of an R object to a file or connection, or uses one to recreate the object.. And for the output of the function: "For dput, the first argument invisibly"
The first line in the output is dput() printing to the screen and the second is what returned by using the output of the function for sub-setting. You will see a similar behavior from deparse()
except deparse()
does not output anything to the screen, so there will be no first line:
mtcars[, deparse(unique(mtcars$gear))]
#[1] "c(4, 3, 5)"
A better example of dput() behavior is to use print() function :
print(dput(unique(mtcars$gear)))
#c(4, 3, 5) # this line is what dput() prints to the screen
#[1] 4 3 5 # this line comes as a result of print the return value of dput()
If you add a file name to dput()
function, you will see that the first line no longer printed and only second one (the return value of dput()
is printed to the screen:
print(dput(unique(mtcars$gear), file="xxx.txt"))
# [1] 4 3 5
It might be easier to understand how it works on the following example:
# Define a function that prints a message and returns the object using invisible() function (which suppress the output)
my.f <- function(x) {message(x); invisible(x)}
# This will print the message, but will not print the output of the function
my.f(777)
# 777
# This will print the message and force printing the return value
print(my.f(777))
# 777
# [1] 777
dput()
works in a similar way except instead of using message() function it either prints the content to the file (if the name provided) or sends the print statement to the standard output.