Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
^LICENSE\.md$
^\.github$
^CODEOWNERS$
^data-raw$
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# gex 0.2.0.9000

* Added hex logo for the package.
* Added 'resolution' argument to `open_device()`.

# gex 0.2.0

Expand Down
11 changes: 9 additions & 2 deletions R/hexagon.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#'
#' @param file_path Character. File path to a .png where the output file will be
#' saved. The containing directory must already exist.
#' @param resolution Numeric. Resolution of the graphics device in pixels per
#' inch (ppi). Higher values have better resolution but create larger file
#' sizes.
#'
#' @details
#'
Expand All @@ -31,7 +34,7 @@
#' add_text()
#' add_border()
#' close_device()
open_device <- function(file_path) {
open_device <- function(file_path, resolution = 300) {

if (tools::file_ext(file_path) != "png") {
stop("Argument 'file_path' must end with '.png'.", call. = FALSE)
Expand All @@ -44,12 +47,16 @@ open_device <- function(file_path) {
)
}

if (!inherits(resolution, "numeric")) {
stop("Argument 'resolution' must be a numeric value.", call. = FALSE)
}

grDevices::png(
filename = file_path,
width = 4.39,
height = 5.08,
units = "cm",
res = 1200,
res = resolution,
bg = "transparent"
)

Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# {gex} <a href="https://github.com/matt-dray/gex"><img src="man/figures/logo.png" align="right" height="139"/></a>
# {gex} <a href='https://github.com/matt-dray/gex'><img src='man/figures/logo.png' align='right' width='200'/></a>

<!-- badges: start -->
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
Expand Down Expand Up @@ -58,7 +58,10 @@ image_path <- system.file("img", "Rlogo.png", package = "png")
image_png <- png::readPNG(image_path)

# Build and write the hex
gex::open_device(file_path = temp_path)
gex::open_device(
file_path = temp_path,
resolution = 300 # ppi, you can increase this
)
gex::add_hex(col = "#BEBEBE") # named or hexadecimal
gex::add_image(
img = image_png,
Expand Down Expand Up @@ -86,9 +89,9 @@ gex::add_border(col = "grey20")
gex::close_device() # writes to file
```

That creates this absolutely stunning sticker, written to the specified `file_path`:
That creates this absolutely stunning demo sticker, written to the specified `file_path`:

<img src='man/figures/readme-hex.png' width='300' alt="A grey hexagon with a dark grey border. An R logo is shown just above centre and angled at 30 degrees. Just below centre is the text 'example' in monospace red and bold font. On the lower right edge is the URL 'htps://rstats.lol' in smaller, blue italic serif font.">
<img src='man/figures/example.png' width='200' alt="A grey hexagon with a dark grey border. An R logo is shown just above centre and angled at 30 degrees. Just below centre is the text 'example' in monospace red and bold font. On the lower right edge is the URL 'htps://rstats.lol' in smaller, blue italic serif font.">

Note that you can't rely on plot-window previews when you're developing your sticker (they lie).
You must inspect the generated PNG file instead.
Expand Down
6 changes: 5 additions & 1 deletion inst/tinytest/test_gex.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ dev_open <- open_device(temp_path)
expect_null(dev_open)
dev_close <- dev.off()
expect_true(is.numeric(dev_close))
unlink(temp_path)

err_arg <- 'argument "file_path" is missing, with no default'
expect_error(open_device(), err_arg)
Expand All @@ -22,6 +21,11 @@ expect_error(open_device("x/y.pdf"), err_png)
err_dir <- "Argument 'file_path' must resolve to an existing directory\\."
expect_error(open_device("x/y/z.png", err_dir))

err_res <- "Argument 'resolution' must be a numeric value\\."
expect_error(open_device(temp_path, resolution = "x"), err_res)

unlink(temp_path)

# Add hex -----------------------------------------------------------------

hex <- add_hex()
Expand Down
Binary file added man/figures/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed man/figures/readme-hex.png
Binary file not shown.
6 changes: 5 additions & 1 deletion man/open_device.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading