r - 如何在 R Shiny 中单独更改 renderTable 中列的位数

标签 r shiny

我有一个要在 R Shiny 中显示的表格,使用 renderTable,但我想将第二列的位数更改为 3 位,将第一列和第三列更改为 0 位。这怎么可能?

   output$Table_X <- renderTable({

        outputData <- loadFunc()
        outputData$x
      }, 
      digits = 3)

在outputData <- loadFunc() 行中,创建了列表outputData,其中包含一个名为“X”的数据帧。其中包含三列数字数据。现在渲染表显示的数据如下:

a               b                 c
0.000           1.123             10.000
2.000           2.345             12.000
4.000           5.789             15.000

我希望只有 3 位数字的“b”列,以及 0 位数字的“a”和“c”列。

最佳答案

一种解决方案是使用DT包中的formatRound。这需要使用 dataTableOutputrenderDataTable,并将数据放入 datatable() 中:

library(shiny)
library(tibble)
library(DT)

data <- tibble(
  x = c(1.889, 1.289),
  y = c(3.44898, 4.48349),
  z = c(3.4343, 3.1)
)

ui <- fluidPage(
  dataTableOutput("table")
)

server <- function(input, output, session) {

  output$table <- renderDataTable({
    datatable(data) %>%
      formatRound(columns = 2, digits = 3) %>%
      formatRound(columns = c(1, 3), digits = 0)
  })

}

shinyApp(ui, server)

编辑:如果不想使用dataTableOutputrenderDataTable,可以替换函数formatRound > 通过 formatC (在基础 R 中),但这有点烦人:

library(shiny)
library(tibble)

data <- tibble(
  x = c(1.889, 1.289),
  y = c(3.44898, 4.48349),
  z = c(3.4343, 3.1)
)

ui <- fluidPage(
  tableOutput("table")
)

server <- function(input, output, session) {

  output$table <- renderTable({
    data$x <- formatC(data$x, digits = 0)
    data$z <- formatC(data$z, digits = 0)
    data$y <- formatC(data$y, digits = 3)
    data
  })

}

shinyApp(ui, server)

关于r - 如何在 R Shiny 中单独更改 renderTable 中列的位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61083811/

相关文章:

r - 如何在 R 编程中将日期时间更改为新列中的日期?

r - 多个用户改变 R Shiny 的 react 值

r - 渲染 Shiny 页面时出现数据表问题 <DataTables warning : table id=DataTables_Table_0 - Requested unknown parameter>

r - 使用下载处理程序将 ggplot 图像保存为 Shiny 的

R Shiny 显示公式

r - 在 Travis CI 上构建 R 包时出错“"tlmgr update --self"失败”

python - 在Python中访问大量未排序的数组元素

java - java中调用R-Rcaller

r - 在代码中使用 Markdown Shiny 参数,而不仅仅是在图形生成中

r - 如何在 R 的表中强制包含一个级别?