diff --git a/R/get_dt_rows.R b/R/get_dt_rows.R index 778575f6..c7c07b50 100644 --- a/R/get_dt_rows.R +++ b/R/get_dt_rows.R @@ -7,21 +7,33 @@ #' @name get_dt_rows #' #' @examples -#' \dontrun{ #' library(shiny) -#' # in Shiny UI function -#' tagList( -#' get_dt_rows(ns("data_table"), ns("dt_rows")), -#' ... -#' ) +#' ui <- function(id) { +#' ns <- NS(id) +#' tagList( +#' DT::DTOutput(ns("data_table")), +#' get_dt_rows(ns("data_table"), ns("dt_rows")) +#' ) +#' } #' #' # use the input$dt_rows in the Shiny Server function -#' if (!is.null(input$dt_rows)) { -#' dt_args$options$pageLength <- input$dt_rows -#' } # nolint -#' do.call(DT::datatable, dt_args) +#' server <- function(id) { +#' moduleServer(id, function(input, output, session) { +#' output$data_table <- DT::renderDataTable( +#' { +#' iris +#' }, +#' options = list(pageLength = input$dt_rows) +#' ) +#' }) #' } #' +#' if (interactive()) { +#' shinyApp( +#' ui = ui("my_table_module"), +#' server = function(input, output, session) server("my_table_module") +#' ) +#' } #' @export get_dt_rows <- function(dt_name, dt_rows) { tags$head( diff --git a/R/optionalInput.R b/R/optionalInput.R index 56a29098..e74a3719 100644 --- a/R/optionalInput.R +++ b/R/optionalInput.R @@ -26,6 +26,9 @@ #' @param width (`character(1)`)\cr #' The width of the input passed to `pickerInput` e.g. `'auto'`, `'fit'`, `'100px'` or `'75%'` #' +#' @return (`shiny.tag`) HTML tag with `pickerInput` widget and +#' non-interactive element listing selected values. +#' #' @export #' #' @examples @@ -471,6 +474,8 @@ extract_raw_choices <- function(choices, sep) { #' an object of class `shiny.tag`. E.g. an object returned by [shiny::helpText()] #' @param ... optional arguments to `sliderInput` #' +#' @return (`shiny.tag`) HTML tag with `sliderInput` widget. +#' #' @export #' #' @examples @@ -532,6 +537,8 @@ optionalSliderInput <- function(inputId, label, min, max, value, label_help = NU #' Otherwise, if it is of length three the three elements will map to `value`, `min` and `max` of #' the [optionalSliderInput()] function. #' +#' @return (`shiny.tag`) HTML tag with range `sliderInput` widget. +#' #' @export #' #' @examples diff --git a/R/plot_with_settings.R b/R/plot_with_settings.R index aa0fabc7..8a823f4c 100644 --- a/R/plot_with_settings.R +++ b/R/plot_with_settings.R @@ -1,9 +1,5 @@ -#' UI part of plot-with-settings module -#' -#' @description `r lifecycle::badge("stable")` +#' @rdname plot_with_settings #' @export -#' -#' @param id (\code{character}) module ID plot_with_settings_ui <- function(id) { checkmate::assert_string(id) @@ -62,12 +58,16 @@ plot_with_settings_ui <- function(id) { ) } -#' Server part of plot-with-settings module +#' Plot-with-settings module #' +#' @rdname plot_with_settings #' @description `r lifecycle::badge("stable")` +#' Universal module for plots with settings for height, width, and download. +#' #' @export #' #' @inheritParams shiny::moduleServer +#' #' @param plot_r (`reactive` or `function`)\cr #' `reactive` expression or a simple `function` to draw a plot. #' A simple `function` is needed e.g. for base plots like `plot(1)` as the output can not be caught when downloading. @@ -107,10 +107,9 @@ plot_with_settings_ui <- function(id) { #' If an invalid value is set then the default value is used and a warning is outputted to the console. #' #' @examples -#' \dontrun{ #' # Example using a reactive as input to plot_r #' library(shiny) -#' shinyApp( +#' app1 <- shinyApp( #' ui = fluidPage( #' plot_with_settings_ui( #' id = "plot_with_settings" @@ -130,8 +129,12 @@ plot_with_settings_ui <- function(id) { #' } #' ) #' +#' if (interactive()) { +#' runApp(app1) +#' } +#' #' # Example using a function as input to plot_r -#' shinyApp( +#' app2 <- shinyApp( #' ui = fluidPage( #' radioButtons("download_option", "Select the Option", list("ggplot", "trellis", "grob", "base")), #' plot_with_settings_ui( @@ -167,8 +170,12 @@ plot_with_settings_ui <- function(id) { #' } #' ) #' +#' if (interactive()) { +#' runApp(app2) +#' } +#' #' # Example with brushing/hovering/clicking/double-clicking -#' shinyApp( +#' app3 <- shinyApp( #' ui = fluidPage( #' plot_with_settings_ui( #' id = "plot_with_settings" @@ -202,10 +209,14 @@ plot_with_settings_ui <- function(id) { #' } #' ) #' +#' if (interactive()) { +#' runApp(app3) +#' } +#' #' # Example which allows module to be hidden/shown #' library("shinyjs") #' -#' shinyApp( +#' app4 <- shinyApp( #' ui = fluidPage( #' useShinyjs(), #' actionButton("button", "Show/Hide"), @@ -229,7 +240,11 @@ plot_with_settings_ui <- function(id) { #' ) #' } #' ) +#' +#' if (interactive()) { +#' runApp(app4) #' } +#' plot_with_settings_srv <- function(id, plot_r, height = c(600, 200, 2000), diff --git a/R/table_with_settings.R b/R/table_with_settings.R index adf29c13..1b7e0a2d 100644 --- a/R/table_with_settings.R +++ b/R/table_with_settings.R @@ -45,11 +45,10 @@ table_with_settings_ui <- function(id, ...) { #' @export #' #' @examples -#' \dontrun{ #' library(shiny) #' library(rtables) #' library(magrittr) -#' shinyApp( +#' app <- shinyApp( #' ui = fluidPage( #' table_with_settings_ui( #' id = "table_with_settings" @@ -69,6 +68,8 @@ table_with_settings_ui <- function(id, ...) { #' table_with_settings_srv(id = "table_with_settings", table_r = table_r) #' } #' ) +#' if (interactive()) { +#' runApp(app) #' } #' table_with_settings_srv <- function(id, table_r, show_hide_signal = reactive(TRUE)) { diff --git a/man/get_dt_rows.Rd b/man/get_dt_rows.Rd index 671be21d..1ada3f61 100644 --- a/man/get_dt_rows.Rd +++ b/man/get_dt_rows.Rd @@ -15,19 +15,31 @@ get_dt_rows(dt_name, dt_rows) \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}} } \examples{ -\dontrun{ library(shiny) -# in Shiny UI function -tagList( - get_dt_rows(ns("data_table"), ns("dt_rows")), - ... -) +ui <- function(id) { + ns <- NS(id) + tagList( + DT::DTOutput(ns("data_table")), + get_dt_rows(ns("data_table"), ns("dt_rows")) + ) +} # use the input$dt_rows in the Shiny Server function -if (!is.null(input$dt_rows)) { - dt_args$options$pageLength <- input$dt_rows -} # nolint -do.call(DT::datatable, dt_args) +server <- function(id) { + moduleServer(id, function(input, output, session) { + output$data_table <- DT::renderDataTable( + { + iris + }, + options = list(pageLength = input$dt_rows) + ) + }) } +if (interactive()) { + shinyApp( + ui = ui("my_table_module"), + server = function(input, output, session) server("my_table_module") + ) +} } diff --git a/man/optionalSelectInput.Rd b/man/optionalSelectInput.Rd index 6d333ff4..41e935d3 100644 --- a/man/optionalSelectInput.Rd +++ b/man/optionalSelectInput.Rd @@ -63,6 +63,10 @@ The width of the input passed to \code{pickerInput} e.g. \code{'auto'}, \code{' \item{session}{(\code{shiny.session})\cr} } +\value{ +(\code{shiny.tag}) HTML tag with \code{pickerInput} widget and +non-interactive element listing selected values. +} \description{ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}} Wrapper for \code{\link[shinyWidgets:pickerInput]{shinyWidgets::pickerInput()}} with additional features. diff --git a/man/optionalSliderInput.Rd b/man/optionalSliderInput.Rd index 851e6dff..cbdf2200 100644 --- a/man/optionalSliderInput.Rd +++ b/man/optionalSliderInput.Rd @@ -24,6 +24,9 @@ an object of class \code{shiny.tag}. E.g. an object returned by \code{\link[shin \item{...}{optional arguments to \code{sliderInput}} } +\value{ +(\code{shiny.tag}) HTML tag with \code{sliderInput} widget. +} \description{ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}} Hidden input widgets are useful to have the \code{input[[inputId]]} variable diff --git a/man/optionalSliderInputValMinMax.Rd b/man/optionalSliderInputValMinMax.Rd index 5f6aef52..4c89363b 100644 --- a/man/optionalSliderInputValMinMax.Rd +++ b/man/optionalSliderInputValMinMax.Rd @@ -28,6 +28,9 @@ an object of class \code{shiny.tag}. E.g. an object returned by \code{\link[shin \item{...}{optional arguments to \code{sliderInput}} } +\value{ +(\code{shiny.tag}) HTML tag with range \code{sliderInput} widget. +} \description{ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}} The \code{\link[=optionalSliderInput]{optionalSliderInput()}} function needs three arguments to determine diff --git a/man/plot_with_settings_srv.Rd b/man/plot_with_settings.Rd similarity index 94% rename from man/plot_with_settings_srv.Rd rename to man/plot_with_settings.Rd index 29afcf8e..89ecff83 100644 --- a/man/plot_with_settings_srv.Rd +++ b/man/plot_with_settings.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/plot_with_settings.R -\name{plot_with_settings_srv} +\name{plot_with_settings_ui} +\alias{plot_with_settings_ui} \alias{plot_with_settings_srv} -\title{Server part of plot-with-settings module} +\title{Plot-with-settings module} \usage{ +plot_with_settings_ui(id) + plot_with_settings_srv( id, plot_r, @@ -65,6 +68,7 @@ the main page.} } \description{ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}} +Universal module for plots with settings for height, width, and download. } \details{ By default the plot is rendered with \verb{72 dpi}. In order to change this, to for example 96 set @@ -72,10 +76,9 @@ By default the plot is rendered with \verb{72 dpi}. In order to change this, to If an invalid value is set then the default value is used and a warning is outputted to the console. } \examples{ -\dontrun{ # Example using a reactive as input to plot_r library(shiny) -shinyApp( +app1 <- shinyApp( ui = fluidPage( plot_with_settings_ui( id = "plot_with_settings" @@ -95,8 +98,12 @@ shinyApp( } ) +if (interactive()) { + runApp(app1) +} + # Example using a function as input to plot_r -shinyApp( +app2 <- shinyApp( ui = fluidPage( radioButtons("download_option", "Select the Option", list("ggplot", "trellis", "grob", "base")), plot_with_settings_ui( @@ -132,8 +139,12 @@ shinyApp( } ) +if (interactive()) { + runApp(app2) +} + # Example with brushing/hovering/clicking/double-clicking -shinyApp( +app3 <- shinyApp( ui = fluidPage( plot_with_settings_ui( id = "plot_with_settings" @@ -167,10 +178,14 @@ shinyApp( } ) +if (interactive()) { + runApp(app3) +} + # Example which allows module to be hidden/shown library("shinyjs") -shinyApp( +app4 <- shinyApp( ui = fluidPage( useShinyjs(), actionButton("button", "Show/Hide"), @@ -194,5 +209,9 @@ shinyApp( ) } ) + +if (interactive()) { + runApp(app4) } + } diff --git a/man/plot_with_settings_ui.Rd b/man/plot_with_settings_ui.Rd deleted file mode 100644 index 2dd0fa07..00000000 --- a/man/plot_with_settings_ui.Rd +++ /dev/null @@ -1,14 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/plot_with_settings.R -\name{plot_with_settings_ui} -\alias{plot_with_settings_ui} -\title{UI part of plot-with-settings module} -\usage{ -plot_with_settings_ui(id) -} -\arguments{ -\item{id}{(\code{character}) module ID} -} -\description{ -\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}} -} diff --git a/man/table_with_settings.Rd b/man/table_with_settings.Rd index 0e689958..3a9c3534 100644 --- a/man/table_with_settings.Rd +++ b/man/table_with_settings.Rd @@ -27,11 +27,10 @@ a mechanism to allow modules which call this module to show/hide the table_with_ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}} } \examples{ -\dontrun{ library(shiny) library(rtables) library(magrittr) -shinyApp( +app <- shinyApp( ui = fluidPage( table_with_settings_ui( id = "table_with_settings" @@ -51,6 +50,8 @@ shinyApp( table_with_settings_srv(id = "table_with_settings", table_r = table_r) } ) +if (interactive()) { + runApp(app) } }