The below R Shiny code, using package rhandsontable, retains full decimal precision via the function row_decimals(). As a next step I am trying to show these values as percentages while retaining full decimal precision. Everything I have tried using format() or custom JavaScript renderer either rounds or truncates the rendered values, which I don't want. Ideally, any values with less than or equal to 2 decimal places are shown with 2 decimals (for example, 0.20, 0.212, 0.2123 shown as 20.00%, 21.20%, and 21.23%, respectively) and any values with greater than 2 decimal places show all decimals (for example, 0.123456789 shown as 12.3456789%). Any recommendations for how to do this? As illustrated in the image below.
Code:
library(shiny)
library(rhandsontable)
# Calculates decimal places for each value in specified column
row_decimals <- function(df, col_name) {
col <- df[[col_name]]
n_digits <- sapply(col, function(x) {
nchar(gsub(".*\\.|^[^.]+$", "", as.character(x)))
})
return(n_digits)
}
ui <- fluidPage(
br(),
rHandsontableOutput("myTable"),
br(),
textOutput("productOutput")
)
server <- function(input, output) {
values <- reactiveValues(data = data.frame(Values = c(0.5, 0.123456789)))
observeEvent(input$myTable, {values$data <- hot_to_r(input$myTable)})
output$myTable <- renderRHandsontable({
data <- values$data
decimals <- row_decimals(data, 'Values')
data$Values <- mapply(function(x,d){format(x, nsmall = d)},data$Values,decimals)
rhandsontable(data)
})
output$productOutput <- renderText({
product <- as.numeric(values$data$Values[1]) * as.numeric(values$data$Values[2])
paste("Product of the two rows: ", product)
})
}
shinyApp(ui = ui, server = server)


There's a very nice JavaScript library for numbers formatting, namely d3.format. You can use it for a handsontable with the help of the numeric renderer:
I don't understand the line
p = Math.max(0, d3.precisionFixed(0.0005) - 2), I copied it from an example.