我在 Shiny 应用程序中使用 htmlWidgets 包中的 onRender() 函数。我正在尝试保存我在给定的 onRender() 函数调用中创建的某些对象,以便它们可以在给定的 onRender() 函数调用之外使用。
下面是我的 MWE。我在 onRender() 函数中创建了一个名为 val2 的对象,它只是滑块输入值乘以 2。我可以保存 val2 对象,以便稍后在 onRender() 函数之外使用它吗?(我意识到在这个过于简化的示例中,我不需要使用 onRender() 函数来创建 val2 对象,但我试图使示例保持简单)。
感谢您的任何建议!
library(plotly)
library(htmlwidgets)
library(shiny)
myPlot <- qplot(data=mtcars, mpg, cyl)
gMyPlot <- ggplotly(myPlot)
ui <- shinyUI(fluidPage(
sliderInput("ci", "Value:", min = 0, max = 1, value=0.95, step=0.01),
plotlyOutput("myTest")
))
server <- shinyServer(function(input, output) {
ci <- reactive(input$ci)
output$myTest <- renderPlotly(gMyPlot %>% onRender("
function(el, x, data) {
val2 = data * 2
console.log(val2)
}", data=ci()))})
shinyApp(ui, server)