### Get Example Data Path and URL Source: https://vegawidget.github.io/vegawidget/articles/articles/render-vegawidget.html These lines retrieve the local file path for example data included with the package and define a remote URL for the same data, useful for demonstrating data loading. ```R path_local <- system.file("example-data", package = "vegawidget") url_remote <- "https://vega.github.io/vega-datasets/data" ``` -------------------------------- ### Install vegawidget from GitHub Source: https://vegawidget.github.io/vegawidget/dev/index.html Install the development version of the vegawidget package directly from its GitHub repository. ```R remotes::install_github("vegawidget/vegawidget") ``` -------------------------------- ### vw_shiny_demo Source: https://vegawidget.github.io/vegawidget/reference/vw_shiny_demo.html Runs Shiny demonstration applications. If no example is specified, it lists all available examples. Additional arguments can be passed to `shiny::runApp()`. ```APIDOC ## vw_shiny_demo ### Description Runs Shiny demonstration applications. If no example is specified, it lists all available examples. Additional arguments can be passed to `shiny::runApp()`. ### Function Signature `vw_shiny_demo(example = NULL, ...)` ### Parameters #### Arguments - **example** (`character`) - Name of the example to run. If `NULL` (default), prints out a list of available examples. - **...** - Additional arguments passed to `shiny::runApp()`. ### Value Invisible `NULL`. Called for side-effects. ### Examples ```R vw_shiny_demo() # returns available examples #> Available examples: "data-set-gapminder", "data-set-get", "data-set-montecarlo", "data-set-swap", "data-set-swap-run", "event-get", "signal-set-get" # Run only in interactive R sessions if (interactive()) { vw_shiny_demo("data-set-get") } ``` ``` -------------------------------- ### Install vegawidget Package Source: https://vegawidget.github.io/vegawidget/dev/index.html Installs the development version of the vegawidget package from GitHub. Ensure devtools is installed first. ```r # install.packages("devtools") devtools::install_github("vegawidget/vegawidget") ``` -------------------------------- ### Example: Check Vega-Lite and Vega versions Source: https://vegawidget.github.io/vegawidget/reference/vw_to_vega.html Demonstrates how to check the library and version of a Vega-Lite specification and then convert it to Vega and check its version. ```R vw_spec_version(spec_mtcars) #> $library #> [1] "vega_lite" #> #> $version #> [1] "5" #> vw_spec_version(vw_to_vega(spec_mtcars)) #> $library #> [1] "vega" #> #> $version #> [1] "5" #> ``` -------------------------------- ### Example: Set Renderer to SVG Source: https://vegawidget.github.io/vegawidget/dev/reference/vega_embed.html Demonstrates how to use `vega_embed` to specify 'svg' as the rendering engine for the embedded visualization. This example also shows the default `defaultStyle` behavior. ```R vega_embed(renderer = "svg") #> $renderer #> [1] "svg" #> #> $defaultStyle #> [1] TRUE #> ``` -------------------------------- ### Example: All Included Vega Versions Output Source: https://vegawidget.github.io/vegawidget/reference/vega_version.html Demonstrates the data frame output from `vega_version_all()`, listing all supported widget and library versions. ```R vega_version_all() #> widget vega_lite vega vega_embed #> 1 vl5 5.16.3 5.24.0 6.22.2 #> 2 vl4 4.17.0 5.17.0 6.12.2 ``` -------------------------------- ### Documenting Parameters with Roxygen2 Source: https://vegawidget.github.io/vegawidget/dev/CONTRIBUTING.html Example of documenting parameters and return values using Roxygen2 syntax, specifying expected classes and descriptions. ```r #' @param spec An object to be coerced to `vegaspec`, a Vega/Vega-Lite specification #' @param width `integer`, sets the view width in pixels #' #' @return `logical` indicating success ``` -------------------------------- ### Example: Default Vega Version Output Source: https://vegawidget.github.io/vegawidget/reference/vega_version.html Illustrates the output structure for `vega_version()`, showing locked status and specific library versions. ```R vega_version() #> $widget #> [1] "vl5" #> #> $vega_lite #> [1] "5.16.3" #> #> $vega #> [1] "5.24.0" #> #> $vega_embed #> [1] "6.22.2" #> #> $is_locked #> [1] TRUE #> ``` -------------------------------- ### Install vegawidget from CRAN Source: https://vegawidget.github.io/vegawidget/dev/index.html Install the stable version of the vegawidget package from the Comprehensive R Archive Network (CRAN). ```R install.packages("vegawidget") ``` -------------------------------- ### Example: Available Vega Versions Output Source: https://vegawidget.github.io/vegawidget/reference/vega_version.html Shows the data frame output from `vega_version_available()`, indicating the currently available widget and library versions. ```R vega_version_available() #> widget vega_lite vega vega_embed #> 1 vl5 5.16.3 5.24.0 6.22.2 ``` -------------------------------- ### Examples of ISO-8601 Date-Time Strings Source: https://vegawidget.github.io/vegawidget/articles/articles/dates-times.html Illustrates strings that conform to the ISO-8601 standard, which Vega parses as UTC by default. ```text 2001-01-01T19:34:05Z 2001-01-01T19:34:05+05:00 2001-01-01 ``` -------------------------------- ### Load Required Libraries Source: https://vegawidget.github.io/vegawidget/articles/articles/image.html Load the necessary libraries for using vegawidget and related functionalities. Ensure these packages are installed before running. ```r library("conflicted") library("vegawidget") library("htmltools") library("glue") ``` -------------------------------- ### Verify Vega Specification Version Source: https://vegawidget.github.io/vegawidget/dev/reference/vw_to_vega.html This example demonstrates how to check the library and version of a Vega-Lite specification and then verify the version after converting it to a Vega specification using `vw_to_vega`. ```R vw_spec_version(spec_mtcars) #> $library #> [1] "vega_lite" #> #> $version #> [1] "5" #> vw_spec_version(vw_to_vega(spec_mtcars)) #> $library #> [1] "vega" #> #> $version #> [1] "5" #> ``` -------------------------------- ### Example: Major Vega Version Output Source: https://vegawidget.github.io/vegawidget/reference/vega_version.html Shows the output of `vega_version(major = TRUE)`, returning only the major version tags. ```R vega_version(major = TRUE) #> $widget #> [1] "vl5" #> #> $vega_lite #> [1] "5" #> #> $vega #> [1] "5" #> #> $vega_embed #> [1] "6" #> #> $is_locked #> [1] TRUE #> ``` -------------------------------- ### Basic Image Export Examples Source: https://vegawidget.github.io/vegawidget/dev/reference/image.html Demonstrates basic usage of image export functions with a vegaspec or vegawidget. Includes writing to temporary files. ```R # call any of these functions using either a vegaspec or a vegawidget svg <- vw_to_svg(vegawidget(spec_mtcars)) bmp <- vw_to_bitmap(spec_mtcars) vw_write_png(spec_mtcars, file.path(tempdir(), "temp.png")) vw_write_svg(spec_mtcars, file.path(tempdir(), "temp.svg")) ``` -------------------------------- ### vw_to_vega Function Source: https://vegawidget.github.io/vegawidget/dev/reference/vw_to_vega.html Compiles a Vega-Lite specification into a Vega specification. Requires the V8 package to be installed. ```APIDOC ## vw_to_vega(spec) ### Description Compiles a Vega-Lite specification into a Vega specification. ### Arguments * **spec** (vegaspec) - An object to be coerced to `vegaspec`, a Vega/Vega-Lite specification. ### Value S3 object of class `vegaspec_vega` and `vegaspec`. ### Examples ```R # Assuming spec_mtcars is a Vega-Lite specification object # vw_spec_version(spec_mtcars) # vw_spec_version(vw_to_vega(spec_mtcars)) ``` ``` -------------------------------- ### Documenting Parameters with Roxygen2 Source: https://vegawidget.github.io/vegawidget/CONTRIBUTING.html Example of documenting function parameters and return values using Roxygen2 syntax. Ensure the class of the argument or return value is specified before the description. ```r ப்புக @param spec An object to be coerced to `vegaspec`, a Vega/Vega-Lite specification ப்புக @param width `integer`, sets the view width in pixels ப்புக @return `logical` indicating success ``` -------------------------------- ### Examples of Non-ISO-8601 Date-Time Strings Source: https://vegawidget.github.io/vegawidget/articles/articles/dates-times.html Shows strings that do not conform to ISO-8601. Vega parses these as local time by default. ```text 2001-01-01 19:34:05 Jan-01-2001 19:34:05 2001/01/01 ``` -------------------------------- ### Run Shiny Demo App Source: https://vegawidget.github.io/vegawidget/dev/reference/vw_shiny_demo.html Call `vw_shiny_demo()` without arguments to list available Shiny examples. This function is intended for use in interactive R sessions. ```R vw_shiny_demo() # returns available examples #> Available examples: "data-set-gapminder", "data-set-get", "data-set-montecarlo", "data-set-swap", "data-set-swap-run", "event-get", "signal-set-get" ``` ```R if (interactive()) { vw_shiny_demo("data-set-get") } ``` -------------------------------- ### Create Vega-Lite Spec Manually Source: https://vegawidget.github.io/vegawidget/dev/reference/vega_schema.html Example of manually creating a Vega-Lite specification by setting the `$schema` property using `vega_schema()` and converting it to a vegaspec. ```R spec <- list( `$schema` = vega_schema(), width = 300, height = 300 # and so on ) %>% as_vegaspec() ``` -------------------------------- ### Examine Vega Specification Source: https://vegawidget.github.io/vegawidget/articles/articles/vegaspec.html Use this function to examine a Vega specification in code mode. Ensure the 'vegawidget' library is installed. ```R vw_examine(spec_category_vega, mode = "code") ``` -------------------------------- ### Initialize Vega Widget with Element ID Source: https://vegawidget.github.io/vegawidget/articles/articles/javascript.html Use `vegawidget()` with `elementId` to specify the container for the chart. This is the initial setup step before adding JavaScript interactions. ```javascript vegawidget(spec_table, elementId = "streaming-demo") ``` -------------------------------- ### Vega-Lite Specification with Autosize Source: https://vegawidget.github.io/vegawidget/dev/reference/vw_examine.html This is an example of a Vega-Lite specification that has been autosized. It includes data, mark, and encoding properties, with explicit width and height set to 300. ```JSON { "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "width": 300, "height": 300, "description": "An mtcars example.", "data": { "values": [ { "mpg": 21, "cyl": 6, "disp": 160, "hp": 110, "drat": 3.9, "wt": 2.62, "qsec": 16.46, "vs": 0, "am": 1, "gear": 4, "carb": 4, "_row": "Mazda RX4" }, { "mpg": 21, "cyl": 6, "disp": 160, "hp": 110, "drat": 3.9, "wt": 2.875, "qsec": 17.02, "vs": 0, "am": 1, "gear": 4, "carb": 4, "_row": "Mazda RX4 Wag" }, { "mpg": 22.8, "cyl": 4, "disp": 108, "hp": 93, "drat": 3.85, "wt": 2.32, "qsec": 18.61, "vs": 1, "am": 1, "gear": 4, "carb": 1, "_row": "Datsun 710" }, { "mpg": 21.4, "cyl": 6, "disp": 258, "hp": 110, "drat": 3.08, "wt": 3.215, "qsec": 19.44, "vs": 1, "am": 0, "gear": 3, "carb": 1, "_row": "Hornet 4 Drive" }, { "mpg": 18.7, "cyl": 8, "disp": 360, "hp": 175, "drat": 3.15, "wt": 3.44, "qsec": 17.02, "vs": 0, "am": 0, "gear": 3, "carb": 2, "_row": "Hornet Sportabout" }, { "mpg": 18.1, "cyl": 6, "disp": 225, "hp": 105, "drat": 2.76, "wt": 3.46, "qsec": 20.22, "vs": 1, "am": 0, "gear": 3, "carb": 1, "_row": "Valiant" }, { "mpg": 14.3, "cyl": 8, "disp": 360, "hp": 245, "drat": 3.21, "wt": 3.57, "qsec": 15.84, "vs": 0, "am": 0, "gear": 3, "carb": 4, "_row": "Duster 360" } ] }, "mark": "point", "encoding": { "x": { "field": "wt", "type": "quantitative" }, "y": { "field": "mpg", "type": "quantitative" }, "color": { "field": "cyl", "type": "nominal" } } } ``` -------------------------------- ### Get Bitmap Dimensions (Scale 1) Source: https://vegawidget.github.io/vegawidget/articles/articles/image.html Converts a Vega-Lite specification to a bitmap array using `vw_to_bitmap` with the default scale (1) and displays its dimensions. ```R vw_to_bitmap(spec_mtcars) %>% dim() ``` ```R ## [1] 347 392 4 ``` -------------------------------- ### Get Event Data in Shiny Source: https://vegawidget.github.io/vegawidget/articles/articles/shiny.html Retrieve the item or datum associated with a Vega-Lite event in a Shiny application. Use 'item' to get the event item or 'datum' to get the underlying data. ```R vw_shiny_get_event(..., body_value = "item") ``` ```R vw_shiny_get_event(..., body_value = "return item;") ``` -------------------------------- ### Get Event Datum in Shiny Source: https://vegawidget.github.io/vegawidget/dev/articles/articles/shiny.html Retrieve the datum associated with a Vega-Lite event. Use 'item' to get the event item itself, or 'datum' to get the data behind the mark. Handles cases where item or datum might be undefined. ```javascript return item; ``` ```javascript if (item === null || item === undefined || item.datum === undefined) { return null; } // returns the datum behind the mark associated with the event return item.datum; ``` -------------------------------- ### Get Default Vega Version Source: https://vegawidget.github.io/vegawidget/dev/reference/vega_version.html Retrieves the default version of Vega JavaScript libraries. Use `major = TRUE` to get only major version tags. ```R vega_version(major = FALSE) ``` ```R vega_version(major = TRUE) ``` -------------------------------- ### Get All Vega Versions Source: https://vegawidget.github.io/vegawidget/dev/reference/vega_version.html Retrieves a data frame of all Vega JavaScript library versions included in the package. Use `major = TRUE` to get only major version tags. ```R vega_version_all(major = FALSE) ``` ```R vega_version_all() ``` -------------------------------- ### Get Available Vega Versions Source: https://vegawidget.github.io/vegawidget/dev/reference/vega_version.html Retrieves a data frame of all available Vega JavaScript library versions, subject to locking. Use `major = TRUE` to get only major version tags. ```R vega_version_available(major = FALSE) ``` ```R vega_version_available() ``` -------------------------------- ### Get Item Value Handler Source: https://vegawidget.github.io/vegawidget/articles/articles/javascript.html Returns the item associated with the event. Use when you need the raw event item. ```javascript function(item) { // returns the item return item; } ``` -------------------------------- ### Create a Basic vegawidget Source: https://vegawidget.github.io/vegawidget/dev/reference/vegawidget.html Use `vegawidget()` to render a Vega/Vega-Lite specification as an HTML widget. Specify width and height to override the default dimensions from the spec. ```R vegawidget( spec, embed = NULL, width = NULL, height = NULL, elementId = NULL, base_url = NULL, ... ) ``` ```R vegawidget(spec_mtcars, width = 350, height = 350) ``` -------------------------------- ### Run Vega-Lite Shiny Demo Source: https://vegawidget.github.io/vegawidget/articles/shiny.html Launches a Shiny demo application for exploring signal set/get functionality with Vega-Lite. ```R vw_shiny_demo("signal-set-get") ``` -------------------------------- ### Get All Available Vega Versions Source: https://vegawidget.github.io/vegawidget/dev/reference/vega_version.html Returns a data frame of all available Vega JavaScript library versions, subject to locking. ```APIDOC ## vega_version_available ### Description Returns all available versions of Vega JavaScript libraries, subject to locking. ### Arguments - **major** (logical) - Optional - If TRUE, returns major version tags instead of specific supported versions. ### Value A data frame with columns: `widget`, `vega_lite`, `vega`, `vega_embed`. ### Example ```R vega_version_available() ``` ``` -------------------------------- ### Get All Included Vega Versions Source: https://vegawidget.github.io/vegawidget/dev/reference/vega_version.html Returns a data frame listing all Vega JavaScript library versions included in the package. ```APIDOC ## vega_version_all ### Description Returns a data frame showing all versions of Vega JavaScript libraries included in this package. ### Arguments - **major** (logical) - Optional - If TRUE, returns major version tags instead of specific supported versions. ### Value A data frame with columns: `widget`, `vega_lite`, `vega`, `vega_embed`. ### Example ```R vega_version_all() ``` ``` -------------------------------- ### Run Shiny Demo App Source: https://vegawidget.github.io/vegawidget/articles/shiny.html Run a specific Shiny demonstration app included with the vegawidget package. Use `vw_shiny_demo()` without arguments to list available demos. ```r vw_shiny_demo("data-set-get") ``` -------------------------------- ### Get Default Vega Version Source: https://vegawidget.github.io/vegawidget/dev/reference/vega_version.html Retrieves the default version of Vega JavaScript libraries. Can optionally return major versions. ```APIDOC ## vega_version ### Description Retrieves the default version of Vega JavaScript libraries. ### Arguments - **major** (logical) - Optional - If TRUE, returns major version tags instead of specific supported versions. ### Value A list with elements: `is_locked`, `widget`, `vega_lite`, `vega`, `vega_embed`. ### Example ```R vega_version() vega_version(major = TRUE) ``` ``` -------------------------------- ### Create Vega-Lite Widget with Autosize Source: https://vegawidget.github.io/vegawidget/articles/articles/image.html Generates a vegawidget using specified width and height, demonstrating autosizing. ```r vegawidget(spec_mtcars, width = 600, height = 300) ``` -------------------------------- ### Write PNG using vegawidget Source: https://vegawidget.github.io/vegawidget/articles/articles/image.html Demonstrates writing a PNG image from a vegawidget object using `vw_write_png`. Expectation is that the image will render at the same size as the reference, but not at retina resolution. ```R tmp_file <- tempfile(fileext = ".png") vw_write_png(vegawidget(spec_mtcars), path = tmp_file) tags$img(src = knitr::image_uri(tmp_file)) ``` -------------------------------- ### Convert Vega-Lite to SVG String Source: https://vegawidget.github.io/vegawidget/articles/articles/image.html Use `vw_to_svg()` to get an SVG representation of a Vega-Lite spec as a string. This string can be further processed or displayed. ```r vw_to_svg(spec_mtcars) %>% str() ``` ```r ## chr "\n% as_vegaspec() output_chart <- vegawidget(spec_circle, elementId = "circle") %>% vw_add_data_listener("source", handler_data) ``` -------------------------------- ### List Available Vega Versions Source: https://vegawidget.github.io/vegawidget/dev/index.html Use `vega_version_all()` to see the available Vega and Vega-Lite versions supported by vegawidget. This helps in understanding compatibility for rendering specifications. ```R library("vegawidget") vega_version_all() #> widget vega_lite vega vega_embed #> 1 vl5 5.16.3 5.24.0 6.22.2 #> 2 vl4 4.17.0 5.17.0 6.12.2 ``` -------------------------------- ### Get Datum Value Handler Source: https://vegawidget.github.io/vegawidget/articles/javascript.html Safely returns the datum associated with the event's item. Returns null if the item or its datum is undefined. ```javascript if (item === null || item === undefined || item.datum === undefined) { return null; } return item.datum; ``` -------------------------------- ### Basic vegawidget creation Source: https://vegawidget.github.io/vegawidget/reference/vegawidget.html Renders a `vegawidget` with specified dimensions. This is effective for single-view and layered charts. ```R vegawidget(spec_mtcars, width = 350, height = 350) ``` -------------------------------- ### Get Datum Value Handler Source: https://vegawidget.github.io/vegawidget/articles/articles/javascript.html Returns the datum behind the mark associated with the event. Handles cases where item or datum might be undefined. ```javascript function(item) { // if there is no item or no datum, return null if (item === null || item === undefined || item.datum === undefined) { return null; } // returns the datum behind the mark associated with the event return item.datum; } ``` -------------------------------- ### Get Bitmap Dimensions (Scale 2) Source: https://vegawidget.github.io/vegawidget/articles/articles/image.html Converts a Vega-Lite specification to a bitmap array using `vw_to_bitmap` with a scale of 2 and displays its dimensions. ```R vw_to_bitmap(spec_mtcars, scale = 2) %>% dim() ``` ```R ## [1] 694 784 4 ``` -------------------------------- ### Write PNG (Retina Scale) Source: https://vegawidget.github.io/vegawidget/articles/articles/image.html Writes a PNG image file from a Vega-Lite specification with an increased scale (2) and then halves the display size to achieve retina resolution. Expectation is that the image will render at the same size as the reference, at retina resolution. ```R tmp_file <- tempfile(fileext = ".png") vw_write_png(spec_mtcars, path = tmp_file, scale = 2) tags$img( src = knitr::image_uri(tmp_file), onload = "this.width/=2;this.onload=null;" ) ``` -------------------------------- ### Fetch Data from URL or File Source: https://vegawidget.github.io/vegawidget/reference/vw_fetch.html Use `vw_fetch` to retrieve content from a URL or `vw_load` to read from a local file. Both functions assume UTF-8 encoding. ```R vw_fetch(url, options = NULL, encoding = "UTF-8") vw_load(filename, encoding = "UTF-8") ``` -------------------------------- ### Equivalent vw_shiny_get_event Calls Source: https://vegawidget.github.io/vegawidget/dev/articles/articles/shiny.html Demonstrates equivalent ways to call vw_shiny_get_event, either by referencing a named handler in the library or by providing the JavaScript function body directly. ```R # name of a body in the event-handler library vw_shiny_get_event(..., body_value = "item") # body of a JavaScript function vw_shiny_get_event(..., body_value = "return item;") ``` -------------------------------- ### Set Renderer to SVG Source: https://vegawidget.github.io/vegawidget/articles/articles/render-vegawidget.html Customize the rendering by specifying the `renderer` option within the `vega_embed()` helper function. This example changes the renderer to SVG. ```R vegawidget(spec_mtcars, embed = vega_embed(renderer = "svg")) ``` -------------------------------- ### Load Required R Packages Source: https://vegawidget.github.io/vegawidget/articles/articles/dates-times.html Loads essential R packages for data manipulation, date handling, and visualization. Ensure these packages are installed before running. ```r library("vegawidget") library("conflicted") library("lubridate") library("readr") library("dplyr") ``` -------------------------------- ### Get Item Datum with Vega-Lite Handler Source: https://vegawidget.github.io/vegawidget/dev/articles/articles/javascript.html Returns the datum associated with the mark for the given event. Handles cases where item or datum might be undefined. ```javascript vw_handler_event("datum") ``` ```javascript ## arguments: event, item body_value: // if there is no item or no datum, return null if (item === null || item === undefined || item.datum === undefined) { return null; } // returns the datum behind the mark associated with the event return item.datum; ``` -------------------------------- ### Create Vega Specification and Widget Source: https://vegawidget.github.io/vegawidget/articles/javascript.html Constructs a Vega specification for a point on the unit circle and creates a vegawidget. It attaches a data listener to the 'source' dataset to trigger updates via the defined handler. ```R spec_circle <- list( `$schema` = vega_schema(), width = 300, height = 300, data = list( values = list(x = 1, y = 0), name = "source" ), mark = "point", encoding = list( x = list( field = "x", type = "quantitative", scale = list(domain = list(-1, 1)) ), y = list( field = "y", type = "quantitative", scale = list(domain = list(-1, 1)) ) ) ) %>% as_vegaspec() output_chart <- vegawidget(spec_circle, elementId = "circle") %>% vw_add_data_listener("source", handler_data) ``` -------------------------------- ### Configure Data Handler for Vega-View Source: https://vegawidget.github.io/vegawidget/articles/articles/javascript.html Sets up a handler to format and display data changes from a Vega view. It targets an element with the ID 'output-data' and stringifies the data. ```R handler_data <- vw_handler_data("value") %>% vw_handler_add_effect( "element_text", selector = "#output-data", expr = "JSON.stringify(x, null, ' ');" ) ``` -------------------------------- ### Compile Vega-Lite to Vega Spec Source: https://vegawidget.github.io/vegawidget/articles/articles/image.html Converts a Vega-Lite specification to a Vega specification using `vw_to_vega()`. ```r vw_to_vega(spec_mtcars) ``` -------------------------------- ### Define a custom signal-handler Source: https://vegawidget.github.io/vegawidget/dev/reference/vw_handler_signal.html Define a custom signal handler by providing the JavaScript code directly as a string, for example, to return the signal's value. ```R vw_handler_signal("return value;") ```