• 使用shiny包制作自动化树状图


    使用shiny包制作自动化树状图

    代码

    library(shiny)
    library(ggplot2)
    library(dplyr)
    library(treemapify)
    
    ui <- fluidPage(
      titlePanel("树状图"),
      sidebarLayout(
        sidebarPanel(
          fileInput("file", "选择一个.csv文件", accept = ".csv"),
          selectInput("fill", "选择分类变量", choices = c(), multiple = FALSE),
          selectInput("area", "选择每个水平的数量", choices = c(), multiple = FALSE),
          selectInput("label", "选择标签", choices = c(), multiple = FALSE)
        ),
        mainPanel(
          plotOutput("treemap")
        )
      )
    )
    
    server <- function(input, output, session) {
      # 数据导入
      data <- reactive({
        if (is.null(input$file)) {
          NULL
        } else {
          read.csv(input$file$datapath, header = TRUE, stringsAsFactors = FALSE)
        }
      })
      
      # 下拉框更新
      observe({
        if (!is.null(data())) {
          updateSelectInput(session, "fill", choices = names(data()))
          updateSelectInput(session, "area", choices = names(data()))
          updateSelectInput(session, "label", choices = names(data()))
        }
      })
      
      # 编制绘图代码
      output$treemap <- renderPlot({
        if (is.null(data())) return(NULL)
        
        ggplot(data(), aes(x = "", y = "", 
                           fill = !!sym(input$fill),
                           area = !!sym(input$area), 
                           label = !!sym(input$label))) + 
          geom_treemap() + 
          geom_treemap_text() +
          theme(legend.position = "none")
      })
    }
    
    shinyApp(ui, server)
    

    报错

    
    ```r
    
    Listening on http://127.0.0.1:4086
    Warning in geom_treemap() :
      All aesthetics have length 1, but the data has 22 rows.
    ℹ Did you mean to use `annotate()`?
    Warning in geom_treemap_text() :
      All aesthetics have length 1, but the data has 22 rows.
    ℹ Did you mean to use `annotate()`?
    Warning: Error in geom_treemap: Problem while setting up geom.
    ℹ Error occurred in the 1st layer.
    Caused by error in `compute_geom_1()`:
    ! `geom_treemap()` requires the following missing aesthetics: area.
      196: 
      195: signalCondition
      194: signal_abort
      193: rlang::abort
      192: cli::cli_abort
      191: handlers[[1L]]
      190: 
      189: signalCondition
      188: signal_abort
      187: rlang::abort
      186: cli::cli_abort
      185: check_required_aesthetics
      184: compute_geom_1
      183: l$compute_geom_1
      182: f
      175: by_layer
      174: ggplot_build.ggplot
      172: print.ggplot
      167: func
      165: f
      164: Reduce
      155: do
      154: hybrid_chain
      126: drawPlot
      112: 
       96: drawReactive
       83: renderFunc
       82: output$treemap
        1: runApp
    Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
    Warning: Error in geom_treemap: Problem while converting geom to grob.
    ℹ Error occurred in the 1st layer.
    Caused by error in `-data[[area]]`:
    ! 一进列运算符的参数不对
      206: 
      205: signalCondition
      204: signal_abort
      203: rlang::abort
      202: cli::cli_abort
      201: handlers[[1L]]
      200: h
      199: .handleSimpleError
      198: order
      195: treemap_f
      194: do_layout
      193: 
      191: draw_panel
      190: self$draw_panel
      188: FUN
      187: lapply
      186: draw_layer
      185: self$geom$draw_layer
      184: draw_geom
      183: l$draw_geom
      182: f
      175: by_layer
      174: ggplot_gtable.ggplot_built
      172: print.ggplot
      167: func
      165: f
      164: Reduce
      155: do
      154: hybrid_chain
      126: drawPlot
      112: 
       96: drawReactive
       83: renderFunc
       82: output$treemap
        1: runApp
    Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
    Listening on http://127.0.0.1:4086
    Warning in geom_treemap() :
      All aesthetics have length 1, but the data has 22 rows.
    ℹ Did you mean to use `annotate()`?
    Warning in geom_treemap_text() :
      All aesthetics have length 1, but the data has 22 rows.
    ℹ Did you mean to use `annotate()`?
    Warning: Error in geom_treemap: Problem while setting up geom.
    ℹ Error occurred in the 1st layer.
    Caused by error in `compute_geom_1()`:
    ! `geom_treemap()` requires the following missing aesthetics: area.
      196: 
      195: signalCondition
      194: signal_abort
      193: rlang::abort
      192: cli::cli_abort
      191: handlers[[1L]]
      190: 
      189: signalCondition
      188: signal_abort
      187: rlang::abort
      186: cli::cli_abort
      185: check_required_aesthetics
      184: compute_geom_1
      183: l$compute_geom_1
      182: f
      175: by_layer
      174: ggplot_build.ggplot
      172: print.ggplot
      167: func
      165: f
      164: Reduce
      155: do
      154: hybrid_chain
      126: drawPlot
      112: 
       96: drawReactive
       83: renderFunc
       82: output$treemap
        1: runApp
    Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
    Warning: Error in geom_treemap: Problem while converting geom to grob.
    ℹ Error occurred in the 1st layer.
    Caused by error in `-data[[area]]`:
    ! 一进列运算符的参数不对
      206: 
      205: signalCondition
      204: signal_abort
      203: rlang::abort
      202: cli::cli_abort
      201: handlers[[1L]]
      200: h
      199: .handleSimpleError
      198: order
      195: treemap_f
      194: do_layout
      193: 
      191: draw_panel
      190: self$draw_panel
      188: FUN
      187: lapply
      186: draw_layer
      185: self$geom$draw_layer
      184: draw_geom
      183: l$draw_geom
      182: f
      175: by_layer
      174: ggplot_gtable.ggplot_built
      172: print.ggplot
      167: func
      165: f
      164: Reduce
      155: do
      154: hybrid_chain
      126: drawPlot
      112: 
       96: drawReactive
       83: renderFunc
       82: output$treemap
        1: runApp
    Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
    
    

    ```

    展开全部

    • 香格里辣 2024-10-13 19:44
      关注

      以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

      你的代码看起来是用来创建一个交互式web应用程序来生成树状图的,使用了shiny包。然而,你在运行代码时遇到了一些错误。这些错误主要涉及到ggplot2的geom_treemap函数的使用和数据格式问题。

      以下是解决这些问题的建议:

      1. 数据格式问题:错误信息中提到你的数据有22行,而geom_treemap函数需要的 aesthetic(美学属性)只有长度1。这可能是因为你在使用geom_treemap函数时没有正确地指定数据列。你需要确保你指定的列(如fill, area, label)在数据中是存在的,并且具有正确的格式和长度。
      2. 更新下拉框选择:你的代码里使用了下拉框来选择数据中的某些列作为树状图的参数,但你可能没有正确地更新这些选择以反映数据的变化。在数据改变时,你需要更新下拉框的选择项。你已经使用observe函数来做到这一点,这是正确的。
      3. 树状图参数问题:geom_treemap函数需要特定的参数(如fill, area)来创建树状图。你需要确保你提供的这些参数在数据中是存在的,并且具有正确的格式。在你的错误日志中,程序在尝试获取这些参数时遇到问题。你需要检查你的数据,确保这些参数的存在和正确性。
      4. jsonlite警告:关于jsonlite的警告信息表示你正在使用一个将来会被改变的选项(keep_vec_names=TRUE)。按照警告信息的建议,如果你想要JSON对象输出,最好使用一个命名列表而不是一个命名向量。

      基于上述建议,我建议你检查你的数据格式和确保你在使用geom_treemap函数时提供了正确的参数。此外,你可能需要调整你的代码以处理可能出现的空数据情况(例如,当没有选择任何文件时)。如果你能提供你的数据样例,我可以提供更具体的帮助。

    • 相关阅读:
      SpringCloud - Spring Cloud Netflix 之 Hystrix Dashboard仪表盘监控(九)
      leetcode刷题——单链表
      Go:Bitwise按位算法(附完整源码)
      wps演示时图片任意位置拖动
      第十四届蓝桥杯第二期模拟赛题解
      苹果iPhone14怎么领取淘宝天猫优惠券购买iPhone苹果14钢化膜手机壳?
      OpenEuler 部署kubesphere网络配置防火墙规则 开放端口访问 确保基础设施组件可以通过特定端口相互通信
      山西佳诺德:短视频有哪些类型
      nrm 使用详解
      【无标题】
    • 原文地址:https://ask.csdn.net/questions/8151658