library("vegablock")
library("vegawidget")

Mike Bostock's blocks service is a great resource for sharing visualizations. Blocks (as they are called) are not persistently stored on the blocks website; they are taken from GitHub gists.

There are a few things you will have to set up to upload gists. First and foremost you will need a GitHub account. From this account, you will need to:

As you may know, it can be a frustrating experience to work with git from R - until it isn't. By this, I mean that you can spend a lot of energy to get things working the first time - then they "just work". If this is your situation, and you have not yet reached the "just work" stage, I highly recommend Jenny Bryan's e-book Happy Git and GitHub for the useR to help you get to the essence of git and GitHub issues. If it's a problem, chances are that Jenny has seen it and documented it!

As discussed above, you will need a GitHub PAT to use these functions. Unsurprisingly, there's a book section on GitHub PATs. In particular, I have run into issues with SSH credentialing - for which this troubleshooting section is invaluable.

Create a block

You can create a block using a vegaspec as an argument to vw_create_block(). Here we use the sample vegaspec from vegawidget, spec_mtcars:

vw_create_block(spec_mtcars)
message(
  "block url: https://bl.ocks.org/06a512525fbe7a96e1a2028e1871b61c\n",
  " gist url: https://gist.github.com/06a512525fbe7a96e1a2028e1871b61c"
)

Here's a link to the deployed block.

Arguments

As you can see in the reference for the vw_create_block() function, there are a lot of arguments. Here are some of the highlights:

Return value

We have two very similar functions vw_create_block() and vw_create_block_gistid(). They have the same arguments and they have the same side-effect; they both create the same gist that can be used as a block. They differ in the return value:

Depending on what you are doing, one function may be more-useful to you than the other.

gistid <- vw_create_block_gistid(spec_mtcars)
message(
  "block url: https://bl.ocks.org/650e59f6a81cf63b9004ae291fe90667\n",
  " gist url: https://gist.github.com/650e59f6a81cf63b9004ae291fe90667"
)

gistid <- list(
  endpoint = "https://api.github.com",
  id = "650e59f6a81cf63b9004ae291fe90667"
)
gistid

Retrieve a block

If a block has a JSON file, you can retrieve that JSON file and parse it into a vegaspec. For example, we can retrieve the vegaspec from the block that created above:

vw_retrieve_block(gistid$id)

The default behavior is to return the first file in the gist that has a .json extension. If you need to be more specific, you can use the file argument.



vegawidget/vegablock documentation built on June 19, 2019, 9:56 p.m.