From 6c7a5f3cd995d159b3af1197c25c9a29b10f0930 Mon Sep 17 00:00:00 2001 From: Andrew Bates Date: Mon, 5 Dec 2022 14:19:22 -0800 Subject: [PATCH 1/3] add new function with_tooltip for adding tooltips to shiny tags. add explanatory tooltip for value input of arbitrary lines ui --- NEWS.md | 4 +++ R/utils-arbitrary_lines.r | 34 ++++++++++++++++++++- man/with_tooltip.Rd | 20 ++++++++++++ tests/testthat.R | 12 ++++++++ tests/testthat/test-utils-arbitrary_lines.r | 23 ++++++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 man/with_tooltip.Rd create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-utils-arbitrary_lines.r diff --git a/NEWS.md b/NEWS.md index e39741ae..b3a1f605 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,10 @@ # Examples now use `scda.2022` data instead of `scda.2021`. +### Enhancements + +* Added new function `with_tooltip` to add tooltips to `shiny` tags. + ### Breaking changes * Constraints range is calculated on the filtered data instead of the unfiltered. diff --git a/R/utils-arbitrary_lines.r b/R/utils-arbitrary_lines.r index a298e961..2b75781a 100644 --- a/R/utils-arbitrary_lines.r +++ b/R/utils-arbitrary_lines.r @@ -19,7 +19,10 @@ ui_arbitrary_lines <- function(id, line_arb, line_arb_label, line_arb_color, tit ns <- NS(id) div( tags$b(title), - textInput(ns("line_arb"), label = "Value:", value = paste(line_arb, collapse = ", ")), + with_tooltip( + textInput(ns("line_arb"), label = "Value:", value = paste(line_arb, collapse = ", ")), + title = "For multiple lines, supply a comma separated list of values." + ), textInput(ns("line_arb_label"), label = "Label:", value = paste(line_arb_label, collapse = ", ")), textInput(ns("line_arb_color"), label = "Color:", value = paste(line_arb_color, collapse = ", ")) ) @@ -75,6 +78,35 @@ srv_arbitrary_lines <- function(id) { }) } +#' Add a tooltip to an input tag +#' +#' +#' @param tag The input tag to place the tooltip on. +#' @param title The title (text content) of the tooltip. +#' @param placement Where to place the tooltip relative to the input tag. One of +#' 'top' (the default), 'bottom', 'left', 'right', or 'auto'. +#' @keywords internal +with_tooltip <- function(tag, title, placement = "top") { + checkmate::assert_class(tag, "shiny.tag") + checkmate::assert( + checkmate::check_string(placement), + checkmate::check_choice( + placement, + choices = c("top", "bottom", "left", "right", "auto") + ), + combine = "and" + ) + + id <- shiny::tagGetAttribute(tag, "id") + tag <- shiny::tagAppendAttributes( + tag, + `data-toggle` = "tooltip", + title = title + ) + + tag +} + # to check the arbitrary line arguments validate_line_arb_arg <- function(line_arb, line_arb_color, line_arb_label) { checkmate::assert_numeric(line_arb) diff --git a/man/with_tooltip.Rd b/man/with_tooltip.Rd new file mode 100644 index 00000000..94892784 --- /dev/null +++ b/man/with_tooltip.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-arbitrary_lines.r +\name{with_tooltip} +\alias{with_tooltip} +\title{Add a tooltip to an input tag} +\usage{ +with_tooltip(tag, title, placement = "top") +} +\arguments{ +\item{tag}{The input tag to place the tooltip on.} + +\item{title}{The title (text content) of the tooltip.} + +\item{placement}{Where to place the tooltip relative to the input tag. One of +'top' (the default), 'bottom', 'left', 'right', or 'auto'.} +} +\description{ +Add a tooltip to an input tag +} +\keyword{internal} diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 00000000..1b6d1e83 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,12 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/tests.html +# * https://testthat.r-lib.org/reference/test_package.html#special-files + +library(testthat) +library(teal.goshawk) + +test_check("teal.goshawk") diff --git a/tests/testthat/test-utils-arbitrary_lines.r b/tests/testthat/test-utils-arbitrary_lines.r new file mode 100644 index 00000000..d6e99cd1 --- /dev/null +++ b/tests/testthat/test-utils-arbitrary_lines.r @@ -0,0 +1,23 @@ +test_that("with_tooltip fails on bad args", { + + expect_error( + with_tooltip(shiny::textInput("id", "label")) + ) + + expect_error( + with_tooltip( + shiny::textInput("id", "label"), + title = "title", + placement = "front" + ) + ) + + expect_error( + with_tooltip( + shiny::textInput("id", "label"), + title = "title", + placement = c("front", "back") + ) + ) + +}) From ee4de2bcb635086c239b91ccfc751f5f4b475930 Mon Sep 17 00:00:00 2001 From: Andrew Bates Date: Tue, 6 Dec 2022 09:45:52 -0800 Subject: [PATCH 2/3] remove new with_tooltip function in favor of simpler method of using a CSS class provided by teal --- NEWS.md | 2 +- R/utils-arbitrary_lines.r | 45 +++++++-------------- man/with_tooltip.Rd | 20 --------- tests/testthat.R | 12 ------ tests/testthat/test-utils-arbitrary_lines.r | 23 ----------- 5 files changed, 15 insertions(+), 87 deletions(-) delete mode 100644 man/with_tooltip.Rd delete mode 100644 tests/testthat.R delete mode 100644 tests/testthat/test-utils-arbitrary_lines.r diff --git a/NEWS.md b/NEWS.md index b3a1f605..6bb83917 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,7 +4,7 @@ ### Enhancements -* Added new function `with_tooltip` to add tooltips to `shiny` tags. +* Added a tooltip to value input of `ui_arbitrary_lines` to explain how to supply multiple values. ### Breaking changes diff --git a/R/utils-arbitrary_lines.r b/R/utils-arbitrary_lines.r index 2b75781a..6d69cb0e 100644 --- a/R/utils-arbitrary_lines.r +++ b/R/utils-arbitrary_lines.r @@ -19,9 +19,20 @@ ui_arbitrary_lines <- function(id, line_arb, line_arb_label, line_arb_color, tit ns <- NS(id) div( tags$b(title), - with_tooltip( - textInput(ns("line_arb"), label = "Value:", value = paste(line_arb, collapse = ", ")), - title = "For multiple lines, supply a comma separated list of values." + textInput( + ns("line_arb"), + div( + class = "teal-tooltip", + tagList( + "Value:", + icon("circle-info"), + span( + class = "tooltiptext", + "For multiple lines, supply a comma separated list of values." + ) + ) + ), + value = paste(line_arb, collapse = ", ") ), textInput(ns("line_arb_label"), label = "Label:", value = paste(line_arb_label, collapse = ", ")), textInput(ns("line_arb_color"), label = "Color:", value = paste(line_arb_color, collapse = ", ")) @@ -78,34 +89,6 @@ srv_arbitrary_lines <- function(id) { }) } -#' Add a tooltip to an input tag -#' -#' -#' @param tag The input tag to place the tooltip on. -#' @param title The title (text content) of the tooltip. -#' @param placement Where to place the tooltip relative to the input tag. One of -#' 'top' (the default), 'bottom', 'left', 'right', or 'auto'. -#' @keywords internal -with_tooltip <- function(tag, title, placement = "top") { - checkmate::assert_class(tag, "shiny.tag") - checkmate::assert( - checkmate::check_string(placement), - checkmate::check_choice( - placement, - choices = c("top", "bottom", "left", "right", "auto") - ), - combine = "and" - ) - - id <- shiny::tagGetAttribute(tag, "id") - tag <- shiny::tagAppendAttributes( - tag, - `data-toggle` = "tooltip", - title = title - ) - - tag -} # to check the arbitrary line arguments validate_line_arb_arg <- function(line_arb, line_arb_color, line_arb_label) { diff --git a/man/with_tooltip.Rd b/man/with_tooltip.Rd deleted file mode 100644 index 94892784..00000000 --- a/man/with_tooltip.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils-arbitrary_lines.r -\name{with_tooltip} -\alias{with_tooltip} -\title{Add a tooltip to an input tag} -\usage{ -with_tooltip(tag, title, placement = "top") -} -\arguments{ -\item{tag}{The input tag to place the tooltip on.} - -\item{title}{The title (text content) of the tooltip.} - -\item{placement}{Where to place the tooltip relative to the input tag. One of -'top' (the default), 'bottom', 'left', 'right', or 'auto'.} -} -\description{ -Add a tooltip to an input tag -} -\keyword{internal} diff --git a/tests/testthat.R b/tests/testthat.R deleted file mode 100644 index 1b6d1e83..00000000 --- a/tests/testthat.R +++ /dev/null @@ -1,12 +0,0 @@ -# This file is part of the standard setup for testthat. -# It is recommended that you do not modify it. -# -# Where should you do additional test configuration? -# Learn more about the roles of various files in: -# * https://r-pkgs.org/tests.html -# * https://testthat.r-lib.org/reference/test_package.html#special-files - -library(testthat) -library(teal.goshawk) - -test_check("teal.goshawk") diff --git a/tests/testthat/test-utils-arbitrary_lines.r b/tests/testthat/test-utils-arbitrary_lines.r deleted file mode 100644 index d6e99cd1..00000000 --- a/tests/testthat/test-utils-arbitrary_lines.r +++ /dev/null @@ -1,23 +0,0 @@ -test_that("with_tooltip fails on bad args", { - - expect_error( - with_tooltip(shiny::textInput("id", "label")) - ) - - expect_error( - with_tooltip( - shiny::textInput("id", "label"), - title = "title", - placement = "front" - ) - ) - - expect_error( - with_tooltip( - shiny::textInput("id", "label"), - title = "title", - placement = c("front", "back") - ) - ) - -}) From ace31ca802ca104b5a0982a1092e9a95062e1962 Mon Sep 17 00:00:00 2001 From: Andrew Bates Date: Tue, 6 Dec 2022 09:46:30 -0800 Subject: [PATCH 3/3] update WORDLIST --- inst/WORDLIST | 1 + 1 file changed, 1 insertion(+) diff --git a/inst/WORDLIST b/inst/WORDLIST index 507bacdb..65552ec9 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -53,6 +53,7 @@ roche toggable tomlinsj tomlinson +tooltip toth tothb trt