R responsivevalues()更新

R responsivevalues()更新,r,shiny,R,Shiny,试图理解reactiveValues()和reactiveVal()之间的区别 我在global.R中初始化一个reactiveValues对象。输出是数据帧的列表。当我试图通过actionButton()更新数据帧列表中的数据时,它无法更新 我在这里使用了错误的函数吗 下面是一个可复制的示例 我希望能够使用刷新按钮更新类似于ggplot(data=invoices$last\u invoices,…)的绘图 多谢各位 library(shiny) library(plotly) library

试图理解
reactiveValues()
reactiveVal()
之间的区别

我在global.R中初始化一个reactiveValues对象。输出是数据帧的列表。当我试图通过
actionButton()
更新数据帧列表中的数据时,它无法更新

我在这里使用了错误的函数吗

下面是一个可复制的示例

我希望能够使用刷新按钮更新类似于
ggplot(data=invoices$last\u invoices,…)
的绘图

多谢各位

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)
库(闪亮)
图书馆(绘本)
图书馆(GG2)
#全球

发票您可以独立更新reactiveValues()的每个元素:

invoices$last\u invoices=发票$this\u invoices
发票$this_invoice[,c('cyl','mpg')]=发票$this_invoice[,c('cyl','mpg')]+1000

使用
reactiveVal
而不是
reactiveValues
立即更新reactive中的所有内容

留下评论供将来更新

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)
库(闪亮)
图书馆(绘本)
图书馆(GG2)
#全球

count脚本中始终存在一个输入错误:inovice而不是invoiceThank you-我修复了输入错误。感谢您的评论-这确实有效,但不足以满足我的用例。我现在明白了,当使用
reactiveValues
时,每个元素都必须独立更新。在我的例子中,我有一个函数,它可以获取新数据,并立即刷新所有内容。然后,您可以将所有内容放在一个reactiveValue中,但每次都必须刷新所有内容