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

Feature formatters #11

Closed
wants to merge 9 commits into from
Closed
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ Imports:
glue,
htmlwidgets,
purrr,
readr,
shiny
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(set_progress_formatter)
export(set_star_formatter)
export(set_tick_cross_formatter)
export(tabulator)
export(tabulatorContext)
export(tabulatorOutput)
export(tabulator_options)
export(trigger_download)
Expand Down
102 changes: 98 additions & 4 deletions R/columns.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,109 @@ add_filter_to_columns <- function(columns) {
return(columns)
}

# Formatters
# Formatters ####

# Plain Text
#' @export
set_plaintext_formatter <- function(widget, column, number_of_stars, hoz_align = "left") {
col_update <- list(formatter = "plaintext", hozAlign = hoz_align)
modify_col_def(widget, column, col_update)
}

# Text Area
#' @export
set_textarea_formatter <- function(widget, column, hoz_align = "left") {
col_update <- list(formatter = "textarea", hozAlign = hoz_align)
modify_col_def(widget, column, col_update)
}

# HTML
#' @export
set_html_formatter <- function(widget, column, hoz_align = "left") {
col_update <- list(formatter = "html", hozAlign = hoz_align)
modify_col_def(widget, column, col_update)
}

# Money
#' @export
set_money_formatter <- function(widget, column, decimal = ",", thousand = ".",
symbol = "£", symbolAfter = "p",
negativeSign = T, precision = F,
hoz_align = "left") {
col_update <- list(
formatter = "money",
formatterParams = list(
decimal = decimal,
thousand = thousand,
symbol = symbol,
symbolAfter = symbolAfter,
negativeSign = negativeSign,
precision = precision
),
hozAlign = hoz_align
)
modify_col_def(widget, column, col_update)
}

# Image
#' @export
set_image_formatter <- function(widget, column, height = "50px", width = "50px",
urlPrefix = "http://website.com/images/",
urlSuffix = ".png", hoz_align = "center") {
col_update <- list(
formatter = "image",
formatterParams = list(
height = height,
width = width,
urlPrefix = urlPrefix,
urlSuffix = urlSuffix
),
hozAlign = hoz_align
)
modify_col_def(widget, column, col_update)
}

# Link
#' @export

set_link_formatter <- function(widget, column, labelField = "", urlPrefix = "",
target = "_blank", hoz_align = "left") {
col_update <- list(
formatter = "link",
formatterParams = list(
labelField = labelField,
urlPrefix = urlPrefix,
target = target
)
)

modify_col_def(widget, column, col_update)
}

# Star
#' @export
set_star_formatter <- function(widget, column, number_of_stars, hoz_align = "center") {
col_update <- list(formatter = "star", formatterParams = list(stars = number_of_stars), hozAlign = hoz_align)
col_update <- list(
formatter = "star",
formatterParams = list(stars = number_of_stars),
hozAlign = hoz_align
)
modify_col_def(widget, column, col_update)
}


