Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spread_values cannot create column named "x" #54

Open
jucor opened this issue Jan 19, 2017 · 4 comments
Open

spread_values cannot create column named "x" #54

jucor opened this issue Jan 19, 2017 · 4 comments

Comments

@jucor
Copy link

jucor commented Jan 19, 2017

Hi folks, thanks for a great package!

I noticed one bug though: spread_values crashes when creating a column named x:

'{ x: 3}' %>% spread_values(x = jnumber("x"))

returns:

Error in UseMethod("as.tbl_json") : 
  no applicable method for 'as.tbl_json' applied to an object of class "function"

whereas

'{ x: 3}' %>% spread_values(myx = jnumber("x"))

works seamlessly.

Is there aw workaround, please?
Thanks!

@colearendt
Copy link

The problem here is apparent if you look at the documentation: ?spread_values. The first parameter of spread_values is x. As a result, the pipe is getting overridden and you are, in essence calling spread_values(x=jnumber('x'),'{ x: 3}'). When using column name myx, the pipe is calling spread_values(x='{ x: 3}', myx=jnumber('x')).

To explicitly see the problem, try calling spread_values(x='{ x: 3}', x=jnumber('x')), as that is what you are trying to do. A simple workaround is to use dplyr::rename.

'{ "x": 3}' %>% 
spread_values(myx=jnumber('x')) %>% 
rename(x=myx)

@jucor
Copy link
Author

jucor commented May 11, 2017

Thanks @colearendt . I figured this was indeed the problem, and have worked around with such a rename [as my post suggested], but it feels very kludgy and un-clean, especially as "x" is quite a frequent field name. So I'll be more explicit in my feature request: is there a way to modify spread_values so that the name of its parameters is not mutually exclusive with the name of the fields?

@colearendt
Copy link

That makes sense. I think you may be interested in taking a look at the continued development of the package here. The changes have not yet made it to CRAN, but I have found the development version there to be superior to the CRAN version.

devtools::install_github('jeremystan/tidyjson')

In that version, spread_values is defined as spread_values(.x,...). Unfortunately, the first parameter still needs to be named, so that data can be passed to the function, but .x seems like a fair and much more reasonable solution, in my opinion. .x should be a less frequent name for a column, at least. The dplyr standard seems to be .tbl as the "column which cannot be named," so this is a similar (although different) convention.

@jucor
Copy link
Author

jucor commented May 12, 2017

That indeed sounds like a much less problematic name! Thanks for the type @colearendt .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants