简体   繁体   中英

rhandsontable in shiny: change separator of thousands and decimals

I would like to know how to change the thousand separator to a point and the decimal separator to a comma when using the rhandsontable function.

Here's a small, reproducible example.

library(tidyverse)
library(shiny)
library(shinydashboard)
library(rhandsontable)

data_test <- iris

ui <- dashboardPage(
  skin = "black",
  dashboardHeader(
    title = "Test",
    titleWidth = 100
  ),
  
  dashboardSidebar(collapsed = TRUE),
  
  dashboardBody(
    
    fluidRow(column(
      width = 3,
      selectInput(
        inputId = "specie",
        label = "Specie",
        choices = unique(data_test$Species),
        multiple = FALSE
      )),
    
     rHandsontableOutput("rTable")))
)

server <- function(input, output, session) {
  
  rv <- reactiveValues(table1 = NULL)
  
  observeEvent(input$specie,{
    data <- data_test %>%
      filter(Species == input$specie) %>% 
      mutate(Sepal.Length2 = 0,
             Sepal.Width2 = 0,
             Petal.Length2 = 0,
             Petal.Width2 = 0,
             amount = 50,
             percentage = 100) %>% 
      select(1:4,6:11)
    rv$table1<- data})
  
  observe({
    if (!is.null(input$rTable)){
      mytable <- as.data.frame(hot_to_r(input$rTable))
      mytable[,7] <- mytable[,5]*100
      mytable[,8] <- mytable[,6]*100
      numberofrows <- nrow(mytable)
      rv$table1 <- mytable}
  })
  
  output$rTable <- renderRHandsontable({
    rhandsontable(rv$table1) %>%
      hot_col("Sepal.Length", format = "0.0,0") %>%
      hot_col("Sepal.Width", format = "0.0,0") %>%
      hot_col("Petal.Length", format = "0.0,0") %>%
      hot_col("Petal.Width", format = "0.0,0") %>%
      hot_col("Sepal.Length2", format = "0.0,0") %>%
      hot_col("Sepal.Width2", format = "0.0,0") %>%
      hot_col("Petal.Length2", format = "0.0,0") %>%
      hot_col("Petal.Width2", format = "0.0,0") %>%
      hot_col("amount", format = "$0.0,0") %>%
      hot_col("percentage", format = "0,0") %>% 
      hot_cols(colWidths = 130)
  })
}

shinyApp(ui = ui, server=server)   

I tried the hot_col functions as seen in the example but this doesn't solve my problem, I also tried other functions like prettyNum, format, formatC, etc but I couldn't solve it either. Any help is much appreciated.

I would use the d3.format library, as follows:

library(rhandsontable)
library(shiny)

mydata <- as.data.frame(
  matrix(runif(40, 0, 100000), nrow = 10, ncol = 4)
)

ui <- fluidPage(
  tags$head(
    tags$script(src = "https://cdn.jsdelivr.net/npm/d3-format@3")
  ),
  rHandsontableOutput("mytable")
)

server <- function(input, output) {
  
  output$mytable <- renderRHandsontable({
    rhandsontable(mydata,rowHeaderWidth = 100)%>%
      hot_cols(
        renderer = 'function(instance, td, row, col, prop, value, cellProperties) {
            Handsontable.renderers.NumericRenderer.apply(this, arguments);
            var locale = d3.formatLocale({
              decimal: ",",
              thousands: ".",
              grouping: [3]
            });
            var fformat = locale.format(",");
            td.innerHTML = fformat(value);
        }') 
  })
  
}

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