简体   繁体   中英

Check rhandsontable for valid inputs in Shiny

Problem : I have the following sample app in which the user can make changes to an rhandsontable object. I want to check if the modifications that the user is doing is valid. Already implemented: If not valid, the cell color changes to dark red.

Question: Is there a possibility to check in R (not only visually) the whole rhandsontable if it contains of only valid inputs, ie some TRUE/FALSE flag that can be returned and is an attribute of rhandsontable object or some hidden option or so?

library(shiny)
library(rhandsontable)

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

server <- function(input, output, session) {
  
  output$table <- renderRHandsontable(
    rhandsontable(mtcars)
  )
  
  observe({
    str(input$table)
  })
}

shinyApp(ui, server)

You can read in the table with hot_to_r and then do the checks. In the example, if you change one cell to a character, the flag is set to FALSE . This is because a character is returned as NA in the input (I'm not sure why not the character is returned):

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  rHandsontableOutput("table"),
  verbatimTextOutput("flag")
)

server <- function(input, output, session) {
  
  # flag for numeric values
  is_table_ok <- reactiveVal(FALSE)
  
  output$table <- renderRHandsontable(
    rhandsontable(mtcars)
  )
  
  observeEvent(input$table, {
    table_object <- hot_to_r(input$table)
    flag <- !is.na(table_object)
    flag <- purrr::reduce(flag, `&&`)
    is_table_ok(flag)
  })
  
  output$flag <- renderPrint({
    is_table_ok()
  })
}

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM