Skip to content

Commit 9518e72

Browse files
committed
Inserted code and tests
1 parent 4a25503 commit 9518e72

15 files changed

+217
-3
lines changed

DESCRIPTION

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ License: MIT + file LICENSE
77
Imports:
88
config (>= 0.3.2),
99
golem (>= 0.4.1),
10+
graphics,
1011
shiny (>= 1.8.1.1)
1112
Encoding: UTF-8
1213
LazyData: true
13-
RoxygenNote: 7.1.1
14+
RoxygenNote: 7.3.1
1415
URL: https://github.com/guidomaggioorg/shinyfaithful
1516
BugReports: https://github.com/guidomaggioorg/shinyfaithful/issues
17+
Suggests:
18+
spelling,
19+
testthat (>= 3.0.0)
20+
Config/testthat/edition: 3
21+
Language: en-US

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ importFrom(golem,add_resource_path)
77
importFrom(golem,bundle_resources)
88
importFrom(golem,favicon)
99
importFrom(golem,with_golem_options)
10+
importFrom(graphics,hist)
1011
importFrom(shiny,shinyApp)

R/app_server.R

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
#' @noRd
77
app_server <- function(input, output, session) {
88
# Your application server logic
9+
mod_faithful_hist_server("hist")
910
}

R/app_ui.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ app_ui <- function(request) {
1010
golem_add_external_resources(),
1111
# Your application UI logic
1212
fluidPage(
13-
h1("shinyfaithful")
13+
h1("Old Faithful Geyser - Hist"),
14+
mod_faithful_hist_ui("hist")
1415
)
1516
)
1617
}

R/mod_faithful_hist.R

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#' Old Faithful histogram Shiny module
2+
#'
3+
#' @description UI and server function for an example Shiny module.
4+
#'
5+
#' @param id Internal parameter for {shiny}.
6+
#'
7+
#' @name mod_faithful_hist
8+
#'
9+
NULL
10+
11+
#' @rdname mod_faithful_hist
12+
#'
13+
#' @import shiny
14+
mod_faithful_hist_ui <- function(id) {
15+
ns <- NS(id)
16+
tagList(
17+
fluidRow(
18+
column(3,
19+
sliderInput(ns("bins"), "Number of bins:", min = 1, max = 50, value = 30),
20+
checkboxInput(ns("density"), label = "density")
21+
),
22+
# Show a plot of the generated distribution
23+
column(9,
24+
plotOutput(ns("distPlot"))
25+
)
26+
)
27+
)
28+
}
29+
30+
#' @rdname mod_faithful_hist
31+
#'
32+
#' @import shiny
33+
mod_faithful_hist_server <- function(id){
34+
moduleServer( id, function(input, output, session){
35+
ns <- session$ns
36+
# generate bins based on input$bins from ui.R
37+
variable <- "waiting"
38+
x <- datasets::faithful[, variable, drop = FALSE]
39+
bins <- reactive(seq(min(x), max(x), length.out = input$bins + 1))
40+
41+
output$distPlot <- renderPlot({
42+
# draw the histogram with the specified number of bins
43+
plot_hist(x = x, breaks = bins(), freq = !input$density)
44+
})
45+
})
46+
}
47+
48+
## To be copied in the UI
49+
# mod_faithful_hist_ui("faithful_hist_1")
50+
51+
## To be copied in the server
52+
# mod_faithful_hist_server("faithful_hist_1")

R/plot_hist.R

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#' plot histogram
2+
#' @description function to plot histogram
3+
#'
4+
#' @param x data.frame to plot
5+
#' @param breaks a vector giving the breakpoints between histogram cells, as of graphics::hist
6+
#' @param freq logical for plotting frequencies, as of graphics::hist
7+
#'
8+
#' @importFrom graphics hist
9+
plot_hist <- function(x, breaks, freq){
10+
variable <- names(x)[1] # take first variable
11+
hist(
12+
x[,variable], breaks = breaks, freq = freq,
13+
main = paste("Histogram of", variable),
14+
col = "darkgray", border = "white", xlab = variable
15+
)
16+
}

inst/WORDLIST

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
golem
2+
th

man/figures/README-pressure-1.png

-6.05 KB
Binary file not shown.

man/mod_faithful_hist.Rd

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_hist.Rd

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/run_app.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/spelling.R

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if(requireNamespace('spelling', quietly = TRUE))
2+
spelling::spell_check_test(vignettes = TRUE, error = FALSE,
3+
skip_on_cran = TRUE)

tests/testthat.R

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is part of the standard setup for testthat.
2+
# It is recommended that you do not modify it.
3+
#
4+
# Where should you do additional test configuration?
5+
# Learn more about the roles of various files in:
6+
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
7+
# * https://testthat.r-lib.org/articles/special-files.html
8+
9+
library(testthat)
10+
library(shinyfaithful)
11+
12+
test_check("shinyfaithful")
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
test_that("app ui", {
2+
ui <- app_ui()
3+
golem::expect_shinytaglist(ui)
4+
# Check that formals have not been removed
5+
fmls <- formals(app_ui)
6+
for (i in c("request")) {
7+
expect_true(i %in% names(fmls))
8+
}
9+
})
10+
11+
test_that("app server", {
12+
server <- app_server
13+
expect_type(server, "closure")
14+
# Check that formals have not been removed
15+
fmls <- formals(app_server)
16+
for (i in c("input", "output", "session")) {
17+
expect_true(i %in% names(fmls))
18+
}
19+
})
20+
21+
test_that(
22+
"app_sys works",
23+
{
24+
expect_true(
25+
app_sys("golem-config.yml") != ""
26+
)
27+
}
28+
)
29+
30+
test_that(
31+
"golem-config works",
32+
{
33+
config_file <- app_sys("golem-config.yml")
34+
skip_if(config_file == "")
35+
36+
expect_true(
37+
get_golem_config(
38+
"app_prod",
39+
config = "production",
40+
file = config_file
41+
)
42+
)
43+
expect_false(
44+
get_golem_config(
45+
"app_prod",
46+
config = "dev",
47+
file = config_file
48+
)
49+
)
50+
}
51+
)
52+
53+
# Configure this test to fit your need.
54+
# testServer() function makes it possible to test code in server functions and modules, without needing to run the full Shiny application
55+
testServer(app_server, {
56+
57+
# Set and test an input
58+
session$setInputs(x = 2)
59+
expect_equal(input$x, 2)
60+
61+
# Example of tests you can do on the server:
62+
# - Checking reactiveValues
63+
# expect_equal(r$lg, 'EN')
64+
# - Checking output
65+
# expect_equal(output$txt, "Text")
66+
})
67+
68+
# Configure this test to fit your need
69+
test_that(
70+
"app launches",
71+
{
72+
golem::expect_running(sleep = 5)
73+
}
74+
)

tests/testthat/test-plot_hist.R

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
test_that("plot_hist produces a plot", {
2+
bins <- 30
3+
x <- datasets::faithful[,"waiting", drop = FALSE]
4+
p <- plot_hist(x = x,
5+
breaks = seq(min(x), max(x), length.out = bins + 1),
6+
freq = TRUE)
7+
expect_visible(p)
8+
expect_type(p, "list")
9+
expect_s3_class(p, "histogram")
10+
})

0 commit comments

Comments
 (0)