1
0
Fork
You've already forked terminalgraphics
0
R-package that defines a graphics device and functions for graphical output in terminal emulators that support graphical output. Currently terminals that support the Terminal Graphics Protocol and terminal supporting Sixel are supported.
  • R 81.2%
  • C++ 17.8%
  • Makefile 1%
2025年08月29日 08:14:00 +02:00
man Documentation cleanup 2025年08月22日 22:54:02 +02:00
R Documentation cleanup 2025年08月22日 22:54:02 +02:00
src Fixed bug in querying support in windows 2025年08月22日 22:39:50 +02:00
work Renames kitty_width/_height_/_dim to term_ 2025年08月13日 07:28:19 +02:00
.gitignore Added .gitignore 2025年08月12日 17:21:01 +02:00
.Rbuildignore Screenshot added to readme 2025年07月08日 09:24:23 +02:00
DESCRIPTION Made magick a suggested dependency 2025年08月21日 20:16:39 +02:00
kitty_demo.png Updated README.md ( fixes #3 and #4 ) 2025年08月20日 22:15:37 +02:00
LICENSE Initial commit 2022年02月22日 08:42:58 +01:00
Makefile Renamed the package to terminalgraphics 2025年08月17日 20:27:37 +02:00
NAMESPACE Sixel support ( fixes #2 ) 2025年08月21日 23:55:24 +02:00
README.md Changed .Rprofile example as suggested in #6 ( fixes #6 ) 2025年08月29日 08:14:00 +02:00

terminalgraphics

An R-package for graphical output in terminals. Currently supports terminals that support the Terminal Graphics Protocol and terminals that support Sixel. It implements a graphical device that will output into the terminal and also some functions for retrieving information from the terminal such as the colour scheme used, the dimensions of the terminal and function for displaying png images and image matrices.

Example code

Set the tgp (Terminal Graphics Protocol) device as the standard device and instruct the package to use the terminal colors (only supported currently in Kitty):

options(device = terminalgraphics::tgp, term_col = TRUE)

For example, one could use the following lines in ~/.Rprofile to set the the tgp device as the default device.

if (interactive() && requireNamespace("terminalgraphics", quietly = TRUE) && 
 terminalgraphics::has_tgp_support()) {
 options(device = terminalgraphics::tgp, term_col = TRUE)
}

or for Sixel support:

if (interactive() && requireNamespace("terminalgraphics", quietly = TRUE) && 
 terminalgraphics::has_sixel_support()) {
 options(device = terminalgraphics::sixel)
}

Creating a plot like, for example:

{
 data(iris)
 plot(iris$Sepal.Width, iris$Sepal.Length, 
 col = term_palette()[iris$Species], 
 pch = 20)
 grid(lty = 2)
 legend("topright", legend = levels(iris$Species), 
 col = term_palette()[1:3], pch = 20)
}

Results in:

Screenshot of a plot in kitty

By default it will match the colours of the plot to those used of the theme used in the terminal (currently the theme can only be determined for Kitty).

Other functions are:

png2terminal, png2tgp, png2sixel
display a png file in the terminal.
raster2terminal, raster2tgp, raster2sixel
display image raster in the terminal.
term_dim, term_width, term_height
Get the dimensions of the terminal (in pixels and number rows and columns).
term_palette
get a palette based on the colours used in the current theme.
kitty_foreground and kitty_background
get the foreground and background colours of the current theme.
has_tgp_support
determine if the terminal supports the Terminal Graphics Protocol.
has_sixel_support
determine if the terminal supports Sixel.
is_kitty
is R running in a Kitty terminal.

Tips

Every time the contents of the graphics device change after evaluating an expression, a new version of the plot is shown in the terminal. Therefore, the following two lines will result in two plots in the terminal:

plot(rnorm(200))
abline(h = 0)

If you want only one plot to appear in the terminal, you should combine the lines into one expression:

{ 
 plot(rnorm(200))
 abline(h = 0)
}

The size and resolution of the plot can be changed using arguments of tgp and sixel:

tgp(width = 1000, height = 500)
plot(rnorm(200))

It is also possible to set some of these using options:

options(device = terminalgraphics::tgp, term_width = 1000, term_height = 500, 
 term_res = 150, term_col = TRUE)

Note that these options are used for new devices. Therefore, when there is already a device open, you should call tgp/sixel to open a new device or close open devices using dev.off before starting a new plot.