Reputation: 322
Trying to understand the difference between reactiveValues()
and reactiveVal()
.
I initialize a reactiveValues object in global.R. The output is a list of dataframes. When I try to update the data in my list of dataframes via actionButton()
, it fails to update.
Am I using the wrong function here?
Below is a reproducible example.
I want to be able to update a plot like ggplot(data = invoices$last_invoice, ....)
with a refresh button.
Thank you.
library(shiny)
library(plotly)
library(ggplot2)
# global.R
invoices <- do.call('reactiveValues', list(last_invoice = mtcars, this_invoice = mtcars))
# ui.R
ui <- fluidPage(
fluidRow(
align='center',
column(8,
actionButton('refresh_data', "Refresh Data")
),
column(8,
offset = 2,
shiny::uiOutput('chart')
)
)
)
# server.R
server <- function(input, output, session) {
# QuickBooks --------------------------------------------------------------
observeEvent(input$refresh_data, {
# browser()
showModal(modalDialog('Connecting to company file', footer = NULL, easyClose = FALSE))
print('Updating...')
# etl_pipe('CompanyFile') function that updates some values...
invoices <- do.call('reactiveValues', list(last_invoice = mtcars[c('cyl','mpg')] + 1000, this_invoice = mtcars[c('cyl', 'mpg')] + 1000))
# Returns NULL value
print(invoices$this_invoice)
session$reload()
})
output$chart <- renderUI({
tagList(
plotly::renderPlotly({
plotly::ggplotly(
invoices$this_invoice %>%
ggplot2::ggplot(ggplot2::aes(mpg, cyl)) +
ggplot2::geom_point()
)
})
)
})
}
shiny::shinyApp(ui,server)
Upvotes: 0
Views: 1478
Reputation: 322
Use reactiveVal
instead of reactiveValues
to update everything within the reactive at once.
Leaving here with comments for future updates.
library(shiny)
library(plotly)
library(ggplot2)
# global.R
count <- 0
if (count == 0) {
# Does not persist update outside observer
# invoices <<- do.call('reactiveValues', list(last_invoice = mtcars, this_invoice = mtcars))
# Updates the original values as a function.
invoices <<- reactiveVal(list(last_invoice = mtcars, this_invoice = mtcars))
}
# ui.R
ui <- fluidPage(
fluidRow(
align='center',
column(8,
actionButton('refresh_data', "Refresh Data")
),
column(8,
offset = 2,
shiny::uiOutput('chart')
)
)
)
# server.R
server <- function(input, output, session) {
# QuickBooks --------------------------------------------------------------
observeEvent(input$refresh_data, {
count <<- count + 1
# browser()
showModal(modalDialog('Connecting to company file', footer = NULL, easyClose = FALSE))
print('Updating...')
# Updates are local to observer
# invoices <- do.call(
# 'reactiveValues',
# list(
# last_invoice = mtcars[c('cyl','mpg')] + 1000,
# this_invoice = mtcars[c('cyl', 'mpg')] + 1000
# )
# )
# Updating the values directly did not work.
# invoices <- list(this_invoice = mtcars[c('cyl','mpg')] + 1000)
# invoices$this_invoice <- mtcars[c('cyl', 'mpg')] + 1000
# This works updating the last values - need to call values as a function
invoices(
list(
this_invoice = invoices()$this_invoice[c('cyl','mpg')] + 1000,
last_inovice = mtcars)
)
session$reload()
})
output$chart <- renderUI({
print(invoices())
tagList(
plotly::renderPlotly({
plotly::ggplotly(
invoices()$this_invoice %>%
ggplot2::ggplot(ggplot2::aes(mpg, cyl)) +
ggplot2::geom_point()
)
})
)
})
}
shiny::shinyApp(ui,server)
Upvotes: -3
Reputation: 19544
You can update each element of the reactiveValues() independently:
invoices$last_invoice = invoices$this_invoice
invoices$this_invoice[,c('cyl', 'mpg')] = invoices$this_invoice[,c('cyl', 'mpg')] + 1000
Upvotes: 1