[英]rhandsontable in shiny: change separator of thousands and decimals
我想知道如何在使用 rhandsontable function 時將千位分隔符更改為點,將小數分隔符更改為逗號。
這是一個可重現的小例子。
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)
我嘗試了示例中的 hot_col 函數,但這並沒有解決我的問題,我還嘗試了其他函數,如 prettyNum、format、formatC 等,但我也無法解決。 任何幫助深表感謝。
我會使用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.