Home > Software design >  How do I pass reactiveValues as an argument to ggplot
How do I pass reactiveValues as an argument to ggplot

Time:10-26

I can't pass reactiveValues as an argument for ggplot.

library(shiny)
library(tidyverse)

ui <- fluidPage(
  sidebarPanel(
    actionButton('getdata', 'Get Data')  
  ),
  mainPanel(
    plotOutput('plot', brush = brushOpts(id = 'brush'), dblclick = 'dclick')  
  )
)

server <- function(input, output, session) {
  dat <- reactiveValues(val = NULL)
  
  dat$val <- eventReactive(input$getdata, {
    tibble('BusDate' = Sys.Date() - 0:10, 'Val' = rnorm(11))
  })
    output$plot <- renderPlot({
    ggplot(dat$val, aes(x = BusDate, y = Val))   geom_line()
  })
  
}

shinyApp(ui, server)

Error returned is

Error in ggplot:   You're passing a function as global data.Have you misspelled the `data` argument in `ggplot()`

If i replace everything with reactiveVal it works fine

e.g.

library(shiny)
library(tidyverse)

ui <- fluidPage(
  sidebarPanel(
    actionButton('getdata', 'Get Data')  
  ),
  mainPanel(
    plotOutput('plot', brush = brushOpts(id = 'brush'), dblclick = 'dclick')  
  )
)

server <- function(input, output, session) {
  
  dat <- eventReactive(input$getdata, {
    tibble('BusDate' = Sys.Date() - 0:10, 'Val' = rnorm(11))
  })
  output$plot <- renderPlot({
    ggplot(dat(), aes(x = BusDate, y = Val))   geom_line()
  })
  
}

shinyApp(ui, server)

How can I pass a reactiveValue to ggplot?

CodePudding user response:

It's reactiveValue inside reactiveValues, so you need to use dat$val()

    output$plot <- renderPlot({
        ggplot(dat$val(), aes(x = BusDate, y = Val))   geom_line()
    })
  • Related