carbonate

[This article was first published on Rstats on Reproducible Random Thoughts, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

carbon.js is the easiest way to create beautiful images of your source code, and carbonate creates an api in R to interact with it. The package gives a new simple way to share your script with the rest of the R community on posters, slides, Shiny apps and social media!

Installation

#remotes::install_github('yonicd/carbonate')
install.packages('carbonate')

Usage

Initialize new carbon object

library(carbonate)

The default code in the carbon object is taken from the clipboard.

x <- carbon$new()

But can also be defined inline. Code can be a character object of any length.

x <- carbon$new(readLines('DESCRIPTION'))

The code is kept in the object and can be changed at any time.

x$code
##  [1] "Package: carbonate"                                                                                 
##  [2] "Title: Interact with 'carbon.js'"                                                                   
##  [3] "Version: 0.1.0"                                                                                     
##  [4] "Authors@R: person(given  = \"Jonathan\","                                                           
##  [5] "                 family  = \"Sidi\","                                                               
##  [6] "                 email   = \"[email protected]\","                                                   
##  [7] "                 role    = c(\"aut\", \"cre\"),"                                                    
##  [8] "                 comment = c(ORCID = \"0000-0002-4222-1819\"))"                                     
##  [9] "Description: Create beautiful images of source code using 'carbon.js'<https://carbon.now.sh/about>."
## [10] "Depends: R (>= 3.2.0)"                                                                              
## [11] "License: MIT + file LICENSE"                                                                        
## [12] "Encoding: UTF-8"                                                                                    
## [13] "LazyData: true"                                                                                     
## [14] "RoxygenNote: 6.1.0"                                                                                 
## [15] "Imports: R6, clipr, magick, wdman, RSelenium, utils, httr, rtweet, yaml"                            
## [16] "Suggests: testthat, covr, knitr, rmarkdown"                                                         
## [17] "URL: https://github.com/yonicd/carbonate"                                                           
## [18] "BugReports: https://github.com/yonicd/carbonate/issues"                                             
## [19] "Roxygen: list(markdown = TRUE)"                                                                     
## [20] "VignetteBuilder: knitr"                                                                             
## [21] "Author: Jonathan Sidi [aut, cre] (<https://orcid.org/0000-0002-4222-1819>)"                         
## [22] "Maintainer: Jonathan Sidi <[email protected]>"                                                       
## [23] "Built: R 3.5.1; ; 2018-10-28 15:50:25 UTC; unix"

The main job of the package is to convert all the options set by the user into a URI that is sent to the carbon url page, where it is processed.

x$uri()
## [1] "https://carbon.now.sh/?bg=rgba(171%2C175%2C195%2C0.7)&t=panda-syntax&wt=none&l=r&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=11px&ph=14px&ln=false&fm=Fira%20Code&fs=14px&lh=133%25&si=false&es=1x&wm=false&ts=false&code=Package%253A%2520carbonate%250ATitle%253A%2520Interact%2520with%2520%27carbon.js%27%250AVersion%253A%25200.1.0%250AAuthors%2540R%253A%2520person(given%2520%2520%253D%2520%2522Jonathan%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520family%2520%2520%253D%2520%2522Sidi%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520email%2520%2520%2520%253D%2520%2522yonicd%2540gmail.com%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520role%2520%2520%2520%2520%253D%2520c(%2522aut%2522%252C%2520%2522cre%2522)%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520comment%2520%253D%2520c(ORCID%2520%253D%2520%25220000-0002-4222-1819%2522))%250ADescription%253A%2520Create%2520beautiful%2520images%2520of%2520source%2520code%2520using%2520%27carbon.js%27%253Chttps%253A%252F%252Fcarbon.now.sh%252Fabout%253E.%250ADepends%253A%2520R%2520(%253E%253D%25203.2.0)%250ALicense%253A%2520MIT%2520%252B%2520file%2520LICENSE%250AEncoding%253A%2520UTF-8%250ALazyData%253A%2520true%250ARoxygenNote%253A%25206.1.0%250AImports%253A%2520R6%252C%2520clipr%252C%2520magick%252C%2520wdman%252C%2520RSelenium%252C%2520utils%252C%2520httr%252C%2520rtweet%252C%2520yaml%250ASuggests%253A%2520testthat%252C%2520covr%252C%2520knitr%252C%2520rmarkdown%250AURL%253A%2520https%253A%252F%252Fgithub.com%252Fyonicd%252Fcarbonate%250ABugReports%253A%2520https%253A%252F%252Fgithub.com%252Fyonicd%252Fcarbonate%252Fissues%250ARoxygen%253A%2520list(markdown%2520%253D%2520TRUE)%250AVignetteBuilder%253A%2520knitr%250AAuthor%253A%2520Jonathan%2520Sidi%2520%255Baut%252C%2520cre%255D%2520(%253Chttps%253A%252F%252Forcid.org%252F0000-0002-4222-1819%253E)%250AMaintainer%253A%2520Jonathan%2520Sidi%2520%253Cyonicd%2540gmail.com%253E%250ABuilt%253A%2520R%25203.5.1;%2520;%25202018-10-28%252015%253A50%253A25%2520UTC;%2520unix"

Click the link to see the URI works.

Manipulate the carbon object

What make carbon.js really nice that you can control all the aesthetics of the image.

Carbon Variable Carbonate Variable Description Default
l language language r
bg palette layout pallete (vector with c(r,g,b,a) c(r=171,g=184,b=195,a=1)
t template layout template (listed in get_templates()) ‘seti’
wc add_window_control add/remove window controls (circles on top left corner) TRUE
wt window_control_theme change window control themes (listed in get_windows_control_themes()) ‘none’
ds add_drop_shadow add/remove dropshadow TRUE
dsyoff drop_shadow_offset_y shadow vertical offset (px) 20
dsblur drop_shadow_blur_radius shadow blur radius (px) 68
wa auto_adjust_width auto-audjust output width TRUE
pv padding_vertical vertical padding (px) 48
ph padding_horizontal horizontal padding (px) 32
ln add_line_number add/remove line numbers FALSE
fm font_family layout font family (listed in get_font_families()) ‘Hack’
fs font_size font size (px) 14
lh line_height_percent relative space between lines (percent) 133
si square_image output image is square FALSE
es relative_export_size image size in export relative to what is in the preview (1,2 or 4) 1
wm add_watermark add official carbon.js watermark FALSE
Default
x$carbonate(file = 'myfile.png')

Changing the template
x$template <-'cobalt'
x$carbonate(file = 'new_template.png')

Changing the font
x$font_family <-'IBM Plex Mono'
x$carbonate(file = 'new_font.png')

Sharing

Tinyurl

You can also put a tinyurl link as a watermark on the image produced that will open to the carbon.now.sh page that has the code in the image.

x$add_tinyurl <- TRUE
x$carbonate(file = 'tiny_url.png')

By default the watermark is placed on the bottom left corner.

If you just want the tinyurl link without the image to use in a tweet you can create it using

x$tiny()
## [1] "http://tinyurl.com/y7c64uyv"

Or you can put the link directly on your clipboard

x$tiny(clip = TRUE)
## [1] "http://tinyurl.com/y7c64uyv"
clipr::read_clip()
## [1] "http://tinyurl.com/y7c64uyv"

Twitter

Direct

You can also directly tweet the image. An automatic status is created with two options

  • Default
    • Created in R using the Carbonate ????
  • When add_tinyurl <- TRUE
  • Manual
    • Using tweet_status you can write your own status.
x <- carbonate::carbon$new()
x$tweet <- TRUE
x$carbonate()
Post process (Batch)

If you have images stored in x$carbons you can post them also in a tweet using.

# for multiple png attachments
x$rtweet(x$carbons,media_type = 'png') #using default status

# subsets of images
x$rtweet(status='These are images',x$carbons[c(1,3)],media_type = 'png')

# for gifs
x$rtweet(status='This is a gif', x$carbons,media_type = 'gif')

Reprex + Gist

Finally, thanks to a suggestion from Maelle Salmon to use carbonate with gist and reprex, now you can create and tweet out the reprex using carbon images linked to your Gist.

reprex::reprex(plot(1:10),
               outfile = "demo_carbonate",
               venue = "r")

gistr::gist_auth(reauth = TRUE)

gist_out <- gistr::gist_create('demo_carbonate_reprex.md',browse = FALSE)

x <- carbonate::carbon$new(readLines('demo_carbonate_reprex_rendered.R'))

x$carbonate(code = clipr::read_clip())

x$carbons <- append(x$carbons,
                    magick::image_read('demo_carbonate_reprex_files/figure-markdown_strict/reprex-body-1.png')
                    )
x$rtweet(
  media = x$carbons,
  status = glue::glue("gistr + carbonate #rstats
                      {emo::ji('link')} {tinyurl(gist_out$html_url)}"),
  media_format = 'png')

To leave a comment for the author, please follow the link and comment on their blog: Rstats on Reproducible Random Thoughts.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)