使用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.
```