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

Add a way to provide d3 script as text rather than as a file path, for quick development/testing #51

Open
daattali opened this issue Jan 12, 2019 · 4 comments

Comments

@daattali
Copy link

daattali commented Jan 12, 2019

Many functions that require a file input also accept some sort of a text argument. For real production use this won't be recommended, but for making quick little scripts, for reproducibility (sharing a single code chunk online for example), for doing testing, for many little tasks it's useful to be able to do this.

I'm envisioning something like this:

my_r3_code <- "
// !preview r2d3 data=c(0.3, 0.6, 3, 0.95, 0.40, 0.20), width=600

var barHeight = Math.ceil(height / data.length);

svg.selectAll('rect')
  .data(data)
  .enter().append('rect')
    .attr('width', function(d) { return d * width; })
    .attr('height', barHeight)
    .attr('y', function(d, i) { return i * barHeight; })
    .attr('fill', 'steelblue');
"
r2d3(data = c(0.3, 0.6, 0.8, 0.95, 0.40), text = my_r3_code)

Now this can be ran and/or shared as one piece of code rather than mandating the creation of a separate file and saving it to disk and then coordinating finding its path.

@edgararuiz-zz
Copy link
Contributor

As a workaround, I save the script as a file using writeLines() and then read the resulting file in r2d3()

@javierluraschi
Copy link
Contributor

@daattali this is already supported, am I missing something? The only change from the code above is using script instead of text:

my_r3_code <- "
// !preview r2d3 data=c(0.3, 0.6, 3, 0.95, 0.40, 0.20), width=600

var barHeight = Math.ceil(height / data.length);

svg.selectAll('rect')
  .data(data)
  .enter().append('rect')
    .attr('width', function(d) { return d * width; })
    .attr('height', barHeight)
    .attr('y', function(d, i) { return i * barHeight; })
    .attr('fill', 'steelblue');
"
r2d3(data = c(0.3, 0.6, 0.8, 0.95, 0.40), script = my_r3_code)

@daattali
Copy link
Author

If that's supposed to work, then yes, that's exactly what I was talking about!

But when I try to run that (Windows 7, R 3.5.2, r2d3 v0.2.3) I get an error

Error in dirname(script) : path too long

I didn't even think of trying that though because the documentation didn't sound like text rather than a script file would work. If this does work, it would be nice if the function documentation made that note.

@daattali
Copy link
Author

Update: I just installed the package from github for the latest dev version and I'm still seeing the same error. @javierluraschi are you sure the code you provided works for you? If so, something strange is going on.

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

3 participants