-1

I have a rhandsontable in R Shiny. This table has 5 columns. I am looking for a functionality where if I edit column 1 then I should not be able to edit column 2 and vice-versa, i.e. user should not be able to edit both columns (column 1 and column 2) at the same time. The other columns should remain unaffected.

Can anyone help me with this?

Vishesh Shrivastav
  • 2,079
  • 2
  • 16
  • 34
Prateek
  • 61
  • 6

1 Answers1

1

You can achieve what you want by rendering the Handsontables accordingly. You can use an observeEvent that checks for changes in the handsontable depending on the modified column and set the desired columns to readOnly. For this purpose, I suggest using reactive values and in this way, you can get the column that was changed simply by column_index = input$hot$changes$changes[[1]][[2]].

Hope this helps.

library(rhandsontable)
library(shiny)


ui <- fluidPage(
  mainPanel(
    rHandsontableOutput("hot")
  )
)

server = function(input, output, session){

  df <- data.frame(a = rnorm(10), b = rnorm(10))
  values <- reactiveValues(data = df)


  observeEvent(
    input$hot$changes$changes, # observe any changes in the cells of the rhandsontable
    {

      column_index =input$hot$changes$changes[[1]][[2]] # get the index of the column that was changed
      values$data <- hot_to_r(input$hot) 
      if(column_index == 1){
        output$hot <- renderRHandsontable({
          rhandsontable(values$data) %>%
            hot_col('a', readOnly = T)
        })
      }
      else{
        output$hot <- renderRHandsontable({
          rhandsontable(values$data) %>%
            hot_col('b', readOnly = T)
        })
      }

    }
  )

  output$hot <- renderRHandsontable({
    rhandsontable(values$data)
  })

}

shinyApp(ui, server)