4

In Shiny App, I want to read a table from my local, display it as an rhandsontable, and overwrite one of the columns with Sys.Date(), and display the updated table as rhandsontable.

the table function_table looks like this.

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

Here is my server.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()
   })

But the error reads:

Reactives: invalid (NULL) left side of assignment

I also followed this page like below, but got another error:

# 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)

Any solutions?

Simon.S.A.
  • 6,240
  • 7
  • 22
  • 41
Makoto Miyazaki
  • 1,743
  • 2
  • 23
  • 39

1 Answers1

1

Focusing on your first example:

You are trying to assign a value to a reactive object. function_table is a reactive, so you can only get its value not set its value via function_table()[....] <- .....

It is difficult to know exactly how to fix this as you have not posted a minimal reproducible example.

One way to resolve this using a reactive value. This is a value that can be assigned to and changed within the server. Here is an example:

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

The use of isolate(...) may be unnecessary. It is used to prevent reactive values being re-evaluated in the retrieval of its contents.

This example assumes you are loading a fixed/known file at the beginning of your server. If you are loading your file dynamically you will probably need to use an observer to set the current value. For example (pseudo-code):

current = reactiveValues()
current$rhandsontable = NULL

observe(file_name,current$rhandsontable = read.csv(file_name))
Simon.S.A.
  • 6,240
  • 7
  • 22
  • 41