Creating rowwise comparisons and validations for rhandsontable

Hello,

For a Shiny Application I need to make use of the rhandsontable package and for my needs want some row wise checks to be done instead of columwise as you can do with hot_validate_numeric. I know this is not shiny code but it seemed simpler to solve it here first before taking it there.

What I want to achieve:

  1. I only want the final column Selected to be editeable while the min and max cannot be changed.
  2. Values inserted into the Selected need to adhere to the corresponding Min and Max vectors, i.e. If I put 15 into the first row it will be successful as the min is 10 and max 20 while putting 22 I am not able to.
df <- data.frame(Min=c(10,20,30),Max=c(20,30,40),Selected=c(0,0,0))

rhandsontable(df, width = 550, height = 300) # %>%
 # hot_validate_numeric(cols = 1, min = -50, max = 50, exclude = 40) 

Hello,

So this solved it. Converted it into a shiny example:

library(shiny)
library(rhandsontable)

ui <- fluidPage(rHandsontableOutput('table')))

server <- function(input, output,session) {
  df <- eventReactive( input$table,  {
    if (is.null(input$table))  {
      
      dfOld <<- df <- data.frame(Parameter=c('A','B','C'),
                                 Min=c(10,20,30),
                                 Max=c(20,30,40),
                                 Selected=c(0,0,0))
    } else {
      df <- hot_to_r(input$table) 
      if (all(df$Selected >= df$Min |
              df$Selected == 0) & all(df$Selected <= df$Max)) {
        df$Selected <- df$Selected
      } else {
        df$Selected <- dfOld$Selected
      }
      
    }
    dfOld <<- df
    df
  }, ignoreNULL = F)
  
  output$table<-renderRHandsontable({ 
    
    if (is.null(df())) return()
    rhandsontable(df())
  })
  
}

shinyApp(ui, server)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.