juba / robservable

Observable notebooks as R htmlwidgets
https://juba.github.io/robservable/
163 stars 11 forks source link

Help understanding how to update data #37

Closed MayaGans closed 3 years ago

MayaGans commented 3 years ago

I created this notebook below - to prototype I was using the viewof to switch between datasets but now I'd like to sub that out to enable the user to select their own data in R. I made a little notebook here: https://observablehq.com/@mayagans/cdisc-time-series-plot-with-update-in-r where essentially I'd like to let the user select from a filtered dataset using shiny, which will run update.chart(order) but I think my mental model is a little incorrect. Here's an example app:

library(shiny)
library(robservable)
library(tidyverse)

original_data <- tibble(
    member = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,
               1,1,1,1,1,2,2,2,2,2,3,3,3,3,3),
    dateTime = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,
                 1,2,3,4,5,1,2,3,4,5,1,2,3,4,5),
    forecast = c(1,5,7,8,3,4,9,10,6,7,3,4,7,9,3,
                 5,9,8,5,6,7,1,1,2,3,4,5,7,8,3),
    type = c(rep("A", 15), rep("B", 15))
)

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            selectInput("CHOICES", "CHOICES", c("A", "B"))
        ),
        mainPanel(
            robservableOutput("chart")
        )
    )
)
server <- function(input, output) {

    df <- reactive({
        original_data %>% filter(type == input$CHOICES)
    })

    output$chart <- renderRobservable(
        robservable::robservable(
            "@mayagans/cdisc-time-series-plot-with-update-in-r",
            include = c("chart"),
            # I thought this should update the data?
            input = list(order = df())
        )
    )
}
shinyApp(ui = ui, server = server)

Any help appreciated! Thank you!!

MayaGans commented 3 years ago

Oh interesting! So it looks like this works when I include = c("chart", "update") ...but I don't want the visual output of the update function, is there a way to hide it?

juba commented 3 years ago

Yes, there's an hide argument to robservable just for that.

robservable(
  "@mbostock/eyes",
  include = c("canvas", "mouse"),
  hide = "mouse"
)