Skip to content

Commit

Permalink
Update justgage; make gauge() work better with bslib themes (#301)
Browse files Browse the repository at this point in the history
* Use github actions; change maintainer to Carson

* Update justgage to 1.4.0; allow it to be themed via bslib

* add bslib to imports

* Remove smart argument -- it was removed in rmarkdown 2.2

* Update news

* require rmarkdown 2.2

* remove debugger

* code review

* Make gaugeColor slightly closer to bg color (closer to the default semantics)

* Tweak labelFontColor, too
  • Loading branch information
cpsievert authored Feb 9, 2021
1 parent d2ed65b commit 4fc48b8
Show file tree
Hide file tree
Showing 11 changed files with 1,362 additions and 1,105 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ README.R?md
^inst/examples/.+\.Rnb\.cached
^examples/*

^\.github$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
116 changes: 116 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# NOTE: This workflow is overkill for most R packages
# check-standard.yaml is likely a better choice
# usethis::use_github_action("check-standard") will install it.
#
# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag.
# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions
on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: macOS-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: windows-latest, r: '3.6'}
# We explicitly set the user agent for R devel to the current release version of R so RSPM serves the release binaries.
- {os: ubuntu-16.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest", http-user-agent: "R/4.0.0 (ubuntu-16.04) R (4.0.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" }
- {os: ubuntu-16.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
- {os: ubuntu-16.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
- {os: ubuntu-16.04, r: '3.5', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
- {os: ubuntu-16.04, r: '3.4', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
- {os: ubuntu-16.04, r: '3.3', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v1
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}

- uses: r-lib/actions/setup-pandoc@v1

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Cache R packages
uses: actions/cache@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

- name: Install system dependencies
if: runner.os == 'Linux'
run: |
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "16.04"))')
- name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("rcmdcheck")
shell: Rscript {0}

- name: Session info
run: |
options(width = 100)
pkgs <- installed.packages()[, "Package"]
sessioninfo::session_info(pkgs, include_base = TRUE)
shell: Rscript {0}

- name: Check
env:
_R_CHECK_CRAN_INCOMING_: false
run: |
options(crayon.enabled = TRUE)
rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
shell: Rscript {0}

- name: Show testthat output
if: always()
run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check

- name: Fix path for Windows caching
if: runner.os == 'Windows'
# This is needed because if you use the default tar at this stage,
# C:/Rtools/bin/tar.exe, it will say that it can't find gzip.exe. So
# we'll just set the path so that the original tar that would be
# found, will be found.
run: echo "C:/Program Files/Git/usr/bin" >> $GITHUB_PATH
16 changes: 13 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Description: Format for converting an R Markdown document to a grid oriented
dashboard. The dashboard flexibly adapts the size of it's components to the
containing web page.
Authors@R: c(
person("Richard", "Iannone", role = c("aut", "cre"), email = "rich@rstudio.com",
person("Carson", "Sievert", role = c("aut", "cre"),
email = "carson@rstudio.com", comment = c(ORCID = "0000-0002-4958-2844")),
person("Richard", "Iannone", role = "aut", email = "rich@rstudio.com",
comment = c(ORCID = "0000-0003-3925-190X")),
person("JJ", "Allaire", role = "aut", email = "jj@rstudio.com"),
person("Barbara", "Borges", role = c("aut"), email = "barb.b.ribeiro@gmail.com"),
Expand All @@ -21,7 +23,6 @@ Authors@R: c(
person("Bojan", "Djuricic", role = c("ctb", "cph"), comment = "JustGage"),
person("Tomas", "Sardyha", role = c("ctb", "cph"), comment = "Sly"),
person("Bryan", "Lewis", role = c("ctb", "cph"), comment = "Examples"),
person("Carson", "Sievert", role = c("ctb", "cph"), comment = "Examples"),
person("Joshua", "Kunst", role = c("ctb", "cph"), comment = "Examples"),
person("Ryan", "Hafen", role = c("ctb", "cph"), comment = "Examples"),
person("Bob", "Rudis", role = c("ctb", "cph"), comment = "Examples"),
Expand All @@ -31,7 +32,16 @@ URL: http://rmarkdown.rstudio.com/flexdashboard
BugReports: https://github.com/rstudio/flexdashboard/issues
Encoding: UTF-8
Depends: R (>= 3.0.2)
Imports: tools, jsonlite, htmltools, knitr (>= 1.13), htmlwidgets (>= 0.6), rmarkdown (>= 1.10.3), shiny (>= 0.13)
Imports:
tools,
jsonlite,
htmltools,
knitr (>= 1.13),
htmlwidgets (>= 0.6),
rmarkdown (>= 2.2),
shiny (>= 0.13),
scales,
bslib
Suggests: testthat
LazyData: TRUE
License: MIT + file LICENSE
Expand Down
11 changes: 10 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
flexdashboard 0.5.2.9000
===========

* Added padding to the top of the sidebar. (#294)
### Possibly breaking changes

* The `smart` argument was removed from `flexdashboard::flex_dashboard` since it was removed in rmarkdown 2.2 (relatedly, we now require rmarkdown 2.2 or higher). (#301)

### Improvements & fixes

* Closed #300: When a `{bslib}` theme is relevant, `gauge()` now derives it's default styles from it. (#301)

* Closed #297, #254: `gauge()` now bundles justgage.js version 1.4.0, allowing labels to be updated via `renderGauge()`. (#301)

* Added padding to the top of the sidebar. (#294)

flexdashboard 0.5.2
===========
Expand Down
3 changes: 1 addition & 2 deletions R/flex_dashboard.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ flex_dashboard <- function(fig_width = 6.0,
fig_retina = 2,
fig_mobile = TRUE,
dev = "png",
smart = TRUE,
self_contained = TRUE,
favicon = NULL,
logo = NULL,
Expand Down Expand Up @@ -435,7 +434,7 @@ flex_dashboard <- function(fig_width = 6.0,
pre_knit = pre_knit,
pre_processor = pre_processor,
on_exit = on_exit,
base_format = html_document_base(smart = smart, theme = theme,
base_format = html_document_base(theme = theme,
self_contained = self_contained,
lib_dir = lib_dir, mathjax = mathjax,
template = "default",
Expand Down
26 changes: 23 additions & 3 deletions R/gauge.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,27 @@ gauge <- function(value, min, max, sectors = gaugeSectors(),
name = 'gauge',
x,
package = 'flexdashboard',
dependencies = rmarkdown::html_dependency_jquery()
dependencies = rmarkdown::html_dependency_jquery(),
preRenderHook = function(widget) {
theme <- bslib::bs_current_theme()
if (is.null(theme)) {
return(widget)
}

vars <- bslib::bs_get_variables(theme, c("bg", "fg", "font-family-base"))
gray_pal <- scales::colour_ramp(
htmltools::parseCssColors(vars[c("bg", "fg")])
)
defaults <- list(
gaugeColor = gray_pal(0.1),
valueFontColor = gray_pal(0.9),
labelFontColor = gray_pal(0.65),
valueFontFamily = vars[["font-family-base"]],
labelFontFamily = vars[["font-family-base"]]
)
widget$x <- utils::modifyList(widget$x, defaults)
widget
}
)
}

Expand Down Expand Up @@ -107,7 +127,7 @@ resolveSectors <- function(sectors, min, max) {

# create default sectors if necessary
if (is.null(sectors)) {
sectors = sectors(
sectors <- sectors(
success = c(min, max),
warning = NULL,
danger = NULL,
Expand Down Expand Up @@ -142,6 +162,6 @@ resolveSectors <- function(sectors, min, max) {
addSector(sectors$danger, sectors$colors[[3]])

# return
customSectors
list(percents = FALSE, ranges = customSectors)
}

35 changes: 20 additions & 15 deletions inst/htmlwidgets/gauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ HTMLWidgets.widget({
return defaultColor;
}
}
for (var i=0; i<x.customSectors.length; i++) {
var sector = x.customSectors[i];
var sectors = x.customSectors.ranges;
for (var i=0; i<sectors.length; i++) {
var sector = sectors[i];
if (sector.color === "primary")
sector.color = themeColor("primary", "#a9d70b");
else if (sector.color === "info")
Expand All @@ -41,26 +42,25 @@ HTMLWidgets.widget({
// justgage config
var config = {
id: el.id,
title: " ",
valueFontColor: "gray",
value: x.value,
min: x.min,
max: x.max,
titlePosition: "below",
relativeGaugeSize: true,
formatNumber: true,
humanFriendly: x.humanFriendly,
humanFriendlyDecimal: x.humanFriendlyDecimal,
customSectors: x.customSectors
};

// add symbol if specified
if (x.symbol !== null)
config.symbol = x.symbol;

// add label if specifed
if (x.label !== null)
config.label = x.label;
// Add these props to the config if specified
var optional_props = [
"symbol", "label", "gaugeColor", "valueFontColor",
"valueFontFamily", "labelFontColor", "labelFontFamily"
]
for (var i = 0; i < optional_props.length; i++) {
var prop = optional_props[i];
if (x[prop]) config[prop] = x[prop];
}

// add linked value class if appropriate
if (x.href !== null && window.FlexDashboard) {
Expand All @@ -71,10 +71,15 @@ HTMLWidgets.widget({
}

// create the justgage if we need to
if (justgage === null)
if (justgage === null) {
justgage = new JustGage(config);
else
justgage.refresh(x.value, x.max, config);
} else {
justgage.refresh(x.value, x.max, x.min, x.label);
gauge.update({
valueFontColor: x.valueFontColor,
labelFontColor: x.labelFontColor
});
}

// fixup svg path filters so they don't use relative hrefs
// e.g. url(#inner-shadow-htmlwidget-068c4cc821772b0a2ef5)
Expand Down
Loading

0 comments on commit 4fc48b8

Please sign in to comment.