尝试制作一个饼图(或者最好是圆环图),显示每个类别的总数,并允许在单击时向下钻取以显示每个类别的细节。说得通?
我想我可能不是每个系统设置都正确,因为复制/粘贴标准示例也会呈现一个空白页面。除非那是某种过时的东西。我的系统:Ubuntu 20.04、R 4.0.5、packageVersion("shiny") 1.6.0、shiny-server --version 1.5.16.958 空白示例:https ://plotly-r.com/linking-views-with-shiny .html#drill-down和在 R Shiny 中创建钻取报告(等等)
我目前的尝试(还没有反应,因为我一生都无法弄清楚):
library(shiny)
library(DBI)
library(ggplot2)
library(dplyr)
library(ggiraph)
ui<-fluidPage(
titlePanel("Budget visuals"),
sidebarLayout(
sidebarPanel(
selectInput("fase", "Choose a budget phase:", choices = c("Budget" = "OWB", "Report" = "JV")),
selectInput("jaar", "Choose a year:", choices = c(2021, 2020, 2019, 2018, 2017, 2016, 2015)),
selectInput("vuo", "V/U/O:", choices = c("Verplichtingen" = "V", "Uitgaven" = "U", "Ontvangsten" = "O")),
submitButton("Submit")
),
mainPanel(
h4(textOutput("header")),
girafeOutput("donut"),
tableOutput("view")
)
)
)
server<-function(input, output, session) {
output$header <- renderText({paste0("Visual: ", input$fase, " (", input$vuo, ") ", input$jaar)})
output$donut <- renderGirafe({
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "btabellen",
host = "localhost",
username = "dbuser",
password = "***")
on.exit(dbDisconnect(conn), add = TRUE)
dbGetQuery(conn, 'set character set "utf8"')
data <- dbGetQuery(conn, paste0(
"SELECT naam_begroting as begroting, sum(bedrag_t) as bedrag FROM ", input$fase, " WHERE jaar = ", input$jaar, " AND VUO = '", input$vuo, "' GROUP BY jaar, naam_begroting ORDER BY bedrag DESC;"))
data$fraction <- data$bedrag / sum(data$bedrag)
data$fraclbl <- paste0(round(100 * data$fraction, 1), "%")
data$ymax <- cumsum(data$fraction)
data$ymin <- c(0, head(data$ymax, n=-1))
data$label <- paste0(data$begroting, ": € ", format(data$bedrag, big.mark=".", decimal.mark=","), " (k)")
donut_plot <- ggplot(data, aes(y = bedrag, fill = begroting, data_id = begroting)) +
geom_bar_interactive(
aes(x = 1, tooltip = label),
width = 0.1,
stat = "identity",
show.legend = FALSE
) +
coord_polar(theta = "y") +
theme_void() +
theme(legend.position = "bottom")
girafe(ggobj = donut_plot, opts_selection(type = "single"))
})
output$view <- renderTable({
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "btabellen",
host = "localhost",
username = "dbuser",
password = "***")
on.exit(dbDisconnect(conn), add = TRUE)
dbGetQuery(conn, 'set character set "utf8"')
data <- dbGetQuery(conn, paste0(
"SELECT naam_begroting as Begroting, SUM(bedrag_t) as Bedrag FROM ", input$fase, " WHERE jaar = ", input$jaar, " AND VUO = '", input$vuo, "' GROUP BY jaar, naam_begroting ORDER BY bedrag DESC;"))
}, digits=0)
}
shinyApp(ui=ui, server=server)
所以基本上,我想要实现的是打开页面,其中显示预算的甜甜圈图,显示类别的所有总数。单击类别时,甜甜圈应自行更新以显示刚刚单击的类别的每个子类别的总数。"SELECT artikelnaam, sum(bedrag_t) FROM OWB WHERE jaar=2018 AND VUO='U' AND naam_begroting='Financiën'"
实际上,在给定用户对这些参数的选择的情况下,单击应该更改 SQL 查询。理想情况下,renderTable 应该显示一个列出子类别的嵌套表,但这是针对不同的问题。
有什么想法我可能做错了吗?