4

Not high priority, but this is an operation I carry out frequently. So, I'd like to know if there's any way to suppress the output of the expression inside dput.

library(data.table)
mtcars <- setDT(copy(mtcars))
mtcars[, dput(unique(gear))]
# c(4, 3, 5) <---- **only** this line was the expected output
# [1] 4 3 5 <---- Why is this line also returned?
dput(unique(mtcars$gear))
# c(4, 3, 5) <---- Expected output

I know dput(unique(mtcars$gear)) works, so not sure why it doesn't work as expected in j.

Thanks!

Ameya
  • 1,712
  • 1
  • 14
  • 29
  • I don't understand what you're asking. You asked `dput` to output the (vector of) unique values of `mtcars$gear`, so that's exactly what it did. What exactly do you want to have output? Are you perhaps trying to group-by `gear`, then output something else? Like `mtcars[..., dput(something_else?), by=gear]` – smci May 07 '18 at 04:13
  • @smci The output contains two lines (commented out above in the question). I was expecting **only** the first line. The second line seemed unnecessary. – Ameya May 07 '18 at 04:24
  • Edited title to *"display both the unevaluated and evaluated expression?"* – smci May 07 '18 at 04:46
  • 1
    `mtcars[, dput(unique(gear))]` yields the output of `dput(unique(gear))` **and** `unique(gear)`. I was expecting only the output of the former expression. Is that what you meant by evaluated and unevaluated? – Ameya May 07 '18 at 04:49
  • Yes I think those are both the unevaluated and evaluated dput expressions? (If not, whenever you figure out what the second line is, please edit the title) – smci May 07 '18 at 04:51
  • 3
    Regarding "any way to suppress the output of the expression inside `dput`", yes, `dput(mtcars[, unique(gear)])` or `invisible(mtcars[, dput(unique(gear))])` or `with(mtcars, dput(unique(gear)))` or with the magrittr package, `mtcars[, unique(gear)] %>% dput` – Frank May 07 '18 at 13:34

1 Answers1

3

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.

Katia
  • 3,784
  • 1
  • 14
  • 27
  • But I indeed am looking for the `dput` output. Just not the output of the expression inside it. In this case, `dput(unique(mtcars$gear))` is what I am looking for. – Ameya May 07 '18 at 03:48
  • OK. But what would you like to see for subsetting . Do you want to output 3rd, 4th and 5th column? – Katia May 07 '18 at 03:54
  • I'm not looking to output any columns, I want to know how many unique values there are in a given column, and to recreate a vector of those unique values. In this case, `c(4, 3, 5)` – Ameya May 07 '18 at 03:56
  • Then length(unique(mtcars$gear)) will give you the number of unique values and unique(mtcars$gear) will give you the vector itself. dput() function is used to write the output to a standard output or file. It is not necessary here. – Katia May 07 '18 at 03:58
  • That explains it. However, it still doesn't make sense to `print` the output instead of just returning the output as one may expect. – Ameya May 07 '18 at 05:54
  • 2
    "the second is what returned by using the output of the function for sub-setting" -- it's not really subsetting, just evaluating. I think it's because `j` doesn't play nice with invisible output: `mtcars[, invisible(42)]` and `(invisible(42))` and `invisible(42)[]` similarly print. Also, fwiw, I would quote the "Value" section of the dput docs. – Frank May 07 '18 at 11:27