2
votes

I am working on a shiny app that responds to some user input and produces a table. I need the data in that table to go to at least 3 decimal places, but renderTable keeps defaulting to 2.

I've tried using the digits = and the round functions, but nothing ever changes the output. Example code below.

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)

I would like for the digits to go to at least 3 decimal places in the var2 column.

1
in the var2 column only ?Stéphane Laurent
That's where it was necessary, but it's fine to have 3 in the var1 column as well. Your answer below works. I think I was missing the comma prior to it.SportSci

1 Answers

2
votes

The digits argument works. To use like this:

  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)