Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] wb_add_plot() #579

Merged
merged 2 commits into from
Apr 8, 2023
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
8 changes: 7 additions & 1 deletion R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -3722,7 +3722,7 @@ wbWorkbook <- R6::R6Class(
dims = rowcol_to_dim(startRow, startCol)
) {
if (!file.exists(file)) {
stop("File does not exist.")
stop("File ", file, " does not exist.")
}

if (is.null(dims) && (startRow > 1 || startCol > 1)) {
Expand Down Expand Up @@ -3897,6 +3897,11 @@ wbWorkbook <- R6::R6Class(

fileName <- tempfile(pattern = "figureImage", fileext = paste0(".", fileType))

# Workaround for wrapper test. Otherwise tempfile names differ
if (requireNamespace("testthat")) {
if (testthat::is_testing()) fileName <- getOption("openxlsx2.temp_png")
}

# TODO use switch()
if (fileType == "bmp") {
dev.copy(bmp, filename = fileName, width = width, height = height, units = units, res = dpi)
Expand All @@ -3910,6 +3915,7 @@ wbWorkbook <- R6::R6Class(

## write image
invisible(dev.off())
stopifnot(file.exists(fileName))

self$add_image(
sheet = sheet,
Expand Down
42 changes: 23 additions & 19 deletions tests/testthat/test-class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,29 @@ test_that("wb_add_image() is a wrapper", {
# wb_add_plot() -----------------------------------------------------------

test_that("wb_add_plot() is a wrapper", {
# plot is written to file. test can only be completed in interactive mode
if (interactive()) {

plot(1:5, 1:5)
wb <- wb_workbook()$add_worksheet("a")

# okay, not the best but the results have different field names. Maybe that's
# a feature to add to expect_wrapper()
expect_error(
expect_wrapper(
"add_plot",
"wb_add_plot",
wb = wb,
params = list(sheet = "a")
),
"wbWorkbook$add_plot$media$image1.png vs wb_add_plot$media$image1.png",
fixed = TRUE
)
}

# workaround: this filename is inserted to the wrapper function
options("openxlsx2.temp_png" = tempfile(pattern = "figureImage", fileext = ".png"))

# create a device we can dev.copy() from
grDevices::pdf(NULL) # do not create "Rplots.pdf"
grDevices::dev.control("enable")
plot(1:5, 1:5)

wb <- wb_workbook()$add_worksheet("a")

expect_wrapper(
"add_plot",
"wb_add_plot",
wb = wb,
params = list(sheet = "a")
)

# # check that it is actually working
# wb$add_plot(sheet = "a")$save("~/test.xlsx")

grDevices::dev.off()

})

test_that("wb_add_drawing is a wrapper", {
Expand Down