【问题标题】:How to display numbers in renderTable with the correct number of decimal places?如何使用正确的小数位数在 renderTable 中显示数字?
【发布时间】:2017-10-31 20:39:45
【问题描述】:

在我的shiny 应用程序中,我有一个renderTable,其中包含整数和十进制数。我的目标是显示没有任何小数位的整数,即。 e. 1 应显示为“1”,所有小数位的十进制数,即。 e. 1.2345 应显示为“1.2345”。我应该提到,这些数字源于用户输入。因此,我事先不知道它们会有多少个小数位。请看下面的例子:

library(shiny)

ui <- fluidPage(
  numericInput("number", "enter a number", value = 1.2345),
  tableOutput("table"))

server <- function(input, output, session) {
  output$table <- renderTable({data.frame(c(1, input$number))}, digits = 0)}

shinyApp(ui, server)

如您所见,我知道 renderTabledigits 参数,但我不知道如何在我的情况下使用它。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    将输出包装在as.character中:

    server <- function(input, output, session) {
      output$table <- renderTable({
        data.frame(as.character(c(1, input$number)))
      }, rownames = T)
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      相关资源
      最近更新 更多