Skip to content

Commit

Permalink
Allow for app.R or server.R apps (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke authored Sep 29, 2023
1 parent 7eb9e40 commit ec11991
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
7 changes: 5 additions & 2 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions local/shiny-apps/global-r/global.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global_value <- 50
8 changes: 8 additions & 0 deletions local/shiny-apps/global-r/server.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
library(shiny)


function(input, output) {
output$txt <- renderText({
paste0("The value of n*2 is ", 2 * input$n)
})
}
5 changes: 5 additions & 0 deletions local/shiny-apps/global-r/ui.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fluidPage(
markdown("## `ui.R` / `server.R` / `global.R`"),
sliderInput("n", "N", 0, 100, global_value),
verbatimTextOutput("txt", placeholder = TRUE),
)
66 changes: 63 additions & 3 deletions tests/testthat/test-export.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@


test_that("export", {
test_that("export - app.R", {
maybe_skip_test()

assets_ensure()
Expand Down Expand Up @@ -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")
)
})

0 comments on commit ec11991

Please sign in to comment.