0

This question is related to this question and this question.

I need to assign a default to the ... argument in a function. I have successfully been able to to use the default package to accomplish this for specific arguments. For instance, lets say I want to allow toJSON from the jsonlite package to show more than four digits. The default is 4, but I want to show 10.

library(jsonlite)
library(default)

df <- data.frame(x = 2:5, 
                 y = 2:5 / pi)
df
#>   x         y
#> 1 2 0.6366198
#> 2 3 0.9549297
#> 3 4 1.2732395
#> 4 5 1.5915494

# show as JSON - defaults to four digits
toJSON(df)
#> [{"x":2,"y":0.6366},{"x":3,"y":0.9549},{"x":4,"y":1.2732},{"x":5,"y":1.5915}]

# use default pacakge to change to 10
default(toJSON) <- list(digits = 10)

toJSON(df)
#> [{"x":2,"y":0.63661977237},{"x":3,"y":0.95492965855},{"x":4,"y":1.2732395447},{"x":5,"y":1.5915494309}]

There is another function called stream_out which uses toJSON but only uses the digits argument in ....

> stream_out(df)
{"x":2,"y":0.63662}
{"x":3,"y":0.95493}
{"x":4,"y":1.27324}
{"x":5,"y":1.59155}
Complete! Processed total of 4 rows.
> 
> stream_out(df, digits = 10)
{"x":2,"y":0.63661977237}
{"x":3,"y":0.95492965855}
{"x":4,"y":1.2732395447}
{"x":5,"y":1.5915494309}
Complete! Processed total of 4 rows.

So even though I have changed the digits in toJSON, it isn't passed to the ... in stream_out. I cannot change this in the same manner as with toJSON.

> default(stream_out) <- list(digits = 10)
Error: 'digits' is not an argument of this function

This is not strictly a jsonlite question, but that is my use case here. I need to somehow change the ... argument of the stream_out function so that any time it is used, 10 digits are returned, rather than 4. However, any examples that show how to change defaults of ... arguments could probably be used to get to what I need.

Thanks!

Nick Criswell
  • 1,733
  • 2
  • 16
  • 32
  • 1
    tried coming up with a solution but can't help you. actually i think the default you want to change is [here](https://github.com/cran/jsonlite/blob/master/R/asJSON.numeric.R#L1) which would explain why the default you get from `stream_out` is 5 digits not 4 as in `toJSON`. typically one can [edit even internal functions](https://stackoverflow.com/questions/38388570/how-to-modify-unexported-object-in-a-package) but again not working for this situation for some reason. also `formals(toJSON)$digits <- 10` is sufficient to change defaults, not sure why we needed a package for that – rawr Nov 15 '21 at 17:23
  • @rawr, thank you. I will tinker with the approach noted in your link. As an FYI, I realized this problem when investigating [this question](https://stackoverflow.com/questions/69948248/r-bigrquery-losing-decimals-in-writing) I previously posted around using `R` with Google BigQuery. Sounds like it is a known issue in [`bigrquery`](https://github.com/r-dbi/bigrquery/issues/320). Thanks for some direction on this. – Nick Criswell Nov 15 '21 at 18:11

0 Answers0