Search code examples
rshinygaugeflexdashboard

R shiny gauge chart


Could someone help me draw a gauge chart in R Shiny?I don`t need it to be dynamic, but to have a KPI that I assign with a value and to show that when I run the app.It should be red from 0 to 0.3, yellow from 0.3 to 0.5 and green from 0.5 to 1.


Solution

  • flexdashboard provides such a gauge chart:

    library(shiny)
    library(flexdashboard)
    
    ui <- fluidPage(
      numericInput("value", label = "Select value", min = 0, max = 1, value = 0.5, step = 0.1),
      gaugeOutput("gauge")
    )
    
    server <- function(input, output) {
    
      output$gauge = renderGauge({
        gauge(input$value, 
              min = 0, 
              max = 1, 
              sectors = gaugeSectors(success = c(0.5, 1), 
                                     warning = c(0.3, 0.5),
                                     danger = c(0, 0.3)))
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    enter image description here