Title: | Import an Observable Notebook as HTML Widget |
---|---|
Description: | Allows loading and displaying an Observable notebook (online JavaScript notebooks powered by <https://observablehq.com>) as an HTML Widget in an R session, 'shiny' application or 'rmarkdown' document. |
Authors: | Julien Barnier [aut, cre], Kenton Russell [aut] |
Maintainer: | Julien Barnier <[email protected]> |
License: | GPL (>=3) |
Version: | 0.2.2.9000 |
Built: | 2024-10-29 05:17:17 UTC |
Source: | https://github.com/juba/robservable |
robservable
notebook input through robservableProxy
Add an observer to a robservable
notebook input through robservableProxy
robs_observe(robs = NULL, observer = NULL)
robs_observe(robs = NULL, observer = NULL)
robs |
|
observer |
|
robservable_proxy
if (interactive()) { # change color with update through proxy library(shiny) library(robservable) ui <- tagList( robservableOutput("bar") ) server <- function(input, output, session) { robs <- robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) output$bar <- renderRobservable({ robs }) # set up a proxy to our bar robservable instance # for later manipulation robs_proxy <- robservableProxy("bar") observe({ invalidateLater(2000, session) # update with random color robs_update( robs_proxy, color = paste0( "rgb(", paste0(col2rgb(colors()[floor(runif(1,1,length(colors())))]),collapse=","), ")" ) ) }) } shinyApp(ui, server) # change data using update with proxy library(shiny) library(robservable) ui <- tagList( actionButton("btnChangeData", "Change Data"), robservableOutput("bar") ) server <- function(input, output, session) { robs <- robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) output$bar <- renderRobservable({ robs }) # set up a proxy to our bar robservable instance # for later manipulation robs_proxy <- robservableProxy("bar") observeEvent(input$btnChangeData, { robs_update( robs_proxy, data = data.frame( name = LETTERS[1:10], value = round(runif(10)*100) ) ) }) } shinyApp(ui, server) # add an observer through proxy library(shiny) library(robservable) ui <- tagList( robservableOutput("bar") ) server <- function(input, output, session) { robs <- robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) output$bar <- renderRobservable({ robs }) # set up a proxy to our bar robservable instance # for later manipulation robs_proxy <- robservableProxy("bar") robs_observe(robs_proxy, "color") observeEvent(input$bar_color, { print(input$bar_color) }) observe({ invalidateLater(2000, session) # update with random color robs_update( robs_proxy, color = paste0( "rgb(", paste0(col2rgb(colors()[floor(runif(1,1,length(colors())))]),collapse=","), ")" ) ) }) } shinyApp(ui, server) }
if (interactive()) { # change color with update through proxy library(shiny) library(robservable) ui <- tagList( robservableOutput("bar") ) server <- function(input, output, session) { robs <- robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) output$bar <- renderRobservable({ robs }) # set up a proxy to our bar robservable instance # for later manipulation robs_proxy <- robservableProxy("bar") observe({ invalidateLater(2000, session) # update with random color robs_update( robs_proxy, color = paste0( "rgb(", paste0(col2rgb(colors()[floor(runif(1,1,length(colors())))]),collapse=","), ")" ) ) }) } shinyApp(ui, server) # change data using update with proxy library(shiny) library(robservable) ui <- tagList( actionButton("btnChangeData", "Change Data"), robservableOutput("bar") ) server <- function(input, output, session) { robs <- robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) output$bar <- renderRobservable({ robs }) # set up a proxy to our bar robservable instance # for later manipulation robs_proxy <- robservableProxy("bar") observeEvent(input$btnChangeData, { robs_update( robs_proxy, data = data.frame( name = LETTERS[1:10], value = round(runif(10)*100) ) ) }) } shinyApp(ui, server) # add an observer through proxy library(shiny) library(robservable) ui <- tagList( robservableOutput("bar") ) server <- function(input, output, session) { robs <- robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) output$bar <- renderRobservable({ robs }) # set up a proxy to our bar robservable instance # for later manipulation robs_proxy <- robservableProxy("bar") robs_observe(robs_proxy, "color") observeEvent(input$bar_color, { print(input$bar_color) }) observe({ invalidateLater(2000, session) # update with random color robs_update( robs_proxy, color = paste0( "rgb(", paste0(col2rgb(colors()[floor(runif(1,1,length(colors())))]),collapse=","), ")" ) ) }) } shinyApp(ui, server) }
robservable
through robservableProxy
Update robservable
through robservableProxy
robs_update(robs = NULL, ...)
robs_update(robs = NULL, ...)
robs |
|
... |
named arguments to represent variables or inputs to update |
robservable_proxy
if (interactive()) { # change color with update through proxy library(shiny) library(robservable) ui <- tagList( robservableOutput("bar") ) server <- function(input, output, session) { robs <- robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) output$bar <- renderRobservable({ robs }) # set up a proxy to our bar robservable instance # for later manipulation robs_proxy <- robservableProxy("bar") observe({ invalidateLater(2000, session) # update with random color robs_update( robs_proxy, color = paste0( "rgb(", paste0(col2rgb(colors()[floor(runif(1,1,length(colors())))]),collapse=","), ")" ) ) }) } shinyApp(ui, server) # change data using update with proxy library(shiny) library(robservable) ui <- tagList( actionButton("btnChangeData", "Change Data"), robservableOutput("bar") ) server <- function(input, output, session) { robs <- robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) output$bar <- renderRobservable({ robs }) # set up a proxy to our bar robservable instance # for later manipulation robs_proxy <- robservableProxy("bar") observeEvent(input$btnChangeData, { robs_update( robs_proxy, data = data.frame( name = LETTERS[1:10], value = round(runif(10)*100) ) ) }) } shinyApp(ui, server) # add an observer through proxy library(shiny) library(robservable) ui <- tagList( robservableOutput("bar") ) server <- function(input, output, session) { robs <- robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) output$bar <- renderRobservable({ robs }) # set up a proxy to our bar robservable instance # for later manipulation robs_proxy <- robservableProxy("bar") robs_observe(robs_proxy, "color") observeEvent(input$bar_color, { print(input$bar_color) }) observe({ invalidateLater(2000, session) # update with random color robs_update( robs_proxy, color = paste0( "rgb(", paste0(col2rgb(colors()[floor(runif(1,1,length(colors())))]),collapse=","), ")" ) ) }) } shinyApp(ui, server) }
if (interactive()) { # change color with update through proxy library(shiny) library(robservable) ui <- tagList( robservableOutput("bar") ) server <- function(input, output, session) { robs <- robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) output$bar <- renderRobservable({ robs }) # set up a proxy to our bar robservable instance # for later manipulation robs_proxy <- robservableProxy("bar") observe({ invalidateLater(2000, session) # update with random color robs_update( robs_proxy, color = paste0( "rgb(", paste0(col2rgb(colors()[floor(runif(1,1,length(colors())))]),collapse=","), ")" ) ) }) } shinyApp(ui, server) # change data using update with proxy library(shiny) library(robservable) ui <- tagList( actionButton("btnChangeData", "Change Data"), robservableOutput("bar") ) server <- function(input, output, session) { robs <- robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) output$bar <- renderRobservable({ robs }) # set up a proxy to our bar robservable instance # for later manipulation robs_proxy <- robservableProxy("bar") observeEvent(input$btnChangeData, { robs_update( robs_proxy, data = data.frame( name = LETTERS[1:10], value = round(runif(10)*100) ) ) }) } shinyApp(ui, server) # add an observer through proxy library(shiny) library(robservable) ui <- tagList( robservableOutput("bar") ) server <- function(input, output, session) { robs <- robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) output$bar <- renderRobservable({ robs }) # set up a proxy to our bar robservable instance # for later manipulation robs_proxy <- robservableProxy("bar") robs_observe(robs_proxy, "color") observeEvent(input$bar_color, { print(input$bar_color) }) observe({ invalidateLater(2000, session) # update with random color robs_update( robs_proxy, color = paste0( "rgb(", paste0(col2rgb(colors()[floor(runif(1,1,length(colors())))]),collapse=","), ")" ) ) }) } shinyApp(ui, server) }
Display an Observable notebook as HTML widget
robservable( notebook, include = NULL, hide = NULL, input = NULL, input_js = NULL, observers = NULL, update_height = TRUE, update_width = TRUE, width = NULL, height = NULL, elementId = NULL, json_args = list(dataframe = "rows"), json_func = NULL )
robservable( notebook, include = NULL, hide = NULL, input = NULL, input_js = NULL, observers = NULL, update_height = TRUE, update_width = TRUE, width = NULL, height = NULL, elementId = NULL, json_args = list(dataframe = "rows"), json_func = NULL )
notebook |
The notebook id, such as "@d3/bar-chart", or the full notebook URL. |
include |
character vector of cell names to be rendered. If NULL, the whole notebook is rendered. |
hide |
character vector of cell names in |
input |
A named list of cells to be updated with a fixed value. |
input_js |
A named list of cells to be updated with JavaScript code. Each list element is itself a list
with a vector of argument names as |
observers |
A vector of character strings representing variables in observable that you would like to set as input values in Shiny. |
update_height |
if TRUE (default) and input$height is not defined, replace its value with the height of the widget root HTML element. Note there will not always be such a cell in every notebook. Set it to FALSE to always keep the notebook value. |
update_width |
if TRUE (default) and input$width is not defined, replace its value with the width of the widget root HTML element. Set it to FALSE to always keep the notebook or the Observable stdlib value. |
width |
htmlwidget width. |
height |
htmlwidget height. |
elementId |
optional manual widget HTML id. |
json_args |
custom arguments passed to JSON serializer. |
json_func |
optional custom JSON serializer R function. |
If a data.frame is passed as a cell value in input
, it will be converted into the format
expected by d3
(ie, converted by rows).
For more details on the use of input_js
to update cells with JavaScript code, see the
introduction vignette and https://github.com/observablehq/runtime#variable_define.
An object of class htmlwidget
.
## Display a notebook cell robservable( "@juba/robservable-bar-chart", include = "chart" ) ## Change cells data with input robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) ## Change data frame cells data df <- data.frame(table(mtcars$cyl)) names(df) <- c("name", "value") robservable( "@juba/robservable-bar-chart", include = "chart", input = list( data = df, x = "value", y = "name" ) )
## Display a notebook cell robservable( "@juba/robservable-bar-chart", include = "chart" ) ## Change cells data with input robservable( "@juba/robservable-bar-chart", include = "chart", input = list(color = "red", height = 700) ) ## Change data frame cells data df <- data.frame(table(mtcars$cyl)) names(df) <- c("name", "value") robservable( "@juba/robservable-bar-chart", include = "chart", input = list( data = df, x = "value", y = "name" ) )
Output and render functions for using robservable within Shiny applications and interactive Rmd documents.
robservableOutput(outputId, width = "100%", height = "400px") renderRobservable(expr, env = parent.frame(), quoted = FALSE)
robservableOutput(outputId, width = "100%", height = "400px") renderRobservable(expr, env = parent.frame(), quoted = FALSE)
outputId |
output variable to read from |
width , height
|
Must be a valid CSS unit (like |
expr |
An expression that generates a robservable |
env |
The environment in which to evaluate |
quoted |
Is |
Creates a robservable-like object that can be used to customize and control a robservable that has already been rendered. For use in Shiny apps and Shiny docs only.
robservableProxy( id, session = shiny::getDefaultReactiveDomain(), deferUntilFlush = TRUE )
robservableProxy( id, session = shiny::getDefaultReactiveDomain(), deferUntilFlush = TRUE )
id |
single-element character vector indicating the output ID of the robservable to modify (if invoked from a Shiny module, the namespace will be added automatically) |
session |
the Shiny session object to which the robservable belongs; usually the default value will suffice |
deferUntilFlush |
indicates whether actions performed against this
instance should be carried out right away, or whether they should be held
until after the next time all of the outputs are updated; defaults to
|
Normally, you create a robservable instance using the robservable
function.
This creates an in-memory representation of a robservable that you can customize,
print at the R console, include in an R Markdown document, or render as a Shiny output.
In the case of Shiny, you may want to further customize a robservable, even after it is rendered to an output. At this point, the in-memory representation of the robservable is long gone, and the user's web browser has already realized the robservable instance.
This is where robservableProxy
comes in. It returns an object that can
stand in for the usual robservable object. The usual robservable functions
can be called, and instead of customizing an in-memory representation,
these commands will execute on the already created robservable instance in
the browser.
A proxy object which allows to update an already created robservable instance.
Convert a Date or POSIXt object to a JS Date format
to_js_date(date)
to_js_date(date)
date |
object to be converted |
Numeric value representing the number of milliseconds between Unix Epoch
(1 January 1970 UTC) and date
.