YEAR: 2018 COPYRIGHT HOLDER: Jonathan Sidi
Hi, thanks for contributing!
Please make sure you read our CONTRIBUTING guide.
Please fill the fields above:
vignettes/tests_and_coverage.Rmd
tests_and_coverage.RmdThis output is created by covrpage.
Coverage summary is created using the covr package.
| Object | Coverage (%) |
|---|---|
| carbonate | 48.58 |
| R/carbonate.R | 0.00 |
| R/selenium_functions.R | 0.00 |
| R/uri_functions.R | 52.63 |
| R/carbon.R | 64.29 |
| R/helpers.R | 70.09 |
| R/set_get_functions.R | 100.00 |
Unit Test summary is created using the testthat package.
| file | n | time | error | failed | skipped | warning | icon |
|---|---|---|---|---|---|---|---|
| test-set.R | 3 | 0.013 | 0 | 0 | 0 | 0 | |
| test-uri.R | 8 | 1.352 | 0 | 0 | 1 | 0 | 🔶 |
| test-yml.R | 6 | 0.017 | 0 | 0 | 0 | 0 |
| file | context | test | status | n | time | icon |
|---|---|---|---|---|---|---|
| test-set.R | set_ | set functions: set_template | PASS | 1 | 0.011 | |
| test-set.R | set_ | set functions: set_font_family | PASS | 1 | 0.001 | |
| test-set.R | set_ | set functions: set_windows_control_theme | PASS | 1 | 0.001 | |
| test-uri.R | uri | options: benchmark | PASS | 1 | 0.002 | |
| test-uri.R | uri | uri: benchmark | PASS | 1 | 0.002 | |
| test-uri.R | uri | uri: 200 | PASS | 1 | 0.721 | |
| test-uri.R | uri | encode: encode character | PASS | 1 | 0.002 | |
| test-uri.R | uri | encode: no encode character | PASS | 1 | 0.001 | |
| test-uri.R | uri | tiny: valid tiny | PASS | 1 | 0.620 | |
| test-uri.R | uri | tiny: clipboard | SKIPPED | 1 | 0.001 | 🔶 |
| test-uri.R | uri | bad template: error uri | PASS | 1 | 0.003 | |
| test-yml.R | yml | yaml fields: rgba | PASS | 1 | 0.006 | |
| test-yml.R | yml | yaml fields: template | PASS | 1 | 0.003 | |
| test-yml.R | yml | yaml fields: bad font family | PASS | 1 | 0.002 | |
| test-yml.R | yml | yaml fields: pv | PASS | 1 | 0.002 | |
| test-yml.R | yml | yaml fields: ph | PASS | 1 | 0.002 | |
| test-yml.R | yml | namesless palette: fill in palette | PASS | 1 | 0.002 |
| Failed | Warning | Skipped |
|---|---|---|
| 🛑 | ⚠️ | 🔶 |
| Field | Value |
|---|---|
| Version | R version 3.5.1 (2018-07-02) |
| Platform | x86_64-apple-darwin15.6.0 (64-bit) |
| Running | macOS 10.14.2 |
| Language | en_US |
| Timezone | America/Chicago |
| Package | Version |
|---|---|
| testthat | 2.0.0.9000 |
| covr | 3.2.0 |
| covrpage | 0.0.69 |
“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:

