Skip to content

Commit

Permalink
Merge pull request #25 from eodaGmbH/docs/shiny-vignette
Browse files Browse the repository at this point in the history
Docs/shiny vignette
  • Loading branch information
crazycapivara authored Sep 17, 2024
2 parents a8cba8c + 8607cf6 commit a46fc6a
Show file tree
Hide file tree
Showing 25 changed files with 471 additions and 65 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Imports:
glue,
htmltools,
htmlwidgets,
jsonlite,
purrr,
readr,
shiny
Expand Down
6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export(add_row)
export(create_columns)
export(delete_selected_rows)
export(for_each_col)
export(list_to_data_frame)
export(redo)
export(renderTabulator)
export(set_calculation)
Expand All @@ -25,7 +24,8 @@ export(set_formatter_toggle_switch)
export(set_formatter_traffic_light)
export(set_header_filter)
export(set_multi_column_header)
export(set_option_group_by)
export(set_options_group_by)
export(set_options_pagination)
export(set_tooltip)
export(spreadsheet_def)
export(tabulator)
Expand All @@ -35,6 +35,6 @@ export(tabulator_options)
export(titanic)
export(trigger_download)
export(trigger_get_data)
export(trigger_get_spreadsheet_data)
export(trigger_get_sheet_data)
export(undo)
import(htmlwidgets)
10 changes: 7 additions & 3 deletions R/context_calls.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#' @param type (character): csv, json or xlsx (needs sheetjs: \code{tabulator(..., sheetjs = TRUE)})
#' @param file_name (character): file name
#' @export
trigger_download <- function(ctx, type = c("csv", "json", "xlsx"), file_name) {
trigger_download <- function(ctx, type = c("csv", "json", "xlsx"), file_name = NULL) {
if (is.null(file_name)) {
file_name <- glue::glue("data.{type}")
}

invoke_method(ctx, "download", match.arg(type), file_name)
}

Expand Down Expand Up @@ -51,6 +55,6 @@ redo <- function(ctx) {
#' Submit data to R
#' @inheritParams trigger_download
#' @export
trigger_get_spreadsheet_data <- function(ctx) {
invoke_method(ctx, "getSpreadsheetData")
trigger_get_sheet_data <- function(ctx) {
invoke_method(ctx, "getSheetData")
}
36 changes: 36 additions & 0 deletions R/input_handlers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# https://book.javascript-for-r.com/shiny-complete.html
input_handler_data <- function(value, ...) {
if (debug_mode()) {
print("custom input handler data")
}

if (getOption("rtabulator.raw_data", FALSE)) {
return(value)
}

data <- value$data
try(data <- tabulator_data_as_df(value$data))
return(data)
}

# TODO: Not used at the moment
input_handler_sheet_data <- function(value, ...) {
if (debug_mode()) {
print("custom input handler data")
}

if (getOption("rtabulator.raw_data", FALSE)) {
return(value)
}

data <- value$data
# try(data <- jsonlite::fromJSON(jsonlite::toJSON(value$data, auto_unbox = TRUE)))
try(data <- sheet_data_to_matrix(data), silent = TRUE)
return(data)
}

sheet_data_to_matrix <- function(data) {
vec <- unlist(purrr::map(data, ~ as_vec(.x)))
# try({vec <- as.numeric(vec)})
return(matrix(vec, nrow = length(data), byrow = TRUE))
}
4 changes: 2 additions & 2 deletions R/js_dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sheetjs_dependency <- htmltools::htmlDependency(
name = "sheetjs",
version = SHEETJS_VERSION,
src = list(
href = glue::glue("https://cdn.sheetjs.com/xlsx-{SHEETJS_VERSION}/package/dist/")
href = glue::glue("https://cdn.sheetjs.com/xlsx-{SHEETJS_VERSION}/package/dist")
),
script = "xlsx.mini.min.js",
all_files = FALSE
Expand All @@ -17,7 +17,7 @@ luxon_dependency <- htmltools::htmlDependency(
name = "luxon",
version = LUXON_VERSION,
src = list(
href = glue::glue("https://cdn.jsdelivr.net/npm/luxon@{LUXON_VERSION}/build/global/")
href = glue::glue("https://cdn.jsdelivr.net/npm/luxon@{LUXON_VERSION}/build/global")
),
script = "luxon.min.js",
all_files = FALSE
Expand Down
3 changes: 3 additions & 0 deletions R/package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
debug_mode <- function() {
return(getOption("rtabulator.debug", FALSE))
}
26 changes: 19 additions & 7 deletions R/tabulator_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,40 @@ default_spreadsheet_options <- list(
spreadsheet_column_definition = list(editor = "input")
)

# TODO: Helper function to set pagination
set_option_pagination <- function(
#' Set pagination options
#' @inheritParams set_formatter_html
#' @inheritParams tabulator_options
#' @example examples/options/pagination_options.R
#' @export
set_options_pagination <- function(
widget,
pagination = FALSE,
pagination = TRUE,
pagination_size = 10,
pagination_size_selector = FALSE,
pagination_add_row = c("page", "table"),
...) {
return(widget)
# Body
options_update <- list(
pagination = pagination,
paginationSize = pagination_size,
paginationSizeSelector = pagination_size_selector,
paginationAddRow = match.arg(pagination_add_row),
...
)
modify_tabulator_options(widget, options_update)
}

#' Set group by option
#' Set group by options
#' @inheritParams set_formatter_html
#' @inheritParams tabulator_options
#' @export
set_option_group_by <- function(
set_options_group_by <- function(
widget,
group_by,
group_start_open = TRUE,
group_toggle_element = "header",
...) {
# return(widget)
# Body
options_update <- list(
groupBy = group_by,
groupStartOpen = group_start_open,
Expand Down
18 changes: 14 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ keys_to_camel_case <- function(x) {
stats::setNames(x, to_camel_case(names(x)))
}

# Remove NULL values from list
# Drop NULL values from list
compact <- function(x) {
x[!sapply(x, is.null)]
}

#' Parse List to Data Frame
#' @param x (list): A list of named lists.
#' @export
# Convert list of named lists to data frame
list_to_data_frame <- function(x) {
# jsonlite::toJSON(x, auto_unbox = TRUE) |> jsonlite::fromJSON()
return(do.call(rbind.data.frame, x))
}

# Convert data returned from JavaScript ####

as_NA <- function(...) NA

as_vec <- function(l) {
unlist(purrr::modify_if(l, is.null, as_NA))
}

tabulator_data_as_df <- function(data) {
return(as.data.frame(purrr::map(data, ~ as_vec(.x))))
}
8 changes: 8 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.onLoad <- function(libname, pkgname) {
# For devtools::load_all() we need to remove to handler if it is already registered
try(shiny::removeInputHandler("rtabulator.data"), silent = FALSE)
try(shiny::removeInputHandler("rtabulator.sheet_data"), silent = FALSE)

try(shiny::registerInputHandler("rtabulator.data", input_handler_data))
try(shiny::registerInputHandler("rtabulator.sheet_data", input_handler_sheet_data))
}
6 changes: 3 additions & 3 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ reference:
contents:
- tabulator
- tabulator_options
- set_option_group_by
- set_options_group_by
- set_options_pagination

- title: Column Settings
desc: >
Expand Down Expand Up @@ -40,7 +41,7 @@ reference:
- delete_selected_rows
- trigger_download
- trigger_get_data
- trigger_get_spreadsheet_data
- trigger_get_sheet_data
- undo
- redo

Expand All @@ -49,7 +50,6 @@ reference:
Utitily functions
contents:
- spreadsheet_def
- list_to_data_frame
- create_columns

- title: Data Sets
Expand Down
2 changes: 2 additions & 0 deletions examples/options/group_by_options.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tabulator(iris) |>
set_options_group_by("Species", group_start_open = FALSE)
2 changes: 2 additions & 0 deletions examples/options/pagination_options.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tabulator(iris) |>
set_options_pagination(pagination_size_selector = c(10, 20, 50))
32 changes: 32 additions & 0 deletions examples/shiny/advanced_app/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
library(shiny)
library(rtabulator)

ui <- fluidPage(
titlePanel("Titanic Data Set"),
tabulatorOutput("titanic"),
actionButton("submit", "Submit data to R")
)

server <- function(input, output) {
output$titanic <- renderTabulator({
tabulator(titanic(c("PassengerId", "Name", "Pclass", "Sex", "Fare", "Survived"))) |>
set_formatter_progress("Fare") |>
set_tooltip("Fare") |>
set_formatter_tick_cross("Survived") |>
set_formatter_star("Pclass", number_of_stars = 3)
})

observeEvent(input$submit, {
print(input$submit)
tabulatorContext("titanic") |>
trigger_get_data()
})

observeEvent(input$titanic_get_data, {
print(input$titanic_get_data)
# print(head(tabulator_data_as_data_frame(input$titanic_get_data$data)))
# print(list_to_data_frame(input$table_data$data))
})
}

shinyApp(ui = ui, server = server)
19 changes: 19 additions & 0 deletions examples/shiny/basic_app/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
library(shiny)
library(rtabulator)

ui <- fluidPage(
titlePanel("Titanic Data Set"),
tabulatorOutput("titanic")
)

server <- function(input, output) {
output$titanic <- renderTabulator({
tabulator(titanic()) |>
set_formatter_progress("Fare") |>
set_tooltip("Fare") |>
set_formatter_tick_cross("Survived") |>
set_formatter_star("Pclass", number_of_stars = 3)
})
}

shinyApp(ui = ui, server = server)
13 changes: 10 additions & 3 deletions examples/shiny/spreadsheet/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ server <- function(input, output) {
observe({
print(input$submit)
tabulatorContext(TABULATOR_OUTPUT_ID) |>
trigger_get_spreadsheet_data()
trigger_get_sheet_data() # |> trigger_get_data()
}) |> bindEvent(input$submit)


# observe({
# print("get_data")
# # print(head(input$spreadsheet_get_data) |> tibble::as_tibble())
# }) |> bindEvent(input$spreadsheet_get_data)

observe({
print(input$spreadsheet_spreadsheet_data)
}) |> bindEvent(input$spreadsheet_spreadsheet_data)
print("get_sheet_data")
print(input$spreadsheet_get_sheet_data)
# browser()
}) |> bindEvent(input$spreadsheet_get_sheet_data)
}

shinyApp(ui = ui, server = server)
59 changes: 55 additions & 4 deletions examples/shiny/titanic/app.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,70 @@
library(shiny)

data_url <- "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"
OUTPUT_ID <- "titanic"

data <- titanic(c("PassengerId", "Pclass", "Survived", "Fare"))
setup <- tabulator_options(
selectable_rows = TRUE,
edit_trigger_event = "focus"
)

ui <- fluidPage(
titlePanel("Titanic Data"),
tabulatorOutput("titanic")
tabulatorOutput(OUTPUT_ID),
actionButton("download", "Download"),
actionButton("submit", "Submit data to R")
)

server <- function(input, output) {
output$titanic <- renderTabulator({
tabulator(data_url, editable = TRUE) |>
set_formatter_progress("Fare") |>
tabulator(data, setup, editable = TRUE) |>
set_header_filter("Pclass", "list") |>
set_header_filter("Survived", "list") |>
set_header_filter("Fare", "number", "<=", clearable = TRUE) |>
set_options_pagination() |>
set_formatter_money(
"Fare",
symbol = "\U00A3", symbol_after = FALSE, precision = 1, hoz_align = "right"
) |>
set_tooltip("Fare") |>
set_formatter_tick_cross("Survived") |>
set_formatter_star("Pclass", number_of_stars = 3)
})

observeEvent(input$download, {
tabulatorContext(OUTPUT_ID) |>
trigger_download("csv")
})

observeEvent(input$titanic_row_clicked, {
print(input$titanic_row_clicked)
})

observeEvent(input$titanic_rows_selected, {
print("rows_selected")
print(input$titanic_rows_selected)
})

observeEvent(input$titanic_cell_edited, {
print("cell_edited")
print(input$titanic_cell_edited)
})

observeEvent(input$titanic_data_filtered, {
print("data_filtered")
print(head(input$titanic_data_filtered))
})

observeEvent(input$submit, {
print("Trigger get data")
tabulatorContext(OUTPUT_ID) |>
trigger_get_data()
})

observeEvent(input$titanic_get_data, {
print("Got data. Thanx!")
print(head(input$titanic_get_data))
})
}

shinyApp(ui = ui, server = server)
Loading

0 comments on commit a46fc6a

Please sign in to comment.