Add Shiny output binding with g2output() and renderG2()#6
Conversation
Agent-Logs-Url: https://github.com/yihui/gglite/sessions/761adebb-cd4a-4204-9b05-27bd8cf72e48 Co-authored-by: yihui-bot <264330240+yihui-bot@users.noreply.github.com>
yihui-bot
left a comment
There was a problem hiding this comment.
if you haven't, please test all shiny apps you provided in browser and make sure they work
| - Added Shiny examples in `examples/shiny.Rmd`. | ||
| - Added a `\dontrun{}` example to `render_shiny()` showing minimal Shiny app setup. | ||
|
|
There was a problem hiding this comment.
Fixed in 78c79dd — NEWS.md is back to just the # gglite 0.1 heading.
| tags$head( | ||
| tags$script(src = 'https://unpkg.com/@antv/g2@5/dist/g2.min.js'), | ||
| tags$script( | ||
| src = 'https://cdn.jsdelivr.net/npm/@xiee/utils/js/g2-column.min.js' | ||
| ), | ||
| includeScript(system.file('www/g2-handler.js', package = 'gglite')) | ||
| ) |
There was a problem hiding this comment.
it's tedious to repeat these lines in every shiny app, and these are also deep technical details that end-users shouldn't worry about; can we provide the traditional shiny syntax like g2output() and renderG2()? g2output() will make sure to include the JS dependencies into shiny's meta, and output$ID = renderG2() feels more familiar to shiny users
if that's possible, we can get rid of render_shiny()
i don't want to introduce hard dependencies for this task; dependencies in Suggests will be fine
There was a problem hiding this comment.
Done in 78c79dd. render_shiny() is removed and replaced with a proper Shiny output binding:
g2output(outputId)— creates the placeholder div and automatically injects the G2 CDN, column-major helper, and the new output binding JS as htmlDependencies; no manualtags$head(...)neededoutput$ID = renderG2({...})— standard Shiny render pattern; usesmarkRenderFunctionso reactivity works out of the box
examples/shiny.Rmd now shows just g2output('chart1') in the UI and output$chart1 = renderG2({...}) in the server, with three examples (basic, reactive, multiple charts).
Tested the binding in headless Playwright: element found with shiny-bound-output class, spec delivered correctly as a JS object (Shiny embeds xfun_raw_string json as raw JSON preserving column-major format), chart renders and old chart is properly destroyed on re-render.
Agent-Logs-Url: https://github.com/yihui/gglite/sessions/21937d1c-c172-4872-91c3-1c5c1181d7dd Co-authored-by: yihui-bot <264330240+yihui-bot@users.noreply.github.com>
Tested in commit 78c79dd. Started the basic app, confirmed the Shiny output binding ( |
gglite had no idiomatic Shiny integration — users had to manually wire up CDN scripts, a message handler, and bare
<div>containers. This PR replaces the low-levelrender_shiny()with a proper Shiny output binding following the standardfooOutput()/renderFoo()pattern.Changes
R/shiny.R— new fileg2output(outputId, width, height)— creates the placeholder<div>and automatically injects the G2 CDN, column-major data helper, and output binding ashtmlDependencyobjects; no manualtags$head()wiring neededrenderG2(expr, ...)— standardoutput$ID = renderG2({...})pattern usingshiny::markRenderFunctioninst/www/g2-binding.js— new fileShiny output binding (
gglite.g2) that finds.gglite-outputelements, creates/destroys G2 chart instances on each render, and stores the instance on the element for cleanup on reactive updates.R/render.RRemoved
render_shiny()(superseded byg2output()/renderG2()).DESCRIPTIONAdded
shinyandhtmltoolstoSuggests.examples/shiny.Rmd— new example pageThree sections with prose explanations:
g2output('chart1')in the UI,output$chart1 = renderG2({...})in the serverselectInputdrives chart updates automaticallyfluidRowUsage