[英]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.我想知道如何在使用 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.我尝试了示例中的 hot_col 函数,但这并没有解决我的问题,我还尝试了其他函数,如 prettyNum、format、formatC 等,但我也无法解决。 Any help is much appreciated.
任何帮助深表感谢。
I would use the d3.format library, as follows:我会使用d3.format库,如下所示:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.