Rhandsontable decimal places rounded even with format="0.00"


#1

Hi guys,

I am just have problem getting decimal points to be displayed in a shiny app in a rhansontable when values are entered by the user - all values are being rounded to the nearest integer even with format="0.00".

Any help would be greatly appreciated.

library(rhandsontable)
library(shiny)

mat <- diag(x=100, ncol = 4, nrow = 4)
RData <- data.frame(mat)
colnames(RData) <- c("A","B","C","D")
rownames(RData) <- colnames(mat)

ui <- fluidPage(
  
  titlePanel("Test"),
  
  mainPanel(
    rHandsontableOutput("hot")
  )
)

server = function(input, output, session){
  
  values <- reactiveValues()
  
  observe({
    
    if(!is.null(input$hot)){
      
      values$new <- hot_to_r(input$hot)
      
    }else{
      
      values$new <- RData
      
    }
    
    print(values$new)
  })    
  
  
  output$hot <- renderRHandsontable({
    
    
    rhandsontable(values$new,readOnly = FALSE) %>% hot_cols(format = "0.00")
    
  })
  
}

shinyApp(ui, server)

Kind regards,

Dillon


#2

Just FYI, I tried the app and had the same problem. No idea what's going on. Good idea submitting a PR to github: https://github.com/jrowen/rhandsontable/issues/256


#3

Thanks @tmastny,

I still have not found a solution but I do have an idea of why it may be happening. If the supplied data.frame has entries with 2 decimal places in them then any alterations to 2 decimal places are stored. Not sure if rhandsontable is expecting an integer input if the other values appear to be not numeric?

If you try the below code you will see that the values are stored with diagonal value = 100.23 (does not work for 100.00):

library(rhandsontable)
library(shiny)

mat <- diag(x=100.23, ncol = 4, nrow = 4)
RData <- data.frame(mat)
colnames(RData) <- c("A","B","C","D")
rownames(RData) <- colnames(mat)

ui <- fluidPage(
  
  titlePanel("Test"),
  
  mainPanel(
    rHandsontableOutput("hot")
  )
)

server = function(input, output, session){
  
  values <- reactiveValues()
  
  observe({
    
    if(!is.null(input$hot)){
      
      values$new <- hot_to_r(input$hot)
      
    }else{
      
      values$new <- RData
      
    }
    
    print(values$new)
  })    
  
  
  output$hot <- renderRHandsontable({
    
    
    rhandsontable(values$new,readOnly = FALSE) %>% hot_cols(format = "0.00")
    
  })
  
}

shinyApp(ui, server)

Maybe a solution would be to explicitly display 2 decimal places in the original data.frame? I am not sure if this is possible?

Thanks for your help!


#4

I tried your latest app, and I was able to add decimal numbers. This definitely seems like a bug with rhandsontable.