diff --git a/R/export.R b/R/export.R index d3a0d1c..140ec97 100644 --- a/R/export.R +++ b/R/export.R @@ -35,8 +35,11 @@ export <- function( verbose_print <- if (verbose) message else list stopifnot(fs::is_dir(appdir)) - if (!fs::file_exists(fs::path(appdir, "app.R"))) { - stop("Directory ", appdir, " does not contain an app.R file.") + if (!( + fs::file_exists(fs::path(appdir, "app.R")) || + fs::file_exists(fs::path(appdir, "server.R")) + )) { + stop("Directory ", appdir, " does not contain an app.R or server.R file.") } if (fs::is_absolute_path(subdir)) { diff --git a/local/shiny-apps/global-r/global.R b/local/shiny-apps/global-r/global.R new file mode 100644 index 0000000..87dae9f --- /dev/null +++ b/local/shiny-apps/global-r/global.R @@ -0,0 +1 @@ +global_value <- 50 diff --git a/local/shiny-apps/global-r/server.R b/local/shiny-apps/global-r/server.R new file mode 100644 index 0000000..dc26bf9 --- /dev/null +++ b/local/shiny-apps/global-r/server.R @@ -0,0 +1,8 @@ +library(shiny) + + +function(input, output) { + output$txt <- renderText({ + paste0("The value of n*2 is ", 2 * input$n) + }) +} diff --git a/local/shiny-apps/global-r/ui.R b/local/shiny-apps/global-r/ui.R new file mode 100644 index 0000000..c053da2 --- /dev/null +++ b/local/shiny-apps/global-r/ui.R @@ -0,0 +1,5 @@ +fluidPage( + markdown("## `ui.R` / `server.R` / `global.R`"), + sliderInput("n", "N", 0, 100, global_value), + verbatimTextOutput("txt", placeholder = TRUE), +) diff --git a/tests/testthat/test-export.R b/tests/testthat/test-export.R index 1dc9f75..9017c2f 100644 --- a/tests/testthat/test-export.R +++ b/tests/testthat/test-export.R @@ -1,6 +1,4 @@ - - -test_that("export", { +test_that("export - app.R", { maybe_skip_test() assets_ensure() @@ -60,3 +58,65 @@ test_that("export", { ) expect_setequal(dir(file.path(out_dir, "test_subdir", "edit")), asset_edit_files) }) + + +test_that("export - server.R", { + maybe_skip_test() + + assets_ensure() + + app_dir <- tempfile() + dir.create(app_dir, recursive = TRUE) + on.exit(unlink_path(app_dir), add = TRUE) + server_r <- file.path(app_dir, "server.R") + ui_r <- file.path(app_dir, "ui.R") + global_r <- file.path(app_dir, "global.R") + + cat( + file = server_r, + collapse(c( + "library(shiny)", + "shinyServer(function(input, output, session) {", + " output$txt <- renderText({", + " paste0(\"The value of n*2 is \", 2 * input$n)", + " })", + "})", + "" + )) + ) + cat( + file = ui_r, + collapse(c( + "library(shiny)", + "shinyUI(fluidPage(", + " sliderInput(\"n\", \"N\", 0, 100, 40),", + " verbatimTextOutput(\"txt\", placeholder = TRUE),", + "))", + "" + )) + ) + cat( + file = global_r, + collapse(c( + "library(shiny)", + "global_value <- 50", + "" + )) + ) + + # Create a temporary directory + out_dir <- file.path(tempfile(), "out") + + # Verify global.R / ui.R / server.R app can be exported + expect_silent({ + export(app_dir, out_dir) + }) + # Verify global.R / ui.R / server.R exported files exist + + app_json <- jsonlite::read_json(file.path(out_dir, "app.json")) + out_app_file_names <- vapply(app_json, `[[`, character(1), "name") + expect_setequal( + out_app_file_names, + c("global.R", "ui.R", "server.R") + ) +})