如何阻止renderTable从默认设置为小数点后2位?

时间:2019-09-11 14:03:42

标签: r shiny

我正在开发一个闪亮的应用程序,该应用程序响应一些用户输入并生成一个表格。我需要该表中的数据至少移至小数点后三位,但renderTable仍默认为2。

我尝试使用digits =和round函数,但是没有任何更改输出。下面的示例代码。

library(shiny)

x1 <- c("red", "orange", "yellow", "green", "blue", "indigo", "violet")

ui <- fluidPage(
  fluidPage(title = "Example Shiny"),
  sidebarLayout(
    sidebarPanel(
      numericInput("rank", "Rank 1 - 7", value = 3, min =1, max = 7),
      selectInput("color", "Color", x1), 
      numericInput("x2", "Another Variable (enter any number)", value = 0, min = 0, max = 500)
    ),
    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Table", tableOutput("table"))
      )
    )
  )
)
server <- function(input, output, session) {
  color_table <- data.frame('red' = c(1,2,3,4,5,6,7,8,9,10,0,0,0,0,0,0,0,0,0,0,0), 
                             'orange' = c(0.1,0.2,0.3,0.4,0.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
                             "yellow" = c(10,20,30,40,50,60,70,80, 0, 0,0,0,0,0,0,0,0,0,0,0,0), 
                             "green" = c(0.1,0.2,0.3,0.4,0.5, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),  
                             "blue" = c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0,0), 
                             "indigo" = c(0.055,0.105,0.205,0.305,0.405,0.505,0.605,0.705,0.805,0.905,1.005,1.105,1.205,1.305,1.405,1.505,1.605,1.705,1.805,1.905,2.05), 
                             "violet" = c(0.055,0.105,0.155,0.205,0.250,0.300,0.350,0.400,0,0,0,0,0,0,0,0,0,0,0,0,0))
  rank <- reactive({
    get(input$rank, inherits = FALSE)
  })

  output$table <- renderTable({

    var1 = color_table[, input$color]

    var2 = if(input$color == "red" | input$color == "orange") {
      ((var1 * as.numeric(input$rank) * 60)/ as.numeric(input$x2))/1000
    } else {
      var1 *  as.numeric(input$rank) / as.numeric(input$x2)
    }
    data.frame(Var1 = var1, Var2 = var2)
  })
}

shinyApp(ui, server)

我希望数字在var2列中至少移至小数点后3位。

1 个答案:

答案 0 :(得分:2)

digits参数起作用。要这样使用:

  output$table <- renderTable({

    var1 = color_table[, input$color]

    var2 = if(input$color == "red" | input$color == "orange") {
      ((var1 * as.numeric(input$rank) * 60)/ as.numeric(input$x2))/1000
    } else {
      var1 *  as.numeric(input$rank) / as.numeric(input$x2)
    }
    data.frame(Var1 = var1, Var2 = var2)
  }, digits = 3)