【问题标题】:piechart along with infoBox() shinyApp dataframe error饼图以及 infoBox() shinyApp 数据框错误
【发布时间】:2019-09-21 08:19:13
【问题描述】:

我想根据 infoBox() 中显示的数据来渲染饼图。 shinyApp 应该在信息框和饼图中显示结果。

这是我的代码:

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 中。 有人可以帮忙吗?

【问题讨论】:

    标签: r shiny pie-chart shinydashboard


    【解决方案1】:

    我认为问题出在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" 我怀疑。

    【讨论】:

      猜你喜欢
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多