首页 > 解决方案 > How can I use a reactive filtered dataset within a gauge plot?

问题描述

I'm relatively new to shiny and flexdashboard, but I'm trying to produce a gauge chart from a filtered dataset.

The goal is to filter a dataset on a variable (in the ReprEx it is gear), and then extract the lowest value in another variable from the filtered dataset (in the ReprEx, I want to find the lowest mpg).

First off, I select the input.

selectInput("x", "Gear", choices = mtcars$gear)

Then I'm currently creating a reactive dataset with a filter for gear in the sidebar.

df <- reactive({
  mtcars %>% filter(gear == input$x)
})

Finally I'm rendering the gauge plot, trying to extract the column mpg into a vector x, and then inputting the min(x) into the gauge chart.

renderPlot({
     x <- df()[,"mpg"]
gauge(min(x), min = 0, max = 50, gaugeSectors(
  success = c(41, 50), warning = c(21, 40), danger = c(0, 20)
))
})

Where in this process am I going wrong?

Thanks.

标签: rshinyflexdashboard

解决方案


你应该使用renderGauge而不是renderPlot

见官方文档


推荐阅读