#remotes::install_github('yonicd/carbonate')
install.packages('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.
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.2.0"
#> [4] "Authors@R: "
#> [5] " person(given = \"Jonathan\","
#> [6] " family = \"Sidi\","
#> [7] " role = c(\"aut\", \"cre\"),"
#> [8] " email = \"yonicd@gmail.com\","
#> [9] " comment = c(ORCID = \"0000-0002-4222-1819\"))"
#> [10] "Description: Create beautiful images of source code using"
#> [11] " 'carbon.js'<https://carbon.now.sh/about>."
#> [12] "License: MIT + file LICENSE"
#> [13] "URL: https://github.com/yonicd/carbonate"
#> [14] "BugReports: https://github.com/yonicd/carbonate/issues"
#> [15] "Depends: "
#> [16] " R (>= 3.2.0)"
#> [17] "Imports: "
#> [18] " clipr,"
#> [19] " details,"
#> [20] " httpuv,"
#> [21] " httr,"
#> [22] " magick,"
#> [23] " R6,"
#> [24] " reprex,"
#> [25] " RSelenium,"
#> [26] " rtweet,"
#> [27] " utils,"
#> [28] " wdman,"
#> [29] " jsonlite,"
#> [30] " yaml"
#> [31] "Suggests: "
#> [32] " covr,"
#> [33] " knitr,"
#> [34] " rmarkdown,"
#> [35] " testthat"
#> [36] "VignetteBuilder: "
#> [37] " knitr"
#> [38] "RdMacros: "
#> [39] " details"
#> [40] "Encoding: UTF-8"
#> [41] "LazyData: true"
#> [42] "Roxygen: list(markdown = TRUE,old_usage = TRUE,r6 = FALSE)"
#> [43] "RoxygenNote: 7.2.1"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()
#> [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.2.0%250AAuthors%2540R%253A%2520%250A%2520%2520%2520%2520person(given%2520%253D%2520%2522Jonathan%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520family%2520%253D%2520%2522Sidi%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520role%2520%253D%2520c(%2522aut%2522%252C%2520%2522cre%2522)%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520email%2520%253D%2520%2522yonicd%2540gmail.com%2522%252C%250A%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%250A%2520%2520%2520%2520%27carbon.js%27%253Chttps%253A%252F%252Fcarbon.now.sh%252Fabout%253E.%250ALicense%253A%2520MIT%2520%252B%2520file%2520LICENSE%250AURL%253A%2520https%253A%252F%252Fgithub.com%252Fyonicd%252Fcarbonate%250ABugReports%253A%2520https%253A%252F%252Fgithub.com%252Fyonicd%252Fcarbonate%252Fissues%250ADepends%253A%2520%250A%2520%2520%2520%2520R%2520(%253E%253D%25203.2.0)%250AImports%253A%2520%250A%2520%2520%2520%2520clipr%252C%250A%2520%2520%2520%2520details%252C%250A%2520%2520%2520%2520httpuv%252C%250A%2520%2520%2520%2520httr%252C%250A%2520%2520%2520%2520magick%252C%250A%2520%2520%2520%2520R6%252C%250A%2520%2520%2520%2520reprex%252C%250A%2520%2520%2520%2520RSelenium%252C%250A%2520%2520%2520%2520rtweet%252C%250A%2520%2520%2520%2520utils%252C%250A%2520%2520%2520%2520wdman%252C%250A%2520%2520%2520%2520jsonlite%252C%250A%2520%2520%2520%2520yaml%250ASuggests%253A%2520%250A%2520%2520%2520%2520covr%252C%250A%2520%2520%2520%2520knitr%252C%250A%2520%2520%2520%2520rmarkdown%252C%250A%2520%2520%2520%2520testthat%250AVignetteBuilder%253A%2520%250A%2520%2520%2520%2520knitr%250ARdMacros%253A%2520%250A%2520%2520%2520%2520details%250AEncoding%253A%2520UTF-8%250ALazyData%253A%2520true%250ARoxygen%253A%2520list(markdown%2520%253D%2520TRUE%252Cold_usage%2520%253D%2520TRUE%252Cr6%2520%253D%2520FALSE)%250ARoxygenNote%253A%25207.2.1"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()
#> [1] "3024-night" "a11y-dark" "blackboard"
#> [4] "base16-dark" "base16-light" "cobalt"
#> [7] "dracula" "duotone-dark" "hopscotch"
#> [10] "lucario" "material" "monokai"
#> [13] "night-owl" "nord" "oceanic-next"
#> [16] "one-light" "one-dark" "panda-syntax"
#> [19] "paraiso-dark" "seti" "shades-of-purple"
#> [22] "solarized dark" "solarized light" "synthwave-84"
#> [25] "twilight" "verminal" "vscode"
#> [28] "yeti" "zenburn"
x$get_font_families()
#> [1] "Anonymous Pro" "Cascadia Code" "Droid Sans Mono"
#> [4] "Fantasque Sans Mono" "Fira Code" "Hack"
#> [7] "IBM Plex Mono" "Inconsolata" "JetBrains Mono"
#> [10] "Monoid" "Source Code Pro" "Space Mono"
#> [13] "Ubuntu Mono"
x$get_windows_control_themes()
#> [1] "none" "sharp" "bw"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$download_path <- file.path('README_files/download')This function depends on RSelenium
x$carbonate(file = 'myfile.png')
x$template <-'cobalt'
x$carbonate(file = 'new_template.png')
x$font_family <-'IBM Plex Mono'
x$carbonate(file = 'new_font.png')
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')
If you just want the tinyurl link without the image to use in a tweet you can create it using
x$tiny()
#> [1] "https://tinyurl.com/2kvjyzlx"Or you can put the link directly on your clipboard
x$tiny(clip = TRUE)
#> [1] "https://tinyurl.com/2kvjyzlx"
clipr::read_clip()
#> [1] "https://tinyurl.com/2kvjyzlx"You can also directly tweet the image. An automatic status is created with two options
add_tinyurl <- TRUE
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
x$carbons%>%
magick::image_scale('300')%>%
magick::image_append()
x$carbons%>%
magick::image_scale('300')%>%
magick::image_append(stack = TRUE)
opens the uri address in $uri in the browser window.
.browse(self, private)carbon self object
carbon private object
x <- carbon$new('x <- 1')
#> Set via '~/carbon.yml'
#> palette:
#> - 171.0
#> - 175.0
#> - 195.0
#> - 0.7
#> template: panda-syntax
#> font_family: Fira Code
#> padding_vertical: 11
#> padding_horizontal: 14
#> language: r
if(interactive())
x$browse()
The Carbon generator creates a new Carbon-object, which is the class containing
all the app logic. The class is based on the R6 OO-system and
is thus reference-based with methods and data attached to each object, in
contrast to the more well known S3 and S4 systems.
A new 'Carbon'-object is initialized using the new() method on the generator:
x <- carbon$new(code = clipr::read_clip()) |
Public Fields
Description of fields of the R6 object that can be set by the user can be found in the following page.
Interacting with Browser
| $carbonate | Using RSelenium fetch the carbon image output |
| $browse | open $uri in the browser |
Aesthetics
| $set_template | set $template |
| $get_templates | get a list of possible templates |
| $set_window_control_theme | set $windows_control_theme |
| $get_windows_control_themes | get a list of possible window control themes |
| $set_font_family | set $font_family |
| $get_font_families | get a list of possible fonts |
URI Building
| $uri | construct valid carbon.js uri |
| $options | return all current carbon options |
| $encode | URL encode a string for the $uri |
| $tiny | Create tinyurl from $uri |
| $rtweet | Send tweets containing media generated from carbon |
Webdriver Settings
| $chromeOptions | construct a chromeOptions object |
| $chrome_start | start a chrome session |
| $chrome_stop | stop a chrome session |
| $start | start a RSelenium session |
| $stop | stop a RSelenium session |
| $stop_all | stop all active RSelenium sessions |
| $get_port | Get active port |
| $set_port | Set new port |
Functions to open,close,set chrome webdriver
.chromeOptions(self, private)carbon self object
carbon private object
Fields that can be changed in the Carbon object
character, script to embbed, Default: NULL
numeric, layout pallete, Default: c(r=171,g=184,b=195,a=1)
character, layout template, Default: 'seti'
boolean, add/remove window controls (circles on top left corner), Default: TRUE
character, change window control themes, Default: 'none'
character, language, Default: 'r'
boolean, add/remove dropshadow, Default: TRUE
numeric, shadow vertical offset (px), Default: 20
numeric, shadow blur radius (px), Default: 68
boolean, auto-audjust output width, Default: TRUE
numeric, vertical padding (px), Default: 48
numeric, horizontal padding (px), Default: 32
boolean, add/remove line numbers, Default: FALSE
character, layout font family, Default: 'Hack'
numeric, font size (px), Default: 14
numeric, relative space between lines (percent), Default: 133
boolean, output image is square, Default: FALSE
numeric, image size in export relative to what is in the preview (1,2 or 4), Default: 1
boolean, add official carbon.js watermark, Default: FALSE
boolean, add tinyurl link to image directing to the carbon page that created the image, Default: FALSE
character, string with gravity value to place the annotate tinyurl address on image, Default: 'southeast'
boolean, tweet out the image using post_tweet
character, status to add to the tweet. If add_tinyurl is TRUE then a link to the url will be appended to the status automatically, Default: 'Created in R using the Carbonate (Package Icon)'
boolean, add timestamp to output filename, Default: FALSE
list, storage of rendered carbon images, Default: NULL
character, path to download the images, Default: tempdir()
character, arguments passed to chrome browser, Default: c('--disable-gpu', '--window-size=1280,800')
list, preferences passed to chrome browser, Default: list("profile.default_content_settings.popups" = 0L,"download.prompt_for_download" = FALSE,"download.default_directory" = tempdir())
RSelenium Driver object
Chrome Driver object from wdman
Functions to open,close,set gecko webdriver
.firefoxOptions(self, private)carbon self object
carbon private object
Functions to open,stop, stop_all RSelenium sessions
Main function of the package that invokes RSelenium to open a browser to the carbon.js uri, create an image and download the file.
.carbonate(self, private, file, path, code, rD, driver)carbon self object
carbon private object
character, name of file to save image as
character, path to save the image
character, lines of script to make carbon image from
RSelenium driver
character, select either 'firefox' or 'chrome' driver
image object
Script is passed to https://carbon.now.sh/ is downloaded to the tempdir() and appended to the list $carbons using RSelenium and Chrome.
if(interactive()){
x <- carbon$new('x <- 1')
x$carbonate()
}
encode uri string for carbon.js. Augmented version of than URLencode
.encode(self, private, URL, reserved, repeated)carbon self object
carbon private object
character, uri string to url encode
boolean, should ‘reserved’ characters be encoded?
boolean, should apparently already-encoded URLs be encoded again?
character
function that gets the available list of aesthetic values of fields
.get_windows_control_themes(self, private)
.get_font_families(self, private)
.get_templates(self, private)carbon self object
carbon private object
All functions |
|
|---|---|
open $uri to in browser window |
|
Carbon R6 class |
|
chrome webdriver functions |
|
Carbon object fields |
|
gecko webdriver functions |
|
|
|
RSelenium webdriver functions |
Carbonate script lines to a carbon image |
|
encode uri for carbon.js |
|
|
|
carbon get functions |
concatenate the carbon options to a string |
|
Creates a Reprex of selected code (or from input) renders it and opens or downloads the Carbon |
|
send tweets using carbonate outputs |
|
|
|
carbon set functions |
convert carbon uri to tinyurl |
|
convert uri to tinyurl |
|
URI constructor |
|
combine all the carbon options into a carbon.js valid string
.options(self, private, code)carbon self object
carbon private object
character, script to embbed into the uri
OUTPUT_DESCRIPTION
R/addins.R
reprex_to_carbon_browser.RdCreates a Reprex of selected code (or from input) renders it and opens or downloads the Carbon
reprex_to_carbon_browser(..., html_preview = FALSE, style = TRUE,
url_only = FALSE)
reprex_to_carbon_download(..., file = "carbon_reprex.png",
html_preview = FALSE, style = TRUE, url_only = FALSE)arguments passed to reprex::reprex
show also reprex html result? Default is false
should the code be styled? Default is True
if only the URL should be returned without opening the browser
the URL to the Carbon (invisible) or the filename (invisible)
reprex_to_carbon_download(): downloads the picture
wrapper for post_tweet to send tweets with media created using carbonate.
.rtweet(self, private, media, status = self$tweet_status,
media_format = c("png", "gif"), ...)carbon self object
carbon private object
magick-image object or path to image
character, status to attach to post, Default: self$tweet_status
character, type of media to tweet, Default: c('png','gif')
arguments to pass to post_tweet
outcome from rtweet
If multiple images are passed they can be either converted to a gif by setting media_format to 'gif' or attached each one to the post.
if (FALSE) {
x <- carbonate::carbon$new()
x$carbonate(code = readLines(system.file('DESCRIPTION',package='carbonate')))
x$carbonate(code = 'x+2')
# using default status value (x$tweet_status)
x$rtweet(system.file('figures/hex_black_small.png',package='carbonate'))
x$rtweet(status = 'these are two pngs',media = x$carbons,media_format='png')
x$rtweet(status = 'this is a gif', media = x$carbons,media_format='gif')
}
function that set the aesthetics of the carbon image output
.set_template(self, private, template)
.set_window_control_theme(self, private, theme)
.set_font_family(self, private, family)carbon self object
carbon private object
character, name of template to set $template to
character, theme to set $theme to
character, font family to set $font_family to
convert carbon uri to tinyurl with option to attach to clipboard.
.tiny(self, private, clip = FALSE)carbon self object
carbon private object
boolean, write tinyurl to clipboard, Default: FALSE
character
If clip is set to TRUE write_clip will put the tinyurl on the clipboard.
convert uri to tinyurl.
tinyurl(uri)character, the uri to convert
character
Function that is called in a carbon object to construct valid uri to send to browser
.uri(self, private, code)carbon self object
carbon private object
character, script to embbed into the uri
if(interactive()){
x <- carbon$new('x <- 1')
# populate from self$code
x$uri()
# enter manually
x$uri(code = 'x <- y + 3')
}