install.packages("figma")
Introduction
I’m very happy to announce the realease of figma
R package to CRAN! This package provides a R interface (or a web client/wrapper) to the Figma API. Below we have all the important links about this package:
This is the first release (or the first version) of the package, and for now, it have all the necessary functionality to get all data from a Figma file, and bring it to your R session. But in the future, the package will include more functionalities and endpoints. Please, if you can, test this package and give feedbacks or report bugs by sending Issues on the official repository.
Getting started
Fist of all, you need to install the package on your machine, and to do that, you can use this code:
Or, to install the development version directly from GitHub:
::install_github("pedropark99/figma") devtools
Now, to get the data of a Figma file through the Figma API, you have to collect two key variables about your file and your credentials. They are:
file_key
: The ID (or the “key”) that identifies your Figma file;token
: Your personal access token from the Figma platform;
To use specifically the figma::get_figma_page()
function, you will need to collect a third information, which is the node_id
, or the ID that identifies a canvas/page of your Figma file. I explain, in details, on how to collect these key variables on the main vignette package. For brevity reasons, lets assume in this blog post that you already have collected these variables.
Use get_figma_file()
to get your Figma file
Now that you have the key (or ID) that identifies your Figma file, and your personal token that identifies yourself, you can use figma::get_figma_file()
to get your Figma file:
library(figma)
<- "hch8YlkIrYbU3raDzjPvCz"
file_key # Insert your personal token:
<- "Your personal token ..."
token
# Returns a `httr::response` object:
<- figma::get_figma_file(
figma_file
file_key, token )
The functions from figma
package returns a httr::response
object by default. But you can use the .output_format
argument to fit the data into a more intuitive data strucuture. For example, a tibble::tibble
object:
# Returns a `tibble::tibble` object:
<- figma::get_figma_file(
figma_file
file_key, token,.output_format = "tibble"
)
print(figma_file)
#> # A tibble: 5 Ă— 7
#> canvas_id canvas_name canvas_type object_id object_name objec…¹ object_att…²
#> <chr> <chr> <chr> <chr> <chr> <chr> <list>
#> 1 0:1 Page 1 CANVAS 1:2 Background RECTAN… <named list>
#> 2 0:1 Page 1 CANVAS 5:2 Paragraph TEXT <named list>
#> 3 0:1 Page 1 CANVAS 5:3 Arrow VECTOR <named list>
#> 4 5:4 Page 2 CANVAS 5:5 BackgroundPa… RECTAN… <named list>
#> 5 5:4 Page 2 CANVAS 5:6 Texto da pág… TEXT <named list>
#> # … with abbreviated variable names ¹​object_type, ²​object_attributes
Each row in the above data.frame
is an object drawn in a canvas/page in your Figma file. Every object that you drawn in the canvas of your Figma file, can have a different type (like RECTANGLE, CIRCLE, TEXT, etc.). And objects of different types usually have different attributes.
For example, TEXT objects have a characters
attribute, which have the exact text that is written in the text box. In the other hand, RECTANGLE objects does not have such attribute.
All the attributes of each object are in the ​object_attributes
column, which is a <list>
column. Each row (or, if you prefer, each element) in this column is a list with all the attributes of the object described in the corresponding row. For example, to collect the characters
attribute of the first TEXT object described in figma_file
(which is in the 2nd row), you could do this:
$object_attributes[[2]][["characters"]] figma_file
[1] "Um texto qualquer, que nĂŁo sei se vai dar certo"
Or, you could the functions from dplyr
and purrr
packages to extract the characters
attribute from all TEXT objects of your Figma file, like this:
<- figma_file |>
text_objects ::filter(object_type == 'TEXT')
dplyr
'object_attributes']] |>
text_objects[[::map_chr('characters') purrr
[1] "Um texto qualquer, que não sei se vai dar certo" "Texto da página 2"
Other scopes of data
The figma::get_figma_file()
get the data of all objects drawn in all canvas/pages of a Figma file. But, maybe, you don’t need all data from your file. Maybe you just need the data from a section, or a specific part of your Figma file.
For these occasions, the figma
package offers the figma::get_figma_page()
(get data of objects drawn in a specific canvas/page of your file) and figma::get_document_info()
(get metadata about your Figma file) functions.
These functions work basically the same way as figma::get_figma_file()
, they have the same arguments (except figma::get_figma_page()
which have a extra argument node_id
). Check the documentation of these functions, and the main vignette of the package to see examples.