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

Switch from rjson to RJSONIO #41

Closed
ramnathv opened this issue Apr 24, 2013 · 6 comments
Closed

Switch from rjson to RJSONIO #41

ramnathv opened this issue Apr 24, 2013 · 6 comments
Labels

Comments

@ramnathv
Copy link
Owner

The use case is when I want to pass JS objects in my JSON payload. My current approach is to enclose JS objects between #! and !# tags, and use a regular expression to remove the quotes. Here is an example.

x = list(tickFormat = "#! function(d) {
  return d3.time.format('%Y-%m-%d')(new Date(d))
} !#"

I have a function toObj that destringifies contents within special markup tags.

toObj <- function(x){
  gsub('\"#!(.*)!#\"', "\\1", x)
}

So essentially, I would do something like toObj(toJSON(x)). There are three cases.

The first case is what I am doing now, using rjson::toJSON

cat(toObj(rjson::toJSON(x)))
{"tickFormat": function(d) {\n  return d3.time.format('%Y-%m-%d')(new Date(d))\n} }

The second case is using RJSONIO::toJSON, but not invoking the .escapeEscapes option.

cat(toObj(RJSONIO::toJSON(x)))
{
 "tickFormat":  function(d) {\n  return d3.time.format('%Y-%m-%d')(new Date(d))\n}  
}

If I use the .escapeEscapes = F option in RJSONIO::toJSON, I get exactly what I want.

{
 "tickFormat":  function(d) {
  return d3.time.format('%Y-%m-%d')(new Date(d))
}  
}

So, I think it makes sense to switch to RJSONIO.

@timelyportfolio
Copy link
Contributor

seems to work well; thanks

@ramnathv
Copy link
Owner Author

I am going to think this over before switching. The main advantage of this approach is that you can write the JS functions with new lines and the payload does not get screwed up. This would then allow us to pass arbitrary JS objects from R, not just functions.

@timelyportfolio
Copy link
Contributor

As I struggle with date handling in the various libraries, I am thinking this functionality might be necessary. I'll start a new issue to discuss the handling of dates in x.

@ramnathv
Copy link
Owner Author

Yes. There should be a way to do this within R rather than relying on d3 to do the formatting. I think you are the best person to be able to resolve this, as you probably work with lots of time series. Let me know if you want to test out some ideas/thoughts.

Thanks again for doing this.

@ramnathv
Copy link
Owner Author

I need to add a few tests to make sure that this feature works as I make more changes to rCharts. I am thinking a more organized test suite would do a lot of good, automating things. I wonder how tests work when it involves visually inspecting a page opened by the browser.

@ramnathv
Copy link
Owner Author

Done.

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

No branches or pull requests

2 participants