# Progress
#' @export
set_progress_formatter <- function(widget, column, hoz_align = "left") {
col_update <- list(formatter = "progress", hozAlign = hoz_align)
set_progress_formatter <- function(widget, column, min = 0, max = 10, color = c("green", "orange", "red"),
legendColor = "#000000", legendAlign = "center", hoz_align = "left") {
col_update <- list(
formatter = "progress",
formatterParams = list(
min = min, max = max, color = color,
legendColor = legendColor, legendAlign = legendAlign
),
hozAlign = hoz_align
)
modify_col_def(widget, column, col_update)
}

Expand All @@ -76,6 +169,7 @@ set_tick_cross_formatter <- function(widget, column) {
modify_col_def(widget, column, col_update)
}


modify_col_def <- function(widget, column, col_update) {
for (index in 1:length(widget$x$options$columns)) {
if (widget$x$options$columns[[index]]$field == column) {
Expand Down
38 changes: 27 additions & 11 deletions R/tabulator.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,47 @@
#' <Add Title>
#'
#' <Add Description>
#' Create a Tabulator Widget
#'
#' @param data (data.frame or list): In spreadsheet mode data needs to be a list or \code{NULL}
#' for an empty spreadsheet.
#' @param options (list): Setup options. See \code{\link{tabulator_options}}.
#' @param editable (bool): Whether the table is editable.
#' @param theme (character): Theme to apply to the table.
#' @param width Width of the widget.
#' @param height Height of the widget.
#' @param element_id description
#' @param ... Named arguments that are appended to the \code{options} parameter.
#' @import htmlwidgets
#'
#' @export
tabulator <- function(data, options = tabulator_options(),
rtabulator_auto_columns = TRUE,
editable = FALSE,
width = NULL, height = NULL, elementId = NULL, ...) {
tabulator <- function(
data,
options = tabulator_options(),
rtabulator_auto_columns = TRUE,
editable = FALSE,
theme = c("default", "midnight", "modern", "simple", "site", "bootstrap3", "bootstrap4", "bootstrap5", "bulma", "materialize", "semanticui"),
width = NULL,
height = NULL,
element_id = NULL,
...) {
if (is.null(options)) options <- list()

options <- utils::modifyList(options, list(...))
if (isTRUE(options$spreadsheet)) {
# ...
} else {
data <- fix_colnames(data)
if (rtabulator_auto_columns && is.null(options$columns)) {
if (getOption("rtabulator.auto_columns", TRUE) && is.null(options$columns)) {
options$columns <- create_columns(data, editor = editable)
}

data <- set_auto_id(data)
}

theme <- match.arg(theme)
stylesheet_text <- ifelse(theme == "default", NA, read_tabulator_theme(theme))

x <- list(
data = data,
options = keys_to_camel_case(compact(options))
options = keys_to_camel_case(compact(options)),
stylesheetText = stylesheet_text
)


Expand All @@ -36,7 +52,7 @@ tabulator <- function(data, options = tabulator_options(),
width = width,
height = height,
package = "rtabulator",
elementId = elementId
elementId = element_id
)
}

Expand Down
4 changes: 4 additions & 0 deletions R/tabulator_context.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ invoke_method <- function(widget, method_name, ...) {
invisible(widget)
}

#' Create a Tabulator Context
#' @param output_id (character): A tabulator output id.
#' @param session shiny session object
#' @export
tabulatorContext <- function(output_id, session = shiny::getDefaultReactiveDomain()) {
ctx <- list(
id = output_id,
Expand Down
9 changes: 9 additions & 0 deletions R/themes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
read_tabulator_theme <- function(
theme = c("midnight", "modern", "simple", "site", "bootstrap3", "bootstrap4", "bootstrap5", "bulma", "materialize", "semanticui")) {
theme <- match.arg(theme)
file_name <- system.file(
glue::glue("htmlwidgets/libs/tabulator/tabulator_{theme}.min.css"),
package = "rtabulator"
)
return(readr::read_file(file_name))
}
10 changes: 10 additions & 0 deletions inst/htmlwidgets/libs/tabulator/get-themes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version=6.2.5
for base_theme in midnight modern simple site; do
echo $base_theme
curl -O https://unpkg.com/tabulator-tables@${version}/dist/css/tabulator_${base_theme}.min.css
done

for framework_theme in bootstrap3 bootstrap4 bootstrap5 bulma materialize semanticui; do
echo $framework_theme
curl -O https://unpkg.com/tabulator-tables@${version}/dist/css/tabulator_${framework_theme}.min.css
done
2 changes: 2 additions & 0 deletions inst/htmlwidgets/libs/tabulator/tabulator_bootstrap3.min.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions inst/htmlwidgets/libs/tabulator/tabulator_bootstrap4.min.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions inst/htmlwidgets/libs/tabulator/tabulator_bootstrap5.min.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions inst/htmlwidgets/libs/tabulator/tabulator_bulma.min.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions inst/htmlwidgets/libs/tabulator/tabulator_materialize.min.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions inst/htmlwidgets/libs/tabulator/tabulator_midnight.min.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions inst/htmlwidgets/libs/tabulator/tabulator_modern.min.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions inst/htmlwidgets/libs/tabulator/tabulator_semanticui.min.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions inst/htmlwidgets/libs/tabulator/tabulator_simple.min.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions inst/htmlwidgets/libs/tabulator/tabulator_site.min.css

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions inst/htmlwidgets/rtabulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
let table = null;
function renderValue(payload) {
console.log(payload);
if (payload.stylesheetText) {
document.head.insertAdjacentHTML(
"beforeend",
`<style>${payload.stylesheetText}</style>`
);
}
if (payload.options === null) {
payload.options = {};
}
Expand Down
26 changes: 23 additions & 3 deletions man/tabulator.Rd

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

16 changes: 16 additions & 0 deletions man/tabulatorContext.Rd

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