From 777fe7f223fdda606a595acc4db8d2652c212933 Mon Sep 17 00:00:00 2001 From: Mohammed Ali Date: Mon, 9 Aug 2021 22:52:43 -0700 Subject: [PATCH 1/2] migrated vignettes --- .gitignore | 1 - vignettes/downloadFile-module.Rmd | 46 ++++++++++---------------- vignettes/downloadablePlot-module.Rmd | 34 +++++++------------ vignettes/downloadableTable-module.Rmd | 40 +++++++++------------- 4 files changed, 44 insertions(+), 77 deletions(-) diff --git a/.gitignore b/.gitignore index 4bd7950..82d7185 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,5 @@ .Ruserdata **/.DS_Store vignettes/*.html -vignettes/*.R **/log .project diff --git a/vignettes/downloadFile-module.Rmd b/vignettes/downloadFile-module.Rmd index 8e04442..d0eff28 100755 --- a/vignettes/downloadFile-module.Rmd +++ b/vignettes/downloadFile-module.Rmd @@ -14,7 +14,7 @@ vignette: > # Overview -## Purpose +## Purpose This *Shiny Module* was created in order to provide a consistent-looking and easy-to-use button that facilitates one or multiple types of file downloads. @@ -36,21 +36,16 @@ easy-to-use button that facilitates one or multiple types of file downloads. Shiny modules consist of a pair of functions that modularize, or package, a small piece of reusable functionality. The UI function is called directly by the user to place the UI in the correct location (as with other shiny UI -objects). The server function is not called directly by the user of the module. -Instead the module server function is called only once to set it up using the -shiny::callModule function inside the server function (i.e. user-local session -scope. The callModule function supplies the first three arguments of the -Shiny Module's function inputs - the input, output, and session. Additional -arguments supplied by the user in the callModule function are passed to the -specific shiny module that is called. There can be additional helper functions -that are a part of a shiny module. +objects). The module server function that is called only once to set it up using the +module name as a function inside the server function (i.e. user-local session +scope. The function first arguments is string represents the module id (the same id used in module UI function). Additional arguments can be supplied by the user based on the specific shiny module that is called. There can be additional helper functions that are a part of a shiny module. The **downloadFile** Shiny Module is a part of the *periscope* package and consists of the following functions: * **downloadFileButton** - the UI function to place the button in the -application -* **downloadFile** - the Server function supplied to callModule. +application. +* **downloadFile** - the server function to be called inside server_local.R. * **downloadFile_ValidateTypes** - a helper function that will check a given list of file types and warn the caller if the list contains an invalid or unsupported type. @@ -101,13 +96,8 @@ downloadFileButton("object_id2", ## downloadFile -The **downloadFile** function is not called directly - instead a call to -shiny::callModule is made inside the server.R (or equivalent) file to initialize -the module. +The **downloadFile** function is called directly. The call consists of the following: -The call consists of the following: - -* the name of the module - unquoted * the unique object ID that was provided to downloadFileButton when creating the UI object * the logging logger to be used @@ -142,20 +132,18 @@ to the user from the application. # Inside server_local.R #single download type -callModule(downloadFile, - "object_id1", - logger = ss_userAction.Log, - filenameroot = "mydownload1", - datafxns = list(csv = mydatafxn1), - aspectratio = 1) +downloadFile("object_id1", + logger = ss_userAction.Log, + filenameroot = "mydownload1", + datafxns = list(csv = mydatafxn1), + aspectratio = 1) #multiple download types -callModule(downloadFile, - "object_id2", - logger = ss_userAction.Log, - filenameroot = "mytype2", - datafxns = list(csv = mydatafxn1, xlsx = mydatafxn2), - aspectratio = 1) +downloadFile("object_id2", + logger = ss_userAction.Log, + filenameroot = "mytype2", + datafxns = list(csv = mydatafxn1, xlsx = mydatafxn2), + aspectratio = 1) ``` diff --git a/vignettes/downloadablePlot-module.Rmd b/vignettes/downloadablePlot-module.Rmd index 56a58c1..9a017c0 100755 --- a/vignettes/downloadablePlot-module.Rmd +++ b/vignettes/downloadablePlot-module.Rmd @@ -15,7 +15,7 @@ vignette: > # Overview -## Purpose +## Purpose This *Shiny Module* was created in order to provide an easy-to-use downloadFileButton for a plot that is automatically created, linked @@ -47,14 +47,9 @@ button Shiny modules consist of a pair of functions that modularize, or package, a small piece of reusable functionality. The UI function is called directly by the user to place the UI in the correct location (as with other shiny UI -objects). The server function is not called directly by the user of the module. -Instead the module server function is called only once to set it up using the -shiny::callModule function inside the server function (i.e. user-local session -scope. The callModule function supplies the first three arguments of the -Shiny Module's function inputs - the input, output, and session. Additional -arguments supplied by the user in the callModule function are passed to the -specific shiny module that is called. There can be additional helper functions -that are a part of a shiny module. +objects). The module server function that is called only once to set it up using the +module name as a function inside the server function (i.e. user-local session +scope. The function first arguments is string represents the module id (the same id used in module UI function). Additional arguments can be supplied by the user based on the specific shiny module that is called. There can be additional helper functions that are a part of a shiny module. ## downloadablePlotUI @@ -93,13 +88,8 @@ downloadablePlotUI("object_id1", ## downloadablePlot -The **downloadablePlot** function is not called directly - instead a call to -shiny::callModule is made inside the server.R (or equivalent) file to initialize -the module. +The **downloadablePlot** function is also called directly. The call consists of the following: -The call consists of the following: - -* the name of the module - unquoted * the unique object ID that was provided to downloadablePlotUI when creating the UI object * the logging logger to be used @@ -139,13 +129,13 @@ to the user from the application. All the above requirements apply. ```{r, eval = F} # Inside server_local.R -callModule(downloadablePlot, - "object_id1", - logger = ss_userAction.Log, - filenameroot = "mydownload1", - aspectratio = 1.33, - downloadfxns = list(png = myplotfxn, tsv = mydatafxn), - visibleplot = myplotfxn) +downloadablePlot("object_id1", + logger = ss_userAction.Log, + filenameroot = "mydownload1", + aspectratio = 1.33, + downloadfxns = list(png = myplotfxn, tsv = mydatafxn), + visibleplot = myplotfxn) + ```
diff --git a/vignettes/downloadableTable-module.Rmd b/vignettes/downloadableTable-module.Rmd index e74be09..cffaddb 100755 --- a/vignettes/downloadableTable-module.Rmd +++ b/vignettes/downloadableTable-module.Rmd @@ -15,7 +15,7 @@ vignette: > # Overview -## Purpose +## Purpose This *Shiny Module* was created in order to provide a consistent-looking and easy-to-use table including a downloadFileButton that is automatically created, @@ -48,21 +48,16 @@ scrolling (no paging) Shiny modules consist of a pair of functions that modularize, or package, a small piece of reusable functionality. The UI function is called directly by the user to place the UI in the correct location (as with other shiny UI -objects). The server function is not called directly by the user of the module. -Instead the module server function is called only once to set it up using the -shiny::callModule function inside the server function (i.e. user-local session -scope. The callModule function supplies the first three arguments of the -Shiny Module's function inputs - the input, output, and session. Additional -arguments supplied by the user in the callModule function are passed to the -specific shiny module that is called. There can be additional helper functions -that are a part of a shiny module. +objects). The module server function that is called only once to set it up using the +module name as a function inside the server function (i.e. user-local session +scope. The function first arguments is string represents the module id (the same id used in module UI function). Additional arguments can be supplied by the user based on the specific shiny module that is called. There can be additional helper functions that are a part of a shiny module. The **downloadableTable** Shiny Module is a part of the *periscope* package and consists of the following functions: * **downloadableTableUI** - the UI function to place the table in the application -* **downloadableTable** - the Server function supplied to callModule. +* **downloadableTable** - the server function to be called inside server_local.R. ## downloadableTableUI @@ -99,13 +94,8 @@ downloadableTableUI("object_id1", ## downloadableTable -The **downloadableTable** function is not called directly - instead a call to -shiny::callModule is made inside the server.R (or equivalent) file to initialize -the module. +The **downloadableTable** function is called directly. The call consists of the following: -The call consists of the following: - -* the name of the module - unquoted * the unique object ID that was provided to downloadableTableUI when creating the UI object * the logging logger to be used @@ -138,7 +128,7 @@ to the user from the application. All the above requirements apply. **Reactive Return Value** -The callModule function returns a reactive expression containing the selected +The server function returns a reactive expression containing the selected rows (data, not references, rownumbers, etc - the actual table row data). This allows the user to capture this to update another table, chart, etc. as desired. It is acceptable to ignore the return value as well if this functionality is not @@ -148,14 +138,14 @@ needed. ```{r, eval = F} # Inside server_local.R -selectedrows <- callModule(downloadableTable, - "object_id1", - logger = ss_userAction.Log, - filenameroot = "mydownload1", - downloaddatafxns = list(csv = mydatafxn1, tsv = mydatafxn2), - tabledata = mydatafxn3, - rownames = FALSE, - caption = "This is a great table! By: Me" ) +selectedrows <- downloadableTable("object_id1", + logger = ss_userAction.Log, + filenameroot = "mydownload1", + downloaddatafxns = list(csv = mydatafxn1, + tsv = mydatafxn2), + tabledata = mydatafxn3, + rownames = FALSE, + caption = "This is a great table! By: Me" ) # selectedrows is the reactive return value, captured for later use ``` From f902babb1348288c3f0afbf252b7d7d88616c5e1 Mon Sep 17 00:00:00 2001 From: Mohammed Ali Date: Mon, 9 Aug 2021 23:47:42 -0700 Subject: [PATCH 2/2] updated unit tests --- tests/testthat/test_app_reset.R | 91 ++++++------------------ tests/testthat/test_body_footer.R | 26 +++---- tests/testthat/test_downloadable_table.R | 33 ++++----- 3 files changed, 45 insertions(+), 105 deletions(-) diff --git a/tests/testthat/test_app_reset.R b/tests/testthat/test_app_reset.R index 9fa3c89..09ea8e2 100755 --- a/tests/testthat/test_app_reset.R +++ b/tests/testthat/test_app_reset.R @@ -14,72 +14,35 @@ test_that("app_reset - no reset button", { }) test_that("app_reset - reset button - no pending", { - testServer(app_reset, - expr = { - suppressWarnings({ - session$setInputs(resetButton = TRUE, - resetPending = FALSE, - logger = periscope:::fw_get_user_log()) - - expect_null(session$getReturned()) - expect_silent(app_reset(input = list(resetButton = TRUE, resetPending = FALSE), - output = list(), - session = MockShinySession$setInputs(resetButton = TRUE, - resetPending = FALSE), - logger = periscope:::fw_get_user_log())) - }) - }) + expect_silent(app_reset(input = list(resetButton = TRUE, resetPending = FALSE), + output = list(), + session = MockShinySession$setInputs(resetButton = TRUE, + resetPending = FALSE), + logger = periscope:::fw_get_user_log())) }) test_that("app_reset - no reset button - with pending", { - testServer(app_reset, - expr = { - suppressWarnings({ - session$setInputs(resetButton = FALSE, - resetPending = TRUE, - logger = periscope:::fw_get_user_log()) - expect_null(session$getReturned()) - expect_silent(app_reset(input = list(resetButton = FALSE, resetPending = TRUE), - output = list(), - session = MockShinySession$setInputs(resetButton = TRUE, - resetPending = FALSE), - logger = periscope:::fw_get_user_log())) - }) - }) + expect_silent(app_reset(input = list(resetButton = FALSE, resetPending = TRUE), + output = list(), + session = MockShinySession$setInputs(resetButton = TRUE, + resetPending = FALSE), + logger = periscope:::fw_get_user_log())) }) test_that("app_reset - reset button - with pending", { - testServer(app_reset, - expr = { - suppressWarnings({ - session$setInputs(resetButton = TRUE, - resetPending = TRUE, - logger = periscope:::fw_get_user_log()) - expect_null(session$getReturned()) - expect_silent(app_reset(input = list(resetButton = TRUE, resetPending = TRUE), - output = list(), - session = MockShinySession$setInputs(resetButton = TRUE, - resetPending = FALSE), - logger = periscope:::fw_get_user_log())) - }) - }) + expect_silent(app_reset(input = list(resetButton = TRUE, resetPending = TRUE), + output = list(), + session = MockShinySession$setInputs(resetButton = TRUE, + resetPending = FALSE), + logger = periscope:::fw_get_user_log())) }) test_that("app_reset", { - testServer(app_reset, - expr = { - suppressWarnings({ - session$setInputs(resetButton = FALSE, - resetPending = FALSE, - logger = periscope:::fw_get_user_log()) - expect_null(session$getReturned()) - expect_silent(app_reset(input = list(resetButton = FALSE, resetPending = FALSE), - output = list(), - session = MockShinySession$setInputs(resetButton = TRUE, - resetPending = FALSE), - logger = periscope:::fw_get_user_log())) - }) - }) + expect_silent(app_reset(input = list(resetButton = FALSE, resetPending = FALSE), + output = list(), + session = MockShinySession$setInputs(resetButton = TRUE, + resetPending = FALSE), + logger = periscope:::fw_get_user_log())) }) test_that(".appReset", { @@ -92,17 +55,3 @@ test_that(".appReset", { expect_equal(class(reset)[[1]], "Observer") expect_equal(class(reset)[[2]], "R6") }) - -test_that("app_reset_old_style_call", { - testServer(app_reset, - expr = { - suppressWarnings({ - reset <- shiny::callModule(app_reset, - "reset", - logger = periscope:::fw_get_user_log()) - expect_equal(class(reset)[[1]], "Observer") - expect_equal(class(reset)[[2]], "R6") - }) - }) -}) - diff --git a/tests/testthat/test_body_footer.R b/tests/testthat/test_body_footer.R index 1b58500..e4a5ab4 100755 --- a/tests/testthat/test_body_footer.R +++ b/tests/testthat/test_body_footer.R @@ -25,23 +25,13 @@ test_that(".bodyFooter", { }) test_that("boody_footer", { - testServer(boody_footer, - expr = { - session$setInputs(logdata = data) - expect_silent(boody_footer) - }) + expect_silent(boody_footer(input = list(), + output = list(), + session = MockShinySession$new(), + logdata = data)) - testServer(boody_footer, - expr = { - session$setInputs(logdata = data2) - expect_silent(boody_footer) - }) -}) - -test_that("boody_footer2", { - testServer(boody_footer, - expr = { - session$setInputs(logdata = data2) - expect_silent(boody_footer) - }) + expect_silent(boody_footer(input = list(), + output = list(), + session = MockShinySession$new(), + logdata = data2)) }) diff --git a/tests/testthat/test_downloadable_table.R b/tests/testthat/test_downloadable_table.R index 36d16c4..2a4289b 100755 --- a/tests/testthat/test_downloadable_table.R +++ b/tests/testthat/test_downloadable_table.R @@ -18,21 +18,22 @@ mydataRowIds <- function(){ } - - test_that("downloadableTable", { - session <- MockShinySession$new() - session$setInputs(dtableSingleSelect = TRUE) - session$env$filenameroot <- "mydownload1" - session$env$downloaddatafxns = list(csv = data, tsv = data) - expect_silent(shiny::callModule(downloadableTable, - "download", - input = list(), - output = list(), - session = session, - logger = periscope:::fw_get_user_log(), - filenameroot = "mydownload1", - downloaddatafxns = list(csv = data, tsv = data), - tabledata = data, - selection = mydataRowIds)) + suppressWarnings({ + session <- MockShinySession$new() + session$setInputs(dtableSingleSelect = "FALSE") + session$env$filenameroot <- "mydownload1" + session$env$downloaddatafxns = list(csv = data, tsv = data) + expect_silent(shiny::callModule(downloadableTable, + "download", + input = list(dtableSingleSelect = "FALSE"), + output = list(), + session = session, + logger = periscope:::fw_get_user_log(), + filenameroot = "mydownload1", + downloaddatafxns = list(csv = data, tsv = data), + tabledata = data, + selection = mydataRowIds)) + }) + })