【发布时间】:2021-02-16 05:11:52
【问题描述】:
我正在尝试在 Shiny 中向我的仪表板添加条形图,但在重塑数据时遇到了问题。
我想显示每个指标的红色/琥珀色/绿色评级的数量,并根据所选的国家和地区做出反应。
值框大部分都在工作,但我通过搜索尝试的所有想法都没有导致条形图或错误。
我的代码:
Country <- c('England', 'Scotland', 'Wales', 'Ireland', 'Spain', 'England', 'Scotland', 'Wales', 'Ireland', 'Spain', 'England', 'Scotland', 'Wales', 'Ireland', 'Spain' , 'England', 'Scotland', 'Wales', 'Ireland', 'Spain')
Region <- c('North' , 'East', 'South', 'South', 'North' , 'South', 'East', 'North' , 'South', 'West', 'North' , 'South' , 'North' , 'West', 'North' , 'West', 'West', 'East', 'East', 'South')
Value <- c(100, 150, 400, 300, 100, 150, 300, 200, 500, 600, 300, 200, 250, 300, 100, 150, 300, 200, 500, 600)
Outcomes <- c('Green', 'Red','' , 'Amber', 'Green', 'Green', 'Red','' , 'Red', 'Amber', 'Red', 'Green', 'Green', 'Green','' ,'' , 'Amber', 'Amber', 'Red', 'Red')
Outputs <- c('Red', 'Green', 'Green', 'Green', '','' , 'Amber', 'Green', 'Red','' , 'Red', 'Amber', 'Red', 'Green', 'Green', '','' , 'Amber', 'Amber', 'Red')
Risk <- c('Green', 'Green', 'Red', 'Red','' , 'Amber', 'Green', 'Green', 'Amber','' , 'Green', 'Red', 'Amber', 'Green', 'Green', 'Red', '', 'Red', 'Amber', '')
Joined_data <- data.frame(Country, Region, Value, Outcomes, Outputs, Risk)
list<- unique(Joined_data$Country)
list2 <- unique(Joined_data$`Region`)
用户界面
ui<- dashboardPage(
dashboardHeader(title = "Performance", titleWidth = 800),
dashboardSidebar(selectizeInput(inputId = "Country", label = "Country", choices = c('All', list)),
(selectizeInput(inputId = "Region", label = "Region", choices = c('All', list2)))),
dashboardBody(
fluidRow(
box(valueBoxOutput(outputId = "Total", width = 12), title = "Total"),
box(valueBoxOutput(outputId = "Value", width = 12), title = "Value"),
plotOutput(outputId = "plot1", width = 600 , height = 600), title = "Metric RAG Rating",
)
),
)
server <- function(input, output, session) {
Test <- reactive({
if(input$Country == 'All') {
Joined_data
} else {
Joined_data %>%
filter(`Country` == input$Country, `Region` == input$Region)
}})
output$Total <- renderValueBox({
valueBox(Test() %>%
tally(),
req(input$Country),
color = "olive")
})
output$Value <- renderValueBox({
valueBox(Test() %>%
summarise("Value" = sum(`Value (Annualised)`)) %>%
prettyNum(big.mark = ","),
req(input$Country),
color = "olive", icon = icon("pound-sign"))
})
output$plot1 <-renderPlot({
Test() %>%
gather(metric, RAG, Outcomes:Risk) #%>%
group_by(metric, RAG) %>%
dplyr::summarise(n = n())
ggplot(data= Test(), aes(x= metric, y= n, color = RAG, fill = RAG, title = "RAG Rating")) +
geom_bar(stat = "identity", position=position_dodge())
req(input$Region)
})
Country.choice <- reactive({
Joined_data %>%
filter(`Country` %in% input$Country) %>%
pull(`Region`)
})
observe({
updateSelectizeInput(session, "Region", choices = Country.choice())
})
}
shiny::shinyApp(ui=ui,server=server)
我收到一个错误 - 找不到对象“指标”。所以肯定和gather()有关
有人有什么想法吗?
【问题讨论】:
-
请提供一些示例数据,谢谢!
-
我现在添加了一些示例数据
标签: r shiny bar-chart reactive