Skip to content

Commit

Permalink
Refactor with switch statement
Browse files Browse the repository at this point in the history
  • Loading branch information
HDash committed Oct 22, 2024
1 parent 6b50885 commit c99577c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
58 changes: 25 additions & 33 deletions R/download_button.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,29 @@ download_button <- function(path,
icon = "fa fa-save",
add_button = TRUE,
...) {
if (add_button) {
wrn_msg <- paste("Package", shQuote("downloadthis"), "is required to",
"add download buttons to the HTML report. Skipping",
"download buttons...")
check_dep("downloadthis", fatal = FALSE, custom_msg = wrn_msg)

type <- tolower(type)
if (type == "dir") {
btn <- downloadthis::download_dir(
path = path,
output_name = output_name,
button_label = button_label,
button_type = button_type,
has_icon = has_icon,
icon = icon,
self_contained = TRUE
)
} else if (type == "file") {
btn <- downloadthis::download_file(
path = path,
output_name = output_name,
button_label = button_label,
button_type = button_type,
has_icon = has_icon,
icon = icon,
self_contained = TRUE
)
}

return(btn)
} else {
return(invisible())
}
if (!add_button) return(invisible())

wrn_msg <- paste("Package", shQuote("downloadthis"), "is required to",
"add download buttons to the HTML report. Skipping",
"download buttons...")
check_dep("downloadthis", fatal = FALSE, custom_msg = wrn_msg)

btn_args <- list(
path = path,
output_name = output_name,
button_label = button_label,
button_type = button_type,
has_icon = has_icon,
icon = icon,
self_contained = TRUE
)

type <- tolower(type)
btn <- switch(
type,
"dir" = do.call(downloadthis::download_dir, btn_args),
"file" = do.call(downloadthis::download_file, btn_args)
)

return(btn)
}
4 changes: 4 additions & 0 deletions tests/testthat/test-download_button.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ test_that("download_button works", {
type = "file",
button_label = "Download Peaks")
expect_true(grepl("button", btn))
expect_invisible(download_button(out, type = "file",
button_label = "Download Peaks",
add_button = FALSE))
})

0 comments on commit c99577c

Please sign in to comment.