R 反应条形图(带聚集)
我试图在Shiny中向仪表板添加条形图,但在重塑数据时遇到了问题 我想显示每个指标的红色/琥珀色/绿色评级数量,并根据选择的国家和地区做出反应 值框在大部分情况下都能正常工作,但我通过搜索尝试的所有想法都没有导致条形图或错误 我的代码:R 反应条形图(带聚集),r,shiny,bar-chart,reactive,R,Shiny,Bar Chart,Reactive,我试图在Shiny中向仪表板添加条形图,但在重塑数据时遇到了问题 我想显示每个指标的红色/琥珀色/绿色评级数量,并根据选择的国家和地区做出反应 值框在大部分情况下都能正常工作,但我通过搜索尝试的所有想法都没有导致条形图或错误 我的代码: Country <- c('England', 'Scotland', 'Wales', 'Ireland', 'Spain', 'England', 'Scotland', 'Wales', 'Ireland', 'Spain', 'Engl
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`)
国家/地区您需要一些req()
和绘图1中缺少的%%
。您可以删除缺少的RAG
值,并使用scale\u fill\u manual
匹配颜色
server <- function(input, output, session) {
Test <- reactive({
req(input$Country)
if(input$Country == 'All') {
Joined_data
} else {
Joined_data %>%
filter(`Country` == input$Country, `Region` == input$Region)
}})
output$Total <- renderValueBox({
valueBox(req(Test()) %>%
tally(), req(input$Country), color = "olive")
})
output$Value <- renderValueBox({
req(Test())
valueBox(Test() %>%
summarise("Value" = sum(Value)) %>%
#summarise("Value" = sum(`Value (Annualised)`)) %>%
prettyNum(big.mark = ","),
req(input$Country),
color = "olive", icon = icon("pound-sign"))
})
output$plot1 <-renderPlot({
req(Test())
Test() %>%
gather(metric, RAG, Risk) %>%
group_by(metric, RAG) %>%
dplyr::summarise(n = n()) %>% filter(RAG!="") %>%
ggplot(aes(x= metric, y= n, color = RAG, fill = RAG, title = "RAG Rating")) +
geom_bar(stat = "identity", position=position_dodge()) +
scale_fill_manual(values=c("Amber"="goldenrod1","Green"="green","Red"="red")) +
scale_color_manual(values=c("Amber"="goldenrod1","Green"="green","Red"="red"))
})
Country.choice <- reactive({
Joined_data %>%
filter(Country %in% req(input$Country)) %>%
pull(Region)
})
observe({
updateSelectizeInput(session, "Region", choices = Country.choice())
})
}
shiny::shinyApp(ui=ui,server=server)
server%
#总结(“价值”=总和(`Value(按年计算)`))%>%
prettyNum(big.mark=“,”),
请求(输入$Country),
color=“olive”,icon=icon(“磅号”))
})
输出$1%
聚集(度量、RAG、风险)%>%
分组依据(公制,RAG)%>%
dplyr::摘要(n=n())%%>%filter(RAG!=“”)%%>%
ggplot(aes(x=公制,y=n,颜色=碎布,填充=碎布,title=“碎布等级”))+
几何图形栏(stat=“identity”,position=position\u dodge())+
刻度填充手册(数值=c(“琥珀色”=“黄花1”,“绿色”=“绿色”,“红色”=“红色”))+
刻度颜色手册(数值=c(“琥珀色”=“黄花1”,“绿色”=“绿色”,“红色”=“红色”))
})
Country.choice%
筛选器(国家/地区%req(输入$Country))中的国家/地区%>%
拉动(区域)
})
观察({
UpdateSelectionInput(会话,“Region”,choices=Country.choice())
})
}
shiny::shinyApp(ui=ui,server=server)
请提供一些示例数据,谢谢!我现在已经添加了一些示例数据,就是这样。非常感谢你!!你能给我解释一下req()的必要性吗,或者告诉我在什么地方可以读到它吗?它主要是在开始时处理缺少的输入。请看这里:
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)
server <- function(input, output, session) {
Test <- reactive({
req(input$Country)
if(input$Country == 'All') {
Joined_data
} else {
Joined_data %>%
filter(`Country` == input$Country, `Region` == input$Region)
}})
output$Total <- renderValueBox({
valueBox(req(Test()) %>%
tally(), req(input$Country), color = "olive")
})
output$Value <- renderValueBox({
req(Test())
valueBox(Test() %>%
summarise("Value" = sum(Value)) %>%
#summarise("Value" = sum(`Value (Annualised)`)) %>%
prettyNum(big.mark = ","),
req(input$Country),
color = "olive", icon = icon("pound-sign"))
})
output$plot1 <-renderPlot({
req(Test())
Test() %>%
gather(metric, RAG, Risk) %>%
group_by(metric, RAG) %>%
dplyr::summarise(n = n()) %>% filter(RAG!="") %>%
ggplot(aes(x= metric, y= n, color = RAG, fill = RAG, title = "RAG Rating")) +
geom_bar(stat = "identity", position=position_dodge()) +
scale_fill_manual(values=c("Amber"="goldenrod1","Green"="green","Red"="red")) +
scale_color_manual(values=c("Amber"="goldenrod1","Green"="green","Red"="red"))
})
Country.choice <- reactive({
Joined_data %>%
filter(Country %in% req(input$Country)) %>%
pull(Region)
})
observe({
updateSelectizeInput(session, "Region", choices = Country.choice())
})
}
shiny::shinyApp(ui=ui,server=server)