簡體   English   中英

R Shiny reactiveValues() 更新

[英]R Shiny reactiveValues() update

試圖了解reactiveValues()reactiveVal()之間的區別。

我在 global.R 中初始化了一個 reactiveValues object。 output 是數據幀列表。 當我嘗試通過actionButton()更新我的數據框列表中的數據時,它無法更新。

我在這里使用了錯誤的 function 嗎?

下面是一個可重現的例子。

我希望能夠使用刷新按鈕更新 plot 之類的ggplot(data = invoices$last_invoice, ....)

謝謝你。

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)

您可以獨立更新 reactiveValues() 的每個元素:

    invoices$last_invoice = invoices$this_invoice
    invoices$this_invoice[,c('cyl', 'mpg')] = invoices$this_invoice[,c('cyl', 'mpg')] + 1000

使用reactiveVal而不是reactiveValues來一次更新響應式中的所有內容。

留下評論以供將來更新。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM