Type: Package
Title: R Wrapper for 'pikchr' (PIC) Diagram Language
Version: 1.0.1
Date: 2025-03-31
Description: An 'R' interface to 'pikchr' (https://pikchr.org, pronounced “picture”), a 'PIC'-like markup language for creating diagrams within technical documentation. Originally developed by Brian Kernighan, 'PIC' has been adapted into 'pikchr' by D. Richard Hipp, the creator of 'SQLite'. 'pikchr' is designed to be embedded in fenced code blocks of Markdown or other documentation markup languages, making it ideal for generating diagrams in text-based formats. This package allows R users to seamlessly integrate the descriptive syntax of 'pikchr' for diagram creation directly within the 'R' environment.
License: GPL-2 | GPL-3 [expanded from: GPL (≥ 2)]
Encoding: UTF-8
RoxygenNote: 7.3.2
VignetteBuilder: knitr
Imports: dplyr, knitr, brio, htmltools, rlang, purrr, stringr, rsvg
Suggests: kableExtra, tidyverse
URL: <https://github.com/StrategicProjects/pikchr>
Note: This package makes use of the 'pikchr' source code created by Richard Hipp (<https://pikchr.org>).
Depends: R (≥ 4.0)
LazyData: true
Config/Needs/website: tidyverse/tidytemplate, rmarkdown
NeedsCompilation: yes
Packaged: 2025-03-31 13:59:02 UTC; leite
Author: Andre Leite [aut, cre], Hugo Vaconcelos [aut], Richard Hipp [ctb], Brian Kernighan [ctb]
Maintainer: Andre Leite <leite@castlab.org>
Repository: CRAN
Date/Publication: 2025-03-31 14:10:02 UTC

Pikchr Code Engine for Knitr

Description

This function serves as a custom code engine for Knitr to render Pikchr diagrams as SVG or PNG images. It processes Pikchr code embedded in R Markdown documents and outputs either HTML or LaTeX-compatible images.

Usage

eng_pikchr(options)

Arguments

options

A list of chunk options from Knitr. It includes the Pikchr code and various parameters controlling the size, appearance, and output format of the rendered image.

Details

The 'eng_pikchr' function allows you to use Pikchr code inside R Markdown documents, rendering it as an image. It supports options like 'width', 'height', font 'size', font 'family', and CSS classes to customize the output. The function automatically detects the output format (HTML or LaTeX) and generates the appropriate image.

If the output is LaTeX, the SVG is converted to PNG using the 'rsvg_png()' function.

Value

This function returns an SVG (or PNG for LaTeX output) rendered by Pikchr.

Chunk Options

- 'eval': Whether to evaluate the Pikchr code (default 'TRUE'). - 'echo': Whether to display the code in the output document (default 'TRUE'). - 'width': Width of the rendered image (default '"80 - 'height': Height of the rendered image (default '"auto"'). - 'size': Font size for the Pikchr diagram (default '"100 - 'family': Font family for the Pikchr diagram (default '"inherit"'). - 'align': Figure align: left, right, center. - 'css': Additional CSS for customizing the Pikchr diagram. - 'class': CSS class to apply to the diagram (default '"pikchr"'). - 'margin': CSS margin around the image (default '"10px 0 10px 0"'). - 'lang': Language for the Pikchr diagram (optional).

See Also

[knitr::knit_engines]


Validate Knitr Chunk Options for Pikchr Engine

Description

This function validates the chunk options provided to the Pikchr engine in Knitr. It checks for unsupported numeric options and converts them to logical 'TRUE' values. A warning is issued if any unsupported numeric options are detected.

Usage

eng_pikchr_validate_options(options)

Arguments

options

A list of options passed to the Pikchr engine from a Knitr code chunk. These options include directives such as 'eval', 'echo', and 'warning'.

Details

The 'eng_pikchr_validate_options' function looks for specific options (e.g., 'eval', 'echo', 'warning') that should not be numeric. If any of these options are numeric, they are converted to logical 'TRUE' and a warning is issued to the user.

Value

The validated options list, where unsupported numeric values are converted to 'TRUE'.


Google Font List

Description

List of fonts and its stytles on google fonts site. Report ...

Usage

google_fonts

Format

## 'google_fonts' A data frame with 1,718 rows and 2 columns:

family

Font family names

styles

Font styles

Source

<https://fonts.google.com>


Convert Pikchr Code to SVG

Description

This function converts a diagram description written in the Pikchr language into an SVG (Scalable Vector Graphics) format. Pikchr is a diagram language inspired by PIC, and this function allows you to easily embed Pikchr diagrams as SVG images in your HTML or Markdown documents.

Usage

pikchr(
  code,
  width = NULL,
  height = NULL,
  fontSize = "100%",
  fontFamily = "Jost",
  class = "pikchr",
  align = "none",
  css = NULL,
  margin = NULL,
  svgOnly = FALSE
)

Arguments

code

A character string containing the diagram code written in the Pikchr language.

width

A character string representing the width of the rendered SVG. It accepts standard HTML values such as percentages ("75%"), pixels ("300px"), or "auto". Defaults to "75%".

height

A character string representing the height of the rendered SVG. It accepts standard HTML values such as pixels ("300px") or "auto". Defaults to "auto".

fontSize

A character string specifying the font size of the text within the SVG. It accepts standard HTML values such as percentages ("80%"), pixels, or keywords ("large"). Defaults to "80%".

fontFamily

A character string specifying the font family for the text in the diagram. Common values include "inherit" (to use the page's default font), "Arial", "Times New Roman", etc. Defaults to "inherit".

class

A character string specifying a CSS class to apply to the SVG. This can be used to style the SVG with external CSS rules. Defaults to "pikchr".

align

A character string specifying the alignment of the SVG within its container. Options are "center" (default), "left", or "right". If set to "none", no alignment is applied.

css

A character string containing extra CSS styles to apply directly to the SVG element. This allows additional customization beyond width, height, and font settings. If NULL, no extra styles are applied.

margin

A character string specifying the margin around the SVG element, formatted like a CSS margin rule (e.g., "10px 0 10px 0"). Defaults to "10px 0 10px 0".

svgOnly

Logical, whether to return only the raw SVG code (TRUE) or open the diagram in the Viewer window (FALSE, default).

Value

A character string containing the SVG diagram code if svgOnly = TRUE. Otherwise, the function opens the diagram in the Viewer window.

Examples

if(interactive()) {
pikchr('
  arrow right 200% "Rmarkdown" "Source"
  box rad 10px "Rmarkdown" "(knitr)" fit
  arrow right 200% "HTML+SVG" "Output"
  arrow <-> down from last box.s
  box same "Pikchr" "(pikchr.c)" fit
  ')
  }

Run Plot Hook and Record Plot Filenames

Description

This function records the filenames of plot outputs (such as SVG or PNG files) in the Knitr environment and then runs the appropriate plot hook to display or save the plots.

Usage

run_hook_plot(x, options)

Arguments

x

A character string representing the path to the plot file.

options

A list of options associated with the Knitr chunk, such as plot settings and output options.

Details

The 'run_hook_plot' function appends the plot file path to the internal Knitr option 'plot_files'. It then retrieves the current plot hook defined in 'knit_hooks' and calls it to handle the plot rendering or output. This function is particularly useful for handling plots generated from both R code and other outputs like HTML widgets.

Value

The result of calling the current Knitr plot hook with the specified plot file and options.