【问题标题】:Shiny: overwriting rhandsontable, invalid (NULL) left side of assignment闪亮:覆盖rhandsontable,赋值左侧无效(NULL)
【发布时间】:2019-01-24 09:09:07
【问题描述】:

在 Shiny App 中,我想从本地读取一个表,将其显示为 rhandsontable,并用Sys.Date() 覆盖其中一列,并将更新后的表显示为 rhandsontable。

表格function_table 看起来像这样。

     client    fun    agency    loading_site   last_update_on_DB
       IKEA   mean        NA           Paris          2018-08-01
     Nestle    sum        NA          Berlin          2018-08-02
     Toyota   mean        NA          Munich          2018-07-01
          :      :         :               :                   :

这是我的服务器。R

# read a table
func_path <- '/...[FILE PASS].../function_table.csv'
function_table <- reactive({read.csv(func_path, header = T, sep = ',', stringsAsFactors = F)})

# convert table to rhandsontable object to display in the app
hot_func <- reactive({function_table() %>%
  rhandsontable(width = 1000, height = 1000) %>%
  hot_cols(readOnly = T) %>%
  hot_table(stretchH = "all" )})

# do some computations and overwrites the table
# ui.R has an action button (id = compute)
observeEvent({
   input$compute
       },{

          # some computations...

          # overwrite last_update_on_DB column with Sys.Date()
          function_table()[function_table()$client == input$client,]$last_update_on_DB <- Sys.Date()
   })

但错误显示:

Reactives:赋值左侧无效(NULL)

我也关注了this page,如下所示,但出现另一个错误:

# read a table
values <- reactiveValues()
func_path <- '/...[FILE PASS].../function_table.csv'
values$function_table <- reactive({read.csv(func_path, header = T, sep = ',', stringsAsFactors = F)})

# convert table to rhandsontable object to display in the app
hot_func <- reactive({values$function_table() %>%
  rhandsontable(width = 1000, height = 1000) %>%
  hot_cols(readOnly = T) %>%
  hot_table(stretchH = "all" )})

# do some computations and overwrites the table
# ui.R has an action button (id = compute)
observeEvent({
   input$compute
       },{

          # some computations...

          # overwrite last_update_on_DB column with Sys.Date()
          values$function_table <- values$function_table()
  values$function_table[values$function_table$client == input$client, ]$last_update_on_DB <- Sys.Date()
   })


Warning: Error in eval: tentative d'appliquer un objet qui n'est pas une fonction
(it's telling me that in the line of hot_func, values$function_table() is not a function)

有什么解决办法吗?

【问题讨论】:

    标签: r shiny overwrite reactive rhandsontable


    【解决方案1】:

    专注于您的第一个示例:

    您正在尝试为反应性对象分配值。 function_table 是一个响应式,所以你只能通过function_table()[....] &lt;- .... 获取它的值而不是设置它的值。

    很难确切知道如何解决这个问题,因为您没有发布一个最小的可重现示例。

    使用反应值解决此问题的一种方法。这是一个可以在服务器中分配和更改的值。这是一个例子:

    server = function(input, output, session){
        current = reactiveValues()
        current$rhandsontable = read.csv(....) # your own code here to load the file on start up
    
        observeEvent(input$compute,{
            tmp = isolate(current$rhandsontable)
            tmp[tmp$client == input$client,]$last_update_on_DB <- Sys.Date()
            current$rhandsontable <- tmp
        }
    }
    

    isolate(...) 的使用可能是不必要的。它用于防止在检索其内容时重新评估反应值。

    此示例假设您在服务器的开头加载一个固定/已知文件。如果您正在动态加载文件,您可能需要使用观察者来设置当前值。例如(伪代码):

    current = reactiveValues()
    current$rhandsontable = NULL
    
    observe(file_name,current$rhandsontable = read.csv(file_name))
    

    【讨论】:

      猜你喜欢
      • 2016-06-16
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      相关资源
      最近更新 更多