Closed pietrodito closed 1 year ago
I am creating a Shniy widget for integrating the JS library tabulator.
In order to that, I use the htmlwidgets R package.
Here is a github repo with the project.
library(devtools) library(usethis) library(htmlwidgets)
create_package("SOirisIssueTabulator") scaffoldWidget("tabulator")
Download js and css tabulator dependencies on website and place tabulator.min.css and tabulator.min.js inside the inst/htmlwidgets/tabulator/ directory.
tabulator.min.css
tabulator.min.js
inst/htmlwidgets/tabulator/
Then, edit inst/htmlwidgets/tabulator.yaml:
inst/htmlwidgets/tabulator.yaml
dependencies: - name: tabulator version: 5.5.2 src: htmlwidgets/tabulator script: tabulator.min.js stylesheet: tabulator.min.css
Edit the inst/htmlwidgets/tabulator.js:
inst/htmlwidgets/tabulator.js
HTMLWidgets.widget({ name: 'tabulator', type: 'output', factory: function(el, width, height) { return { renderValue: function(x) { new Tabulator("#" + el.id, { data: x.data, autoColumns: true, layout: "fitColumns", height: "100%" }); }, resize: function(width, height) {} }; } });
Edit R/tabulator.R
R/tabulator.R
#' @import htmlwidgets #' @export tabulator <- function(message, width = NULL, height = NULL, elementId = NULL) { x = list( message = message ) ## Add this line to template attr(x, 'TOJSON_ARGS') <- list(dataframe = "rows") htmlwidgets::createWidget( name = 'tabulator', x, width = width, height = height, package = 'SOirisIssueTabulator', elementId = elementId ) } ### Nothing to change after #' @name tabulator-shiny #' @export tabulatorOutput <- function(outputId, width = '100%', height = '400px'){ htmlwidgets::shinyWidgetOutput(outputId, 'tabulator', width, height, package = 'SOirisIssueTabulator') } #' @rdname tabulator-shiny #' @export renderTabulator <- function(expr, env = parent.frame(), quoted = FALSE) { if (!quoted) { expr <- substitute(expr) } htmlwidgets::shinyRenderWidget(expr, tabulatorOutput, env, quoted = TRUE) }
document(); install()
Create app.R:
app.R
library(shiny) library(tabulator) ui <- fluidPage( selectInput("data", "Data", choices = c("mtcars", "iris")), tabulatorOutput("table")) server <- function(input, output, session) { output$table <- renderTabulator( tabulator(get(input$data)))} shinyApp(ui, server)
The widget works fine with mtcars and all data.frames I have tested but not with iris but I cannot figure why!
mtcars
data.frame
iris
Context
I am creating a Shniy widget for integrating the JS library tabulator.
In order to that, I use the htmlwidgets R package.
Here is a github repo with the project.
Reproducible steps (RStudio)
Package setup
Dependencies
Download js and css tabulator dependencies on website and place
tabulator.min.css
andtabulator.min.js
inside theinst/htmlwidgets/tabulator/
directory.Then, edit
inst/htmlwidgets/tabulator.yaml
:JS side
Edit the
inst/htmlwidgets/tabulator.js
:R side
Edit
R/tabulator.R
Build package
Shinhy App
Create
app.R
:Issue
The widget works fine with
mtcars
and alldata.frame
s I have tested but not withiris
but I cannot figure why!