Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre-release tm_front_page document update #654

Merged
merged 6 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 66 additions & 58 deletions R/tm_front_page.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#' Front page module
#'
#' @description This `teal` module creates a simple front page for `teal` applications
#' Creates a simple front page for `teal` applications, displaying
#' introductory text, tables, additional `html` or `shiny` tags, and footnotes.
#'
#' @inheritParams teal::module
#' @param header_text `character vector` text to be shown at the top of the module, for each
#' element, if named the name is shown first in bold as a header followed by the value. The first
#' element's header is displayed larger than the others
#' @param tables `named list of dataframes` tables to be shown in the module
#' @param additional_tags `shiny.tag.list` or `html` additional shiny tags or `html` to be included after the table,
#' for example to include an image, `tagList(tags$img(src = "image.png"))` or to include further `html`,
#' `HTML("html text here")`
#' @param footnotes `character vector` text to be shown at the bottom of the module, for each
#' element, if named the name is shown first in bold, followed by the value
#' @param show_metadata `logical` should the metadata of the datasets be available on the module?
#' @return A `teal` module to be used in `teal` applications
#' @export
#' @param header_text (`character` vector) text to be shown at the top of the module, for each
#' element, if named the name is shown first in bold as a header followed by the value. The first
#' element's header is displayed larger than the others.
#' @param tables (`named list` of `data.frame`s) tables to be shown in the module.
#' @param additional_tags (`shiny.tag.list` or `html`) additional shiny tags or `html` to be included after the table,
#' for example to include an image, `tagList(tags$img(src = "image.png"))` or to include further `html`,
#' `HTML("html text here")`.
#' @param footnotes (`character` vector) of text to be shown at the bottom of the module, for each
#' element, if named the name is shown first in bold, followed by the value.
#' @param show_metadata (`logical`) indicating whether the metadata of the datasets be available on the module.
#'
#' @examples
#'
#' data <- teal_data()
Expand All @@ -37,9 +37,9 @@
#' "Table 3" = table_3
#' )
#'
#' app <- teal::init(
#' app <- init(
#' data = data,
#' modules = teal::modules(
#' modules = modules(
#' teal.modules.general::tm_front_page(
#' header_text = c(
#' "Important information" = "It can go here.",
Expand All @@ -54,9 +54,13 @@
#' header = tags$h1("Sample Application"),
#' footer = tags$p("Application footer"),
#' )
#'
#' if (interactive()) {
#' shinyApp(app$ui, app$server)
#' }
#'
#' @export
#'
tm_front_page <- function(label = "Front page",
header_text = character(0),
tables = list(),
Expand All @@ -83,6 +87,7 @@ tm_front_page <- function(label = "Front page",
)
}

# UI function for the front page module
ui_front_page <- function(id, ...) {
args <- list(...)
ns <- NS(id)
Expand Down Expand Up @@ -121,6 +126,52 @@ ui_front_page <- function(id, ...) {
)
}

# Server function for the front page module
srv_front_page <- function(id, data, tables, show_metadata) {
checkmate::assert_class(data, "reactive")
checkmate::assert_class(isolate(data()), "teal_data")
moduleServer(id, function(input, output, session) {
ns <- session$ns

lapply(seq_along(tables), function(idx) {
output[[paste0("table_", idx)]] <- renderTable(
tables[[idx]],
bordered = TRUE,
caption = names(tables)[idx],
caption.placement = "top"
)
})

if (show_metadata) {
observeEvent(
input$metadata_button, showModal(
modalDialog(
title = "Metadata",
dataTableOutput(ns("metadata_table")),
size = "l",
easyClose = TRUE
)
)
)

metadata_data_frame <- reactive({
datanames <- teal.data::datanames(data())
convert_metadata_to_dataframe(
lapply(datanames, function(dataname) attr(data()[[dataname]], "metadata")),
datanames
)
})

output$metadata_table <- renderDataTable({
validate(need(nrow(metadata_data_frame()) > 0, "The data has no associated metadata"))
metadata_data_frame()
})
}
})
}

## utils functions

get_header_tags <- function(header_text) {
if (length(header_text) == 0) {
return(list())
Expand Down Expand Up @@ -167,49 +218,6 @@ get_footer_tags <- function(footnotes) {
}, bold_text = bold_texts, value = footnotes)
}

srv_front_page <- function(id, data, tables, show_metadata) {
checkmate::assert_class(data, "reactive")
checkmate::assert_class(isolate(data()), "teal_data")
moduleServer(id, function(input, output, session) {
ns <- session$ns

lapply(seq_along(tables), function(idx) {
output[[paste0("table_", idx)]] <- renderTable(
tables[[idx]],
bordered = TRUE,
caption = names(tables)[idx],
caption.placement = "top"
)
})

if (show_metadata) {
observeEvent(
input$metadata_button, showModal(
modalDialog(
title = "Metadata",
dataTableOutput(ns("metadata_table")),
size = "l",
easyClose = TRUE
)
)
)

metadata_data_frame <- reactive({
datanames <- teal.data::datanames(data())
convert_metadata_to_dataframe(
lapply(datanames, function(dataname) attr(data()[[dataname]], "metadata")),
datanames
)
})

output$metadata_table <- renderDataTable({
validate(need(nrow(metadata_data_frame()) > 0, "The data has no associated metadata"))
metadata_data_frame()
})
}
})
}

# take a list of metadata, one item per dataset (raw_metadata each element from datasets$get_metadata())
# and the corresponding datanames and output a data.frame with columns {Dataset, Name, Value}.
# which are, the Dataset the metadata came from, the metadata's name and value
Expand Down
28 changes: 14 additions & 14 deletions man/tm_front_page.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.