闪亮:覆盖rhandsontable,无效(NULL)左侧的赋值

在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: invalid (NULL) left side of assignment

我也跟着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)

有解决方案吗

最佳答案 专注于你的第一个例子:

您正在尝试为反应对象分配值. function_table是一个被动的,所以你只能通过function_table()[….]< – …..得到它的值不设置它的值. 很难确切知道如何解决这个问题,因为你还没有发布一个可重复性最小的例子. 一种使用无功值解决此问题的方法.这是可以在服务器中分配和更改的值.这是一个例子:

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
    }
}

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

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

current = reactiveValues()
current$rhandsontable = NULL

observe(file_name,current$rhandsontable = read.csv(file_name))
点赞