r语言 - 饼图以及信息框()闪亮的应用程序数据帧错误



我想根据infoBox((中显示的数据渲染饼图。闪亮的应用程序应该在信息框和饼图中显示结果。

这是我的代码:

library(shiny)
library(shinydashboard)
library(ECharts2Shiny)
a <- 10
b <- 20
dat1 <- data.frame(a,b)
colnames(dat1) <- c("Male", "Female")
c <- 20
d <- 20
dat2 <- data.frame(c,d)
colnames(dat2) <- c("Male", "Female")
e <- 30
f <- 20
dat3 <- data.frame(e,f)
colnames(dat3) <- c("Male", "Female")
ui <- shinyUI(dashboardPage(
dashboardHeader(title = "123"),
dashboardSidebar(disable = TRUE),
dashboardBody(
fluidRow(radioButtons("Ben", "Details", c("1","2", "3"), inline = T), 
infoBoxOutput("loc", width = 960)
)))

server <- shinyServer(function(input,output){
output$loc <- renderInfoBox({
if (input$Ben == "1"){
box(h3("1"),
infoBox("Total", 30, "Visits", icon = icon('users'), width = 4),
infoBox("M", 10 ,"Visits", icon = icon('male'), width = 4),
infoBox("F", 20 ,"Visits", icon = icon('female'), width = 4),
loadEChartsLibrary(),  tags$div(id="test1", style="width:60%;height:300px;"),
deliverChart(div_id = "test1"), width = "800px", height = "400px",
renderPieChart(div_id = "test1", data = dat1, show.label = TRUE),
background = "black")       
}
else {if (input$Ben == "2") {
box(h3("2"),
infoBox("Total", 40, "Visits", icon = icon('users'), width = 4),
infoBox("M", 20 ,"Visits", icon = icon('male'), width = 4),
infoBox("F", 20 ,"Visits", icon = icon('female'), width = 4),
loadEChartsLibrary(),  tags$div(id="test2", style="width:60%;height:300px;"),
deliverChart(div_id = "test2"), width = "800px", height = "400px",
renderPieChart(div_id = "test2", data = dat2, show.label = TRUE),
background = "black") 
}    
else{
box(h3("3"),
infoBox("Total", 50 , icon = icon('users'), width = 4),
infoBox("m", 30, icon = icon("male"), width = 4),
infoBox("f", 20,  icon = icon('female'), width = 4),
loadEChartsLibrary(),  tags$div(id="test3", style="width:60%;height:300px;"),
deliverChart(div_id = "test3"), width = "800px", height = "400px",
renderPieChart(div_id = "test1", data = dat1, show.label = TRUE), background ="black")
}}  }}}
})
})
shinyApp(ui,server)

选择单选按钮时,infoBox((和相关饼图应显示在 shinyApp 中。 任何人都可以帮忙吗?

我相信问题出在renderPieChart语句中:

renderPieChart(div_id = "test1", data = dat1, show.label = TRUE)

用于饼图的数据不是renderPieChart所需的格式。正在提供data.frame,但它应采用以下格式:

如果是 data.frame,则数据必须仅由两列组成, "名称"和"值"。"值"列必须是数字或整数。

因此,对于dat1(和其他数据(,您可以执行以下操作(对于 10 名男性,20 名女性(:

dat1 <- data.frame(
name = c("Male", "Female"),
value = c(10, 20)
)

然后它应该绘制饼图。

另请注意,我怀疑第三个饼图应该div_id = "test 3"

相关内容

最新更新