下面的Rshiny 代码使用包rhandsontable,通过函数row_decimals()保持完全的小数精度.作为下一步,我将try 将这些值显示为百分比,同时保持完全的小数精度.我使用format()或定制的JavaScript呈现器try 的所有内容要么舍入,要么截断呈现值,这是我不想要的.理想情况下,任何小于或等于两个小数位的值都显示为两个小数(例如,0.20、0.212、0.2123分别显示为20.00%、21.20%和21.23%),任何大于两个小数位的值都显示为小数(例如,0.123456789显示为12.3456789%).对于如何做到这一点,有什么建议吗?如下图所示.

enter image description here

代码:

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)

推荐答案

有一个非常好的用于格式化数字的JavaScript库,即d3.format.在数字渲染器的帮助下,您可以将其用于手提式桌面:

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  tags$head(
    # https://github.com/d3/d3-format
    tags$script(src = "https://cdn.jsdelivr.net/npm/d3-format@3")
  ),
  br(),
  rHandsontableOutput("myTable"),
  br(),
  textOutput("productOutput")
)

server <- function(input, output) {
  dat <- data.frame(Values = c(0.5, 0.123456789))
  
  output$myTable <- renderRHandsontable({
    rhandsontable(dat) %>% 
      hot_col(
        1,
        renderer = "function(instance, td, row, col, prop, value, cellProperties) {
          Handsontable.renderers.NumericRenderer.apply(this, arguments);
          const p = Math.max(0, d3.precisionFixed(0.0005) - 2);
          const f = d3.format('.' + p + '%');
          td.innerHTML = f(value);
        }"
      )
  })
  
  output$productOutput <- renderText({
    dd <- hot_to_r(input$myTable)
    product <- dd$Values[1] * dd$Values[2]
    paste("Product of the two rows: ", product)
  })
}

shinyApp(ui = ui, server = server)

enter image description here

我不明白第p = Math.max(0, d3.precisionFixed(0.0005) - 2)行,我是从一个例子中抄袭过来的.

R相关问答推荐

如何将具有重复名称的收件箱合并到R中的另一列中,而结果不同?

如何求解arg必须为NULL或deSolve包的ode函数中的字符向量错误

如何将dygraph调用到R Markdown作为一个shiny 的react 对象的参数?

lightgbm发动机在tidymmodels中的L1正则化""

在另存为PNG之前隐藏htmlwidget绘图元素

根据元素和前一个值之间的差值过滤矩阵的元素

使用`Watch()`和`renderUI()`时,不再满足仍出现在SHILINY AFTER条件中的条件输入

为左表中的所有行使用值Fill滚动左连接

将标识符赋给事件序列,避免错误观察

如何使用前缀作为匹配来连接数据帧?

如何在R中使用hmm TMB提前一步预测观察到的状态?

R中时间间隔的大向量与参考时间间隔的相交

将列的值乘以在不同数据集中找到的值

R基于变量组合创建新的指标列

如何判断代码是否在R Markdown(RMD)上下文中交互运行?

R try Catch in the loop-跳过缺少的值并创建一个DF,显示跳过的内容

如何更改包中函数中的参数?

R-如何在ggplot2中显示具有不同x轴值(日期)的多行?

根据排名的顶点属性调整曲线图布局(&Q)

将仪表板中的值框大小更改为Quarto