knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/" ) library(magick)
"carbon.js is the easiest way to create beautiful images of your source code."
This package uses an R6
api to interact with carbon.js and create directly from the console carbon images.
Like the image below:
library(carbonate) x <- carbon$new(readLines('DESCRIPTION')) x$carbonate(file = 'myfile.png',path = 'man/figures')
x$carbons
#remotes::install_github('yonicd/carbonate') install.packages('carbonate')
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
The main job of the R6 object is to convert all the options into a uri that is sent to the carbon url page, where it is processed.
x$uri()
Click the link to see the URI works.
Aesthetics of the carbon object that can be manipulated
|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|
x$get_templates()
x$get_font_families()
x$get_windows_control_themes()
Users can use a file called carbon.yml
and place it in their home directory (~
) to override the packages defaults in order to create a session peristent carbon template they like. The config file controls all the public carbon object fields. The following is an example of such a configuration file.
These fields will set
g
and a
fields to 175 and 0.7 respectivelypanda-syntax
Fira Code
palette: - g: 175.0 - a: 0.7 template: panda-syntax font_family: Fira Code padding_vertical: 11 padding_horizontal: 14
x$browse()
x$download_path <- file.path('README_files/download')
This function depends on RSelenium
x$carbonate(file = 'myfile.png')
x$carbons
Sys.sleep(2)
x$template <-'cobalt' x$carbonate(file = 'new_template.png')
Sys.sleep(2)
x$font_family <-'IBM Plex Mono' x$carbonate(file = 'new_font.png')
Sys.sleep(2)
Closing all instances of open browsers used by RSelenium.
x$stop_all()
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.
Sys.sleep(2)
x$add_tinyurl <- TRUE x$carbonate(file = 'tiny_url.png')
If you just want the tinyurl link without the image to use in a tweet you can create it using
x$tiny()
Or you can put the link directly on your clipboard
x$tiny(clip = TRUE) clipr::read_clip()
Twitter
You can also directly tweet the image. An automatic status is created with two options
Default
r 'Created in R using the Carbonate \U0001f4e6'
When add_tinyurl <- TRUE
r sprintf('%s\nCheck out this script at \U0001f517 %s', 'Created in R using the Carbonate \U0001f4e6', x$tiny())
Manual
tweet_status
you can write your own status. x <- carbonate::carbon$new() x$tweet <- TRUE x$carbonate()
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')
All carbon outputs are collected and saved in the list x$carbons
Combining
x$carbons%>% magick::image_scale('300')%>% magick::image_append() x$carbons%>% magick::image_scale('300')%>% magick::image_append(stack = TRUE)
GIFs
x$carbons%>% magick::image_animate(fps = 1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.