m2geval / r /colors_function_bench.jsonl
Tswatery's picture
Add files using upload-large-folder tool
f18999e verified
Raw
History Blame Contribute Delete
122 kB
{"repo_name": "colors", "file_name": "/colors/app.R", "inference_info": {"prefix_code": "library(shiny)\nlibrary(purrr)\nlibrary(scales)\nlibrary(ggplot2)\nlibrary(stringr)\nlibrary(magrittr)\nlibrary(reactable)\nlibrary(gridExtra)\nlibrary(htmltools)\nlibrary(data.table)\nlibrary(shinyWidgets)\nlibrary(colourpicker)\nlibrary(shinydashboard)\n\nui <- dashboardPage(\n dashboardHeader(title = \"科研绘图配色推荐器\",titleWidth = \"100%\"),\n dashboardSidebar(disable = TRUE, collapsed = TRUE),\n dashboardBody(\n uiOutput(\"uipanel\"),\n tags$hr(),\n tags$div(align = \"center\", \n tags$p(\"\\ua9 2021-2024, Lcpmgh, All rights reserved.\", style=\"height:8px\"),\n tags$div(align = \"center\",\n actionLink(inputId = \"\", label = \"lcpmgh \", icon = icon(\"github\"), onclick =\"window.open('https://github.com/lcpmgh')\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n actionLink(inputId = \"\", label = \"lcpmgh@gmail.com\", icon = icon(\"envelope\"), onclick =\"window.location.href='mailto:lcpmgh@gmail.com'\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n actionLink(inputId = \"\", label = \"lcpmgh.com\", icon = icon(\"home\"), onclick =\"window.location.href='http://lcpmgh.com/'\")\n ),\n tags$div(align = \"center\",\n tags$a(\"冀ICP备2022003075号\", target=\"_blank\", href=\"https://beian.miit.gov.cn\", style=\"color:#06c; display:inline;\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n # tags$img(src=\"gaba.png\"),\n tags$a(\"川公网安备51010702002736\", target=\"_blank\", href=\"http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=51010702002736\", style=\"color:#06c; display:inline;\")\n )\n )\n )\n)\n\nserver <- function(input, output, session){\n # 数据\n colors <- readLines(\"@colors.txt\") %>% str_split(\",\") %>% lapply(., sort) %>% .[!duplicated(.)] #读取、排序、去重\n colors_nasc <- map_int(colors, length) %>% order() %>% colors[.] #按子颜色数量排序\n colors_table <- data.frame(col_id=1:length(colors_nasc), col_num=map_int(colors_nasc, length)) #颜色序号数量表\n setDT(colors_table)\n colors_sect <- colors_table[, c(min(.SD),max(.SD)), .SDcols=1,by=\"col_num\"] %>% \n .[, type:=rep(c(\"min\", \"max\"), nrow(.)/2)] %>% \n dcast(., col_num~type, value.var = \"V1\") %>% \n rbind(data.table(col_num=\"all\", \n max=max(colors_table$col_id), \n min=min(colors_table$col_id)), .) #提取每个数量区间的id范围 \n # 函数\n examp_plot <- function(id, colors_nasc=NULL, custom=c(\"#F5A889\", \"#ACD6EC\"), alp=0.1){\n # 函数,根据给定颜色id或自定义的颜色,画4个案例图\n # id=0时自定义颜色,否则按colors_nasc中的颜色\n if(id == 0){\n tcolor <- custom\n ncolor <- length(tcolor)\n } else{\n tcolor <- colors_nasc[[id]]\n ncolor <- length(tcolor)\n }\n # 图1.bar\n dat_bar <- data.frame(a=sample(letters, ncolor, replace = F), \n b=runif(ncolor, 7, 10))\n p_bar <- ggplot(dat_bar, aes(a, b, fill=a))+\n geom_bar(color=\"black\", stat = \"identity\", alpha=alp)+\n scale_y_continuous(expand = c(0,0,0,0.1))+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Bar Chart with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n # 图2.box\n dat_box <- data.frame(a=sample(letters, ncolor, replace = F),\n b=runif(ncolor*20, 7, 10))\n p_box <- ggplot(dat_box, aes(a, b, fill=a))+\n stat_boxplot(geom = \"errorbar\", linewidth=0.8, width = 0.3)+\n geom_boxplot(alpha=alp)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Boxplot with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n # 图3.point\n dat_point <- data.frame(a=rep(runif(30), ncolor),\n b=rep(runif(30), ncolor),\n t=rep(sample(letters, ncolor, replace = F), 30))\n p_point <- ggplot(dat_point, aes(a, b, color=t, fill=t))+\n geom_point(shape=21, size=5, alpha=alp)+\n scale_color_manual(values = tcolor)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"scatterplot without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n # 图4.line\n dat_line <- data.frame(a=rep(1:20, ncolor),\n b=rep(1:ncolor, each=20)+rnorm(20*ncolor, 0, 0.3), \n t=rep(sample(letters, ncolor), each=20))\n p_line <- ggplot(dat_line, aes(a, b, color=t, group=t))+\n geom_line(linewidth=1, alpha=alp)+\n scale_color_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Line chart without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n # 合并\n p <- grid.arrange(p_bar, p_box, p_point, p_line, padding=0, nrow = 2, ncol = 2)\n return(p)\n } \n iscolors <- ", "suffix_code": "\n \n ##### ui #####\n output$uipanel <- renderUI({\n tagList(\n tags$head(tags$link(rel = \"shortcut icon\", href = \"pmgh.ico\")),\n tags$style(HTML(\".custom-margin {margin-bottom: 20px;}\")),\n tags$div(class = \"custom-margin\",\n HTML(\"<h3 style='display: inline;'>方案选择</h3>\"),\n HTML(paste(\"<h5 style='display: inline;'>(数据库内现有\", length(colors_nasc), \"种配色方案)</h5>\"))),\n radioButtons(inputId = \"showtype\",\n label = NULL,\n choices = list(\"配色数据库方案id\" = \"id\", \"自定义配色方案\" = \"custom\"),\n inline = T,\n selected = c(\"按数量\" = \"id\")),\n # 按id\n conditionalPanel(condition = \"input.showtype == 'id'\",\n selectInput(inputId = \"num_select\", \n label = \"选色颜色数量\", \n choices = colors_sect$col_num,\n selected = \"all\",\n multiple = FALSE),\n div(style = \"display: flex; align-items: center; width:700px\",\n sliderTextInput(inputId = \"id_select\", \n label = \"选择方案id\", \n choices = seq(colors_sect[[1, \"min\"]], colors_sect[[1, \"max\"]]),\n selected = colors_sect[[1, \"min\"]],\n width = \"500px\", \n grid = T),\n div(style = \"width: 15%; text-align: center; padding-left: 20px;\", \n shiny::actionButton(inputId = \"pre\", \n label = \"上一个\",\n icon = icon(\"angle-left\"))),\n div(style = \"width: 15%; text-align: center;\", \n shiny::actionButton(inputId = \"nex\", \n label = \"下一个\",\n icon = icon(\"angle-right\"))))),\n # 自定义\n conditionalPanel(condition = \"input.showtype == 'custom'\",\n div(style = \"display: flex; align-items: center; width:700px\",\n colourInput(inputId = \"sele_col\", \n label = \"追加颜色\", \n allowTransparent = T,\n width = \"174px\",\n value = \"skyblue\"),\n div(style = \"width: 15%; text-align: center; padding-left: 20px; padding-top: 10px;\", \n shiny::actionButton(inputId = \"add_col\", \n label = \"加入\",\n icon = icon(\"plus\"))),\n div(style = \"width: 15%; text-align: center; padding-left: 10px; padding-top: 10px;\", \n shiny::actionButton(inputId = \"reset_col\", \n label = \"重置\",\n icon = icon(\"redo\")))),\n textInput(inputId = \"col_custom\",\n label = \"自定义颜色(HEX码,多个颜色以逗号、顿号、空格、分号间隔,颜色数量不可超过16,结果将去重):\",\n width = \"1000px\",\n value = \"#4DBBD5, #00A087, #E64B35\")),\n # 选择结果\n h3(\"所选配色方案\"),\n reactableOutput(outputId = \"colors_info\"),\n # h3(\"颜色透明度\"),\n sliderTextInput(inputId = \"color_alpha\",\n label = \"颜色透明度(颜色图层的alpha值)\", \n choices = seq(0,1, by=0.05),\n selected = 1,\n grid = T,\n width = \"500px\"),\n div(style = \"display: flex; align-items: center; width:1021px\",\n div(style = \"width: 50%; text-align: center;\", \n h3(\"绘图效果\"),\n plotOutput(outputId = \"plot_example\", width = \"510px\", height = \"400px\")),\n div(style = \"width: 50%; text-align: center;\", \n h3(\"方案样式\"),\n plotOutput(outputId = \"plot_color\", width = \"510px\", height = \"400px\"))),\n h3(\"配色数据库(点击表格显示绘图效果)\"),\n tags$head(tags$style(HTML(\".reactable-hover .rt-tr-group:hover {cursor: pointer;}\"))), \n reactableOutput(outputId = \"colors_db\")\n )\n })\n \n ##### server #####\n # 点击颜色数量,更新下面sliderTextInput中备选id的区间\n observeEvent(input$num_select, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n updateSliderTextInput(session, \"id_select\",\n choices = selected_seq,\n selected = selected_seq[1])\n })\n # 点击上一个或下一个后,更新slider对应的值\n observeEvent(input$pre, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n newValue <- max(selected_seq[1], as.numeric(input$id_select) - 1)\n updateSliderTextInput(session, \"id_select\", selected = newValue)\n })\n observeEvent(input$nex, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n newValue <- min(tail(selected_seq,1), as.numeric(input$id_select) + 1)\n updateSliderTextInput(session, \"id_select\", selected = newValue)\n })\n # 点击加入或重置颜色,更新textinput\n observeEvent(input$add_col, {\n newValue <- str_split(input$col_custom, \"[,,;、 ]\") %>%\n unlist() %>%\n str_trim() %>%\n .[nchar(.) > 0] %>%\n c(., input$sele_col) %>%\n .[!duplicated(.)] %>%\n paste0(collapse = \",\")\n updateTextInput(session, \"col_custom\", value = newValue)\n })\n observeEvent(input$reset_col, {\n newValue <- \"#4DBBD5, #00A087, #E64B35\"\n updateTextInput(session, \"col_custom\", value = newValue)\n })\n \n # 动态数据\n rv <- reactiveValues() #创建一个反应值对象rv,用于存储反应性的数据\n rv$value <- 1 #初始值为1\n observeEvent(c(input$id_select, input$showtype), {\n # 监听多个位置,如果有变动,则运行下面的内容,生成显示颜色方案的id\n if(input$showtype == \"id\"){\n # 如果此时页面为按id,则按选择的id更新id\n id <- input$id_select %>% as.numeric()\n rv$value <- id\n }\n })\n \n # table.1:所选配色方案\n output$colors_info <- renderReactable({\n showtype <- input$showtype\n custtext <- input$col_custom\n if(showtype == \"id\"){\n # 如果非自定义,则按id显示方案信息\n id <- rv$value\n colo_inf <- data.frame(id=id, \n colors_n=length(colors_nasc[[id]]), \n colors_hex=paste0(colors_nasc[[id]], collapse = \", \"),\n colors_show=paste0(colors_nasc[[id]], collapse = \", \"))\n } else{\n # 如果自定义\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n # 如果输入的字符不是颜色,则显示ERROR\n colo_inf <- data.frame(id=0, \n colors_n=\"ERROR\", \n colors_hex=\"ERROR\",\n colors_show=\"FFFFFF\")\n } else{\n # 如果未输入或输入的字符是颜色,则显示自定义的颜色信息\n colo_inf <- data.frame(id=0, \n colors_n=length(colsig), \n colors_hex=paste0(colsig, collapse = \", \"),\n colors_show=paste0(colsig, collapse = \", \"))\n }\n }\n # 显示表格\n reactable(colo_inf,\n columns = list(\n id = colDef(name = \"方案id\", width = 60, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_n = colDef(name = \"所含颜色数\", width = 90, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_hex = colDef(name = \"颜色HEX码\", width = 650, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_show=colDef(name = \"颜色预览\", align = \"center\", cell = function(value) {\n color_list <- strsplit(value, \", \")[[1]]\n color_divs <- lapply(color_list, function(color) {\n tags$div(style = paste(\"width: 20px; height: 20px; background-color:\", color, \"; display: inline-block; margin-right: 5px; border: 1px solid black;\"))\n })\n do.call(tagList, color_divs)\n }, style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\"))\n ),\n sortable = F, \n resizable = F,\n showPageSizeOptions = F,\n highlight = F,\n striped = T,\n bordered = T,\n compact = F,\n width = \"1020px\",\n fullWidth = F)\n })\n \n # fig.1:绘图效果\n output$plot_example <- renderPlot({\n showtype <- input$showtype\n custtext <- input$col_custom\n coloralp <- input$color_alpha %>% as.numeric()\n if(showtype == \"id\"){\n id <- rv$value\n pic <- examp_plot(id, colors_nasc, alp = coloralp)\n } else{\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n pic <- ggplot(data=NULL, aes(1,1))+\n geom_text(label=\"ERROR\", hjust=0.5, vjust=0.5, size=8)+\n theme_void()\n } else{\n pic <- examp_plot(0, NULL, colsig, alp = coloralp)\n }\n }\n return(pic)\n }) \n \n # fig.2:方案样式\n output$plot_color <- renderPlot({\n showtype <- input$showtype\n custtext <- input$col_custom\n if(showtype == \"id\"){\n id <- rv$value\n pic <- show_col(colors_nasc[[id]])\n } else{\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n pic <- ggplot(data=NULL, aes(1,1))+\n geom_text(label=\"ERROR\", hjust=0.5, vjust=0.5, size=8)+\n theme_void()\n } else{\n pic <- show_col(colsig)\n }\n }\n return(pic)\n }) \n \n # table.2:配色数据库详情\n output$colors_db <- renderReactable({\n showtype <- input$showtype\n colnum <- colors_sect[col_num == input$num_select,] #根据所选颜色数量更新数据库表格\n id_min <- colnum$min\n id_max <- colnum$max\n colo_db <- data.table(id=colors_table$col_id,\n colors_n=colors_table$col_num,\n colors_hex=map_chr(colors_nasc, ~paste0(.x, collapse = \", \")),\n colors_show=map_chr(colors_nasc, ~paste0(.x, collapse = \", \"))) %>% \n .[id>=id_min&id<=id_max,]\n class_name <- if (input$showtype == \"id\") \"reactable-hover\" else \"\" #当用户选择id时,才有hover属性\n reactable(colo_db,\n columns = list(\n id = colDef(name = \"方案id\", width = 60, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_n = colDef(name = \"所含颜色数\", width = 90, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_hex = colDef(name = \"颜色HEX码\", width = 650, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_show=colDef(name = \"颜色预览\", align = \"center\", cell = function(value) {\n color_list <- strsplit(value, \", \")[[1]]\n color_divs <- lapply(color_list, function(color) {\n tags$div(style = paste(\"width: 20px; height: 20px; background-color:\", color, \"; display: inline-block; margin-right: 5px; border: 1px solid black;\"))\n })\n do.call(tagList, color_divs)\n }, style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\"))\n ),\n class = class_name,\n language = reactableLang(searchPlaceholder = \"查找...\"),\n sortable = F,\n resizable = F,\n showPageSizeOptions = T,\n searchable = T,\n highlight = T,\n striped = T,\n bordered = T,\n compact = F,\n width = \"1020px\",\n fullWidth = F,\n theme = reactableTheme(searchInputStyle = list(\"margin-top\" = \"7px\", \"margin-right\" = \"7px\")),\n onClick = if(showtype != \"custom\"){\n JS(\"function(rowInfo, colInfo) {\n if (window.Shiny) {\n Shiny.setInputValue('selected_row', rowInfo.index + 1);\n window.scrollTo({\n top: 0, \n left: 0, \n behavior:'smooth'}); // 平滑滚动到页面顶部\n }\n }\")}\n )\n })\n \n # 点击表格更新绘图效果\n observeEvent(input$selected_row, {\n selected_row <- input$selected_row\n if (!is.null(selected_row)) {\n colnum <- colors_sect[col_num == input$num_select,] #所选颜色数量对应的id区间\n selected_id <- seq(colnum$min, colnum$max)[selected_row] #提取id区间,点击的行号,对应的准确id\n updateSliderTextInput(session, \"id_select\", selected = selected_id) #更新selected\n }\n })\n \n}\n\n# 输出app\nshinyApp(ui, server)", "middle_code": "function(str){\n if(nchar(str_trim(str))<1) return(F) \n colo <- str_split(str, \"[,,;、 ]\") %>% unlist() %>% str_trim() %>% .[nchar(.) > 0] %>% .[!duplicated(.)]\n sig <- str_detect(colo, \"^\n if(any(!sig) | length(colo)>16){\n return(F)\n } else{\n return(colo)\n }\n }", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "r", "sub_task_type": null}, "context_code": [], "task_instance_info": {"created_time": "2025-08-21 01:11:52", "created_task_model": "DeepSeek-R1", "function_skeleton": "function(str) {}", "code_description": "This code segment functions to: Validate and sanitize user-provided color code strings by checking hexadecimal format compliance, removing duplicates, and enforcing length constraints while splitting input into standardized components."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```r\n[TASK_BEGIN]\nfunction(str) {\n cols <- str_split(str, \"[,,;、 ]\") %>% \n unlist() %>% \n str_trim() %>% \n .[nchar(.) > 0] %>% \n unique()\n \n valid <- map_lgl(cols, function(x) {\n tryCatch(is.matrix(col2rgb(x)), error = function(e) FALSE)\n })\n \n if(all(valid) && length(cols) > 0) {\n return(cols)\n } else {\n return(FALSE)\n }\n}\n[TASK_END]\n```", "inference_time": "2025-08-21 01-12-10"}, "editdistance_info": {"edit_distance": 27.9279, "calculate_time": "2025-08-21 01:12:10", "true_code_clean": "function(str){\n if(nchar(str_trim(str))<1) return(F) \n colo <- str_split(str, \"[,,;、 ]\") %>% unlist() %>% str_trim() %>% .[nchar(.) > 0] %>% .[!duplicated(.)]\n sig <- str_detect(colo, \"^\n if(any(!sig) | length(colo)>16){\n return(F)\n } else{\n return(colo)\n }\n }", "predict_code_clean": "function(str) {\n cols <- str_split(str, \"[,,;、 ]\") %>% \n unlist() %>% \n str_trim() %>% \n .[nchar(.) > 0] %>% \n unique()\n valid <- map_lgl(cols, function(x) {\n tryCatch(is.matrix(col2rgb(x)), error = function(e) FALSE)\n })\n if(all(valid) && length(cols) > 0) {\n return(cols)\n } else {\n return(FALSE)\n }\n}"}}
{"repo_name": "colors", "file_name": "/colors/app.R", "inference_info": {"prefix_code": "library(shiny)\nlibrary(purrr)\nlibrary(scales)\nlibrary(ggplot2)\nlibrary(stringr)\nlibrary(magrittr)\nlibrary(reactable)\nlibrary(gridExtra)\nlibrary(htmltools)\nlibrary(data.table)\nlibrary(shinyWidgets)\nlibrary(colourpicker)\nlibrary(shinydashboard)\n\nui <- dashboardPage(\n dashboardHeader(title = \"科研绘图配色推荐器\",titleWidth = \"100%\"),\n dashboardSidebar(disable = TRUE, collapsed = TRUE),\n dashboardBody(\n uiOutput(\"uipanel\"),\n tags$hr(),\n tags$div(align = \"center\", \n tags$p(\"\\ua9 2021-2024, Lcpmgh, All rights reserved.\", style=\"height:8px\"),\n tags$div(align = \"center\",\n actionLink(inputId = \"\", label = \"lcpmgh \", icon = icon(\"github\"), onclick =\"window.open('https://github.com/lcpmgh')\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n actionLink(inputId = \"\", label = \"lcpmgh@gmail.com\", icon = icon(\"envelope\"), onclick =\"window.location.href='mailto:lcpmgh@gmail.com'\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n actionLink(inputId = \"\", label = \"lcpmgh.com\", icon = icon(\"home\"), onclick =\"window.location.href='http://lcpmgh.com/'\")\n ),\n tags$div(align = \"center\",\n tags$a(\"冀ICP备2022003075号\", target=\"_blank\", href=\"https://beian.miit.gov.cn\", style=\"color:#06c; display:inline;\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n # tags$img(src=\"gaba.png\"),\n tags$a(\"川公网安备51010702002736\", target=\"_blank\", href=\"http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=51010702002736\", style=\"color:#06c; display:inline;\")\n )\n )\n )\n)\n\nserver <- function(input, output, session){\n # 数据\n colors <- readLines(\"@colors.txt\") %>% str_split(\",\") %>% lapply(., sort) %>% .[!duplicated(.)] #读取、排序、去重\n colors_nasc <- map_int(colors, length) %>% order() %>% colors[.] #按子颜色数量排序\n colors_table <- data.frame(col_id=1:length(colors_nasc), col_num=map_int(colors_nasc, length)) #颜色序号数量表\n setDT(colors_table)\n colors_sect <- colors_table[, c(min(.SD),max(.SD)), .SDcols=1,by=\"col_num\"] %>% \n .[, type:=rep(c(\"min\", \"max\"), nrow(.)/2)] %>% \n dcast(., col_num~type, value.var = \"V1\") %>% \n rbind(data.table(col_num=\"all\", \n max=max(colors_table$col_id), \n min=min(colors_table$col_id)), .) #提取每个数量区间的id范围 \n # 函数\n examp_plot <- ", "suffix_code": " \n iscolors <- function(str){\n # 函数,使用str_detect检查str是否是正确的颜色HEX码\n if(nchar(str_trim(str))<1) return(F) #如果str是空,直接返回F\n colo <- str_split(str, \"[,,;、 ]\") %>% unlist() %>% str_trim() %>% .[nchar(.) > 0] %>% .[!duplicated(.)]\n sig <- str_detect(colo, \"^#[A-Fa-f0-9]{6}$\")\n if(any(!sig) | length(colo)>16){\n # 如不是颜色,返回F\n return(F)\n } else{\n # 如是颜色,返回颜色HEX\n return(colo)\n }\n }\n \n ##### ui #####\n output$uipanel <- renderUI({\n tagList(\n tags$head(tags$link(rel = \"shortcut icon\", href = \"pmgh.ico\")),\n tags$style(HTML(\".custom-margin {margin-bottom: 20px;}\")),\n tags$div(class = \"custom-margin\",\n HTML(\"<h3 style='display: inline;'>方案选择</h3>\"),\n HTML(paste(\"<h5 style='display: inline;'>(数据库内现有\", length(colors_nasc), \"种配色方案)</h5>\"))),\n radioButtons(inputId = \"showtype\",\n label = NULL,\n choices = list(\"配色数据库方案id\" = \"id\", \"自定义配色方案\" = \"custom\"),\n inline = T,\n selected = c(\"按数量\" = \"id\")),\n # 按id\n conditionalPanel(condition = \"input.showtype == 'id'\",\n selectInput(inputId = \"num_select\", \n label = \"选色颜色数量\", \n choices = colors_sect$col_num,\n selected = \"all\",\n multiple = FALSE),\n div(style = \"display: flex; align-items: center; width:700px\",\n sliderTextInput(inputId = \"id_select\", \n label = \"选择方案id\", \n choices = seq(colors_sect[[1, \"min\"]], colors_sect[[1, \"max\"]]),\n selected = colors_sect[[1, \"min\"]],\n width = \"500px\", \n grid = T),\n div(style = \"width: 15%; text-align: center; padding-left: 20px;\", \n shiny::actionButton(inputId = \"pre\", \n label = \"上一个\",\n icon = icon(\"angle-left\"))),\n div(style = \"width: 15%; text-align: center;\", \n shiny::actionButton(inputId = \"nex\", \n label = \"下一个\",\n icon = icon(\"angle-right\"))))),\n # 自定义\n conditionalPanel(condition = \"input.showtype == 'custom'\",\n div(style = \"display: flex; align-items: center; width:700px\",\n colourInput(inputId = \"sele_col\", \n label = \"追加颜色\", \n allowTransparent = T,\n width = \"174px\",\n value = \"skyblue\"),\n div(style = \"width: 15%; text-align: center; padding-left: 20px; padding-top: 10px;\", \n shiny::actionButton(inputId = \"add_col\", \n label = \"加入\",\n icon = icon(\"plus\"))),\n div(style = \"width: 15%; text-align: center; padding-left: 10px; padding-top: 10px;\", \n shiny::actionButton(inputId = \"reset_col\", \n label = \"重置\",\n icon = icon(\"redo\")))),\n textInput(inputId = \"col_custom\",\n label = \"自定义颜色(HEX码,多个颜色以逗号、顿号、空格、分号间隔,颜色数量不可超过16,结果将去重):\",\n width = \"1000px\",\n value = \"#4DBBD5, #00A087, #E64B35\")),\n # 选择结果\n h3(\"所选配色方案\"),\n reactableOutput(outputId = \"colors_info\"),\n # h3(\"颜色透明度\"),\n sliderTextInput(inputId = \"color_alpha\",\n label = \"颜色透明度(颜色图层的alpha值)\", \n choices = seq(0,1, by=0.05),\n selected = 1,\n grid = T,\n width = \"500px\"),\n div(style = \"display: flex; align-items: center; width:1021px\",\n div(style = \"width: 50%; text-align: center;\", \n h3(\"绘图效果\"),\n plotOutput(outputId = \"plot_example\", width = \"510px\", height = \"400px\")),\n div(style = \"width: 50%; text-align: center;\", \n h3(\"方案样式\"),\n plotOutput(outputId = \"plot_color\", width = \"510px\", height = \"400px\"))),\n h3(\"配色数据库(点击表格显示绘图效果)\"),\n tags$head(tags$style(HTML(\".reactable-hover .rt-tr-group:hover {cursor: pointer;}\"))), \n reactableOutput(outputId = \"colors_db\")\n )\n })\n \n ##### server #####\n # 点击颜色数量,更新下面sliderTextInput中备选id的区间\n observeEvent(input$num_select, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n updateSliderTextInput(session, \"id_select\",\n choices = selected_seq,\n selected = selected_seq[1])\n })\n # 点击上一个或下一个后,更新slider对应的值\n observeEvent(input$pre, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n newValue <- max(selected_seq[1], as.numeric(input$id_select) - 1)\n updateSliderTextInput(session, \"id_select\", selected = newValue)\n })\n observeEvent(input$nex, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n newValue <- min(tail(selected_seq,1), as.numeric(input$id_select) + 1)\n updateSliderTextInput(session, \"id_select\", selected = newValue)\n })\n # 点击加入或重置颜色,更新textinput\n observeEvent(input$add_col, {\n newValue <- str_split(input$col_custom, \"[,,;、 ]\") %>%\n unlist() %>%\n str_trim() %>%\n .[nchar(.) > 0] %>%\n c(., input$sele_col) %>%\n .[!duplicated(.)] %>%\n paste0(collapse = \",\")\n updateTextInput(session, \"col_custom\", value = newValue)\n })\n observeEvent(input$reset_col, {\n newValue <- \"#4DBBD5, #00A087, #E64B35\"\n updateTextInput(session, \"col_custom\", value = newValue)\n })\n \n # 动态数据\n rv <- reactiveValues() #创建一个反应值对象rv,用于存储反应性的数据\n rv$value <- 1 #初始值为1\n observeEvent(c(input$id_select, input$showtype), {\n # 监听多个位置,如果有变动,则运行下面的内容,生成显示颜色方案的id\n if(input$showtype == \"id\"){\n # 如果此时页面为按id,则按选择的id更新id\n id <- input$id_select %>% as.numeric()\n rv$value <- id\n }\n })\n \n # table.1:所选配色方案\n output$colors_info <- renderReactable({\n showtype <- input$showtype\n custtext <- input$col_custom\n if(showtype == \"id\"){\n # 如果非自定义,则按id显示方案信息\n id <- rv$value\n colo_inf <- data.frame(id=id, \n colors_n=length(colors_nasc[[id]]), \n colors_hex=paste0(colors_nasc[[id]], collapse = \", \"),\n colors_show=paste0(colors_nasc[[id]], collapse = \", \"))\n } else{\n # 如果自定义\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n # 如果输入的字符不是颜色,则显示ERROR\n colo_inf <- data.frame(id=0, \n colors_n=\"ERROR\", \n colors_hex=\"ERROR\",\n colors_show=\"FFFFFF\")\n } else{\n # 如果未输入或输入的字符是颜色,则显示自定义的颜色信息\n colo_inf <- data.frame(id=0, \n colors_n=length(colsig), \n colors_hex=paste0(colsig, collapse = \", \"),\n colors_show=paste0(colsig, collapse = \", \"))\n }\n }\n # 显示表格\n reactable(colo_inf,\n columns = list(\n id = colDef(name = \"方案id\", width = 60, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_n = colDef(name = \"所含颜色数\", width = 90, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_hex = colDef(name = \"颜色HEX码\", width = 650, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_show=colDef(name = \"颜色预览\", align = \"center\", cell = function(value) {\n color_list <- strsplit(value, \", \")[[1]]\n color_divs <- lapply(color_list, function(color) {\n tags$div(style = paste(\"width: 20px; height: 20px; background-color:\", color, \"; display: inline-block; margin-right: 5px; border: 1px solid black;\"))\n })\n do.call(tagList, color_divs)\n }, style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\"))\n ),\n sortable = F, \n resizable = F,\n showPageSizeOptions = F,\n highlight = F,\n striped = T,\n bordered = T,\n compact = F,\n width = \"1020px\",\n fullWidth = F)\n })\n \n # fig.1:绘图效果\n output$plot_example <- renderPlot({\n showtype <- input$showtype\n custtext <- input$col_custom\n coloralp <- input$color_alpha %>% as.numeric()\n if(showtype == \"id\"){\n id <- rv$value\n pic <- examp_plot(id, colors_nasc, alp = coloralp)\n } else{\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n pic <- ggplot(data=NULL, aes(1,1))+\n geom_text(label=\"ERROR\", hjust=0.5, vjust=0.5, size=8)+\n theme_void()\n } else{\n pic <- examp_plot(0, NULL, colsig, alp = coloralp)\n }\n }\n return(pic)\n }) \n \n # fig.2:方案样式\n output$plot_color <- renderPlot({\n showtype <- input$showtype\n custtext <- input$col_custom\n if(showtype == \"id\"){\n id <- rv$value\n pic <- show_col(colors_nasc[[id]])\n } else{\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n pic <- ggplot(data=NULL, aes(1,1))+\n geom_text(label=\"ERROR\", hjust=0.5, vjust=0.5, size=8)+\n theme_void()\n } else{\n pic <- show_col(colsig)\n }\n }\n return(pic)\n }) \n \n # table.2:配色数据库详情\n output$colors_db <- renderReactable({\n showtype <- input$showtype\n colnum <- colors_sect[col_num == input$num_select,] #根据所选颜色数量更新数据库表格\n id_min <- colnum$min\n id_max <- colnum$max\n colo_db <- data.table(id=colors_table$col_id,\n colors_n=colors_table$col_num,\n colors_hex=map_chr(colors_nasc, ~paste0(.x, collapse = \", \")),\n colors_show=map_chr(colors_nasc, ~paste0(.x, collapse = \", \"))) %>% \n .[id>=id_min&id<=id_max,]\n class_name <- if (input$showtype == \"id\") \"reactable-hover\" else \"\" #当用户选择id时,才有hover属性\n reactable(colo_db,\n columns = list(\n id = colDef(name = \"方案id\", width = 60, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_n = colDef(name = \"所含颜色数\", width = 90, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_hex = colDef(name = \"颜色HEX码\", width = 650, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_show=colDef(name = \"颜色预览\", align = \"center\", cell = function(value) {\n color_list <- strsplit(value, \", \")[[1]]\n color_divs <- lapply(color_list, function(color) {\n tags$div(style = paste(\"width: 20px; height: 20px; background-color:\", color, \"; display: inline-block; margin-right: 5px; border: 1px solid black;\"))\n })\n do.call(tagList, color_divs)\n }, style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\"))\n ),\n class = class_name,\n language = reactableLang(searchPlaceholder = \"查找...\"),\n sortable = F,\n resizable = F,\n showPageSizeOptions = T,\n searchable = T,\n highlight = T,\n striped = T,\n bordered = T,\n compact = F,\n width = \"1020px\",\n fullWidth = F,\n theme = reactableTheme(searchInputStyle = list(\"margin-top\" = \"7px\", \"margin-right\" = \"7px\")),\n onClick = if(showtype != \"custom\"){\n JS(\"function(rowInfo, colInfo) {\n if (window.Shiny) {\n Shiny.setInputValue('selected_row', rowInfo.index + 1);\n window.scrollTo({\n top: 0, \n left: 0, \n behavior:'smooth'}); // 平滑滚动到页面顶部\n }\n }\")}\n )\n })\n \n # 点击表格更新绘图效果\n observeEvent(input$selected_row, {\n selected_row <- input$selected_row\n if (!is.null(selected_row)) {\n colnum <- colors_sect[col_num == input$num_select,] #所选颜色数量对应的id区间\n selected_id <- seq(colnum$min, colnum$max)[selected_row] #提取id区间,点击的行号,对应的准确id\n updateSliderTextInput(session, \"id_select\", selected = selected_id) #更新selected\n }\n })\n \n}\n\n# 输出app\nshinyApp(ui, server)", "middle_code": "function(id, colors_nasc=NULL, custom=c(\"\n if(id == 0){\n tcolor <- custom\n ncolor <- length(tcolor)\n } else{\n tcolor <- colors_nasc[[id]]\n ncolor <- length(tcolor)\n }\n dat_bar <- data.frame(a=sample(letters, ncolor, replace = F), \n b=runif(ncolor, 7, 10))\n p_bar <- ggplot(dat_bar, aes(a, b, fill=a))+\n geom_bar(color=\"black\", stat = \"identity\", alpha=alp)+\n scale_y_continuous(expand = c(0,0,0,0.1))+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Bar Chart with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_box <- data.frame(a=sample(letters, ncolor, replace = F),\n b=runif(ncolor*20, 7, 10))\n p_box <- ggplot(dat_box, aes(a, b, fill=a))+\n stat_boxplot(geom = \"errorbar\", linewidth=0.8, width = 0.3)+\n geom_boxplot(alpha=alp)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Boxplot with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_point <- data.frame(a=rep(runif(30), ncolor),\n b=rep(runif(30), ncolor),\n t=rep(sample(letters, ncolor, replace = F), 30))\n p_point <- ggplot(dat_point, aes(a, b, color=t, fill=t))+\n geom_point(shape=21, size=5, alpha=alp)+\n scale_color_manual(values = tcolor)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"scatterplot without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_line <- data.frame(a=rep(1:20, ncolor),\n b=rep(1:ncolor, each=20)+rnorm(20*ncolor, 0, 0.3), \n t=rep(sample(letters, ncolor), each=20))\n p_line <- ggplot(dat_line, aes(a, b, color=t, group=t))+\n geom_line(linewidth=1, alpha=alp)+\n scale_color_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Line chart without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n p <- grid.arrange(p_bar, p_box, p_point, p_line, padding=0, nrow = 2, ncol = 2)\n return(p)\n }", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "r", "sub_task_type": null}, "context_code": [], "task_instance_info": {"created_time": "2025-08-21 01:11:52", "created_task_model": "DeepSeek-R1", "function_skeleton": "function(id, colors_nasc=NULL, custom=c(\"#F5A889\", \"#ACD6EC\"), alp=0.1) {}", "code_description": "This code segment functions to: Generate multiple visualization examples using specified color schemes, dynamically creating diverse chart types to demonstrate color application across different graphical representations while incorporating adjustable transparency settings."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```r\n[TASK_BEGIN]\nexamp_plot <- function(id, colors_nasc=NULL, custom=c(\"#F5A889\", \"#ACD6EC\"), alp=0.1) {\n if(!is.null(colors_nasc)) {\n col_use <- colors_nasc[[id]]\n } else {\n col_use <- custom\n }\n \n col_alpha <- adjustcolor(col_use, alpha.f = alp)\n \n # 创建示例条形图\n p1 <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(cyl))) +\n geom_bar() +\n scale_fill_manual(values = col_alpha) +\n labs(title = \"柱状图示例\", x = \"气缸数\", y = \"计数\") +\n theme_minimal() +\n theme(legend.position = \"none\")\n \n # 创建示例折线图\n p2 <- ggplot(economics, aes(x = date, y = unemploy)) +\n geom_line(color = col_alpha[1], linewidth = 1) +\n labs(title = \"折线图示例\", x = \"日期\", y = \"失业人数\") +\n theme_minimal()\n \n # 创建示例箱线图\n p3 <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +\n geom_boxplot() +\n scale_fill_manual(values = col_alpha) +\n labs(title = \"箱线图示例\", x = \"鸢尾花种类\", y = \"萼片长度\") +\n theme_minimal() +\n theme(legend.position = \"none\")\n \n # 组合图形\n grid.arrange(p1, p2, p3, ncol = 2)\n}\n[TASK_END]\n```", "inference_time": "2025-08-21 01-12-17"}, "editdistance_info": {"edit_distance": 14.2857, "calculate_time": "2025-08-21 01:12:17", "true_code_clean": "function(id, colors_nasc=NULL, custom=c(\"\n if(id == 0){\n tcolor <- custom\n ncolor <- length(tcolor)\n } else{\n tcolor <- colors_nasc[[id]]\n ncolor <- length(tcolor)\n }\n dat_bar <- data.frame(a=sample(letters, ncolor, replace = F), \n b=runif(ncolor, 7, 10))\n p_bar <- ggplot(dat_bar, aes(a, b, fill=a))+\n geom_bar(color=\"black\", stat = \"identity\", alpha=alp)+\n scale_y_continuous(expand = c(0,0,0,0.1))+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Bar Chart with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_box <- data.frame(a=sample(letters, ncolor, replace = F),\n b=runif(ncolor*20, 7, 10))\n p_box <- ggplot(dat_box, aes(a, b, fill=a))+\n stat_boxplot(geom = \"errorbar\", linewidth=0.8, width = 0.3)+\n geom_boxplot(alpha=alp)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Boxplot with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_point <- data.frame(a=rep(runif(30), ncolor),\n b=rep(runif(30), ncolor),\n t=rep(sample(letters, ncolor, replace = F), 30))\n p_point <- ggplot(dat_point, aes(a, b, color=t, fill=t))+\n geom_point(shape=21, size=5, alpha=alp)+\n scale_color_manual(values = tcolor)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"scatterplot without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_line <- data.frame(a=rep(1:20, ncolor),\n b=rep(1:ncolor, each=20)+rnorm(20*ncolor, 0, 0.3), \n t=rep(sample(letters, ncolor), each=20))\n p_line <- ggplot(dat_line, aes(a, b, color=t, group=t))+\n geom_line(linewidth=1, alpha=alp)+\n scale_color_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Line chart without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n p <- grid.arrange(p_bar, p_box, p_point, p_line, padding=0, nrow = 2, ncol = 2)\n return(p)\n }", "predict_code_clean": "examp_plot <- function(id, colors_nasc=NULL, custom=c(\"\n if(!is.null(colors_nasc)) {\n col_use <- colors_nasc[[id]]\n } else {\n col_use <- custom\n }\n col_alpha <- adjustcolor(col_use, alpha.f = alp)\n p1 <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(cyl))) +\n geom_bar() +\n scale_fill_manual(values = col_alpha) +\n labs(title = \"柱状图示例\", x = \"气缸数\", y = \"计数\") +\n theme_minimal() +\n theme(legend.position = \"none\")\n p2 <- ggplot(economics, aes(x = date, y = unemploy)) +\n geom_line(color = col_alpha[1], linewidth = 1) +\n labs(title = \"折线图示例\", x = \"日期\", y = \"失业人数\") +\n theme_minimal()\n p3 <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +\n geom_boxplot() +\n scale_fill_manual(values = col_alpha) +\n labs(title = \"箱线图示例\", x = \"鸢尾花种类\", y = \"萼片长度\") +\n theme_minimal() +\n theme(legend.position = \"none\")\n grid.arrange(p1, p2, p3, ncol = 2)\n}"}}
{"repo_name": "colors", "file_name": "/colors/app.R", "inference_info": {"prefix_code": "library(shiny)\nlibrary(purrr)\nlibrary(scales)\nlibrary(ggplot2)\nlibrary(stringr)\nlibrary(magrittr)\nlibrary(reactable)\nlibrary(gridExtra)\nlibrary(htmltools)\nlibrary(data.table)\nlibrary(shinyWidgets)\nlibrary(colourpicker)\nlibrary(shinydashboard)\n\nui <- dashboardPage(\n dashboardHeader(title = \"科研绘图配色推荐器\",titleWidth = \"100%\"),\n dashboardSidebar(disable = TRUE, collapsed = TRUE),\n dashboardBody(\n uiOutput(\"uipanel\"),\n tags$hr(),\n tags$div(align = \"center\", \n tags$p(\"\\ua9 2021-2024, Lcpmgh, All rights reserved.\", style=\"height:8px\"),\n tags$div(align = \"center\",\n actionLink(inputId = \"\", label = \"lcpmgh \", icon = icon(\"github\"), onclick =\"window.open('https://github.com/lcpmgh')\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n actionLink(inputId = \"\", label = \"lcpmgh@gmail.com\", icon = icon(\"envelope\"), onclick =\"window.location.href='mailto:lcpmgh@gmail.com'\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n actionLink(inputId = \"\", label = \"lcpmgh.com\", icon = icon(\"home\"), onclick =\"window.location.href='http://lcpmgh.com/'\")\n ),\n tags$div(align = \"center\",\n tags$a(\"冀ICP备2022003075号\", target=\"_blank\", href=\"https://beian.miit.gov.cn\", style=\"color:#06c; display:inline;\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n # tags$img(src=\"gaba.png\"),\n tags$a(\"川公网安备51010702002736\", target=\"_blank\", href=\"http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=51010702002736\", style=\"color:#06c; display:inline;\")\n )\n )\n )\n)\n\nserver <- function(input, output, session){\n # 数据\n colors <- readLines(\"@colors.txt\") %>% str_split(\",\") %>% lapply(., sort) %>% .[!duplicated(.)] #读取、排序、去重\n colors_nasc <- map_int(colors, length) %>% order() %>% colors[.] #按子颜色数量排序\n colors_table <- data.frame(col_id=1:length(colors_nasc), col_num=map_int(colors_nasc, length)) #颜色序号数量表\n setDT(colors_table)\n colors_sect <- colors_table[, c(min(.SD),max(.SD)), .SDcols=1,by=\"col_num\"] %>% \n .[, type:=rep(c(\"min\", \"max\"), nrow(.)/2)] %>% \n dcast(., col_num~type, value.var = \"V1\") %>% \n rbind(data.table(col_num=\"all\", \n max=max(colors_table$col_id), \n min=min(colors_table$col_id)), .) #提取每个数量区间的id范围 \n # 函数\n examp_plot <- ", "suffix_code": " \n iscolors <- function(str){\n # 函数,使用str_detect检查str是否是正确的颜色HEX码\n if(nchar(str_trim(str))<1) return(F) #如果str是空,直接返回F\n colo <- str_split(str, \"[,,;、 ]\") %>% unlist() %>% str_trim() %>% .[nchar(.) > 0] %>% .[!duplicated(.)]\n sig <- str_detect(colo, \"^#[A-Fa-f0-9]{6}$\")\n if(any(!sig) | length(colo)>16){\n # 如不是颜色,返回F\n return(F)\n } else{\n # 如是颜色,返回颜色HEX\n return(colo)\n }\n }\n \n ##### ui #####\n output$uipanel <- renderUI({\n tagList(\n tags$head(tags$link(rel = \"shortcut icon\", href = \"pmgh.ico\")),\n tags$style(HTML(\".custom-margin {margin-bottom: 20px;}\")),\n tags$div(class = \"custom-margin\",\n HTML(\"<h3 style='display: inline;'>方案选择</h3>\"),\n HTML(paste(\"<h5 style='display: inline;'>(数据库内现有\", length(colors_nasc), \"种配色方案)</h5>\"))),\n radioButtons(inputId = \"showtype\",\n label = NULL,\n choices = list(\"配色数据库方案id\" = \"id\", \"自定义配色方案\" = \"custom\"),\n inline = T,\n selected = c(\"按数量\" = \"id\")),\n # 按id\n conditionalPanel(condition = \"input.showtype == 'id'\",\n selectInput(inputId = \"num_select\", \n label = \"选色颜色数量\", \n choices = colors_sect$col_num,\n selected = \"all\",\n multiple = FALSE),\n div(style = \"display: flex; align-items: center; width:700px\",\n sliderTextInput(inputId = \"id_select\", \n label = \"选择方案id\", \n choices = seq(colors_sect[[1, \"min\"]], colors_sect[[1, \"max\"]]),\n selected = colors_sect[[1, \"min\"]],\n width = \"500px\", \n grid = T),\n div(style = \"width: 15%; text-align: center; padding-left: 20px;\", \n shiny::actionButton(inputId = \"pre\", \n label = \"上一个\",\n icon = icon(\"angle-left\"))),\n div(style = \"width: 15%; text-align: center;\", \n shiny::actionButton(inputId = \"nex\", \n label = \"下一个\",\n icon = icon(\"angle-right\"))))),\n # 自定义\n conditionalPanel(condition = \"input.showtype == 'custom'\",\n div(style = \"display: flex; align-items: center; width:700px\",\n colourInput(inputId = \"sele_col\", \n label = \"追加颜色\", \n allowTransparent = T,\n width = \"174px\",\n value = \"skyblue\"),\n div(style = \"width: 15%; text-align: center; padding-left: 20px; padding-top: 10px;\", \n shiny::actionButton(inputId = \"add_col\", \n label = \"加入\",\n icon = icon(\"plus\"))),\n div(style = \"width: 15%; text-align: center; padding-left: 10px; padding-top: 10px;\", \n shiny::actionButton(inputId = \"reset_col\", \n label = \"重置\",\n icon = icon(\"redo\")))),\n textInput(inputId = \"col_custom\",\n label = \"自定义颜色(HEX码,多个颜色以逗号、顿号、空格、分号间隔,颜色数量不可超过16,结果将去重):\",\n width = \"1000px\",\n value = \"#4DBBD5, #00A087, #E64B35\")),\n # 选择结果\n h3(\"所选配色方案\"),\n reactableOutput(outputId = \"colors_info\"),\n # h3(\"颜色透明度\"),\n sliderTextInput(inputId = \"color_alpha\",\n label = \"颜色透明度(颜色图层的alpha值)\", \n choices = seq(0,1, by=0.05),\n selected = 1,\n grid = T,\n width = \"500px\"),\n div(style = \"display: flex; align-items: center; width:1021px\",\n div(style = \"width: 50%; text-align: center;\", \n h3(\"绘图效果\"),\n plotOutput(outputId = \"plot_example\", width = \"510px\", height = \"400px\")),\n div(style = \"width: 50%; text-align: center;\", \n h3(\"方案样式\"),\n plotOutput(outputId = \"plot_color\", width = \"510px\", height = \"400px\"))),\n h3(\"配色数据库(点击表格显示绘图效果)\"),\n tags$head(tags$style(HTML(\".reactable-hover .rt-tr-group:hover {cursor: pointer;}\"))), \n reactableOutput(outputId = \"colors_db\")\n )\n })\n \n ##### server #####\n # 点击颜色数量,更新下面sliderTextInput中备选id的区间\n observeEvent(input$num_select, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n updateSliderTextInput(session, \"id_select\",\n choices = selected_seq,\n selected = selected_seq[1])\n })\n # 点击上一个或下一个后,更新slider对应的值\n observeEvent(input$pre, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n newValue <- max(selected_seq[1], as.numeric(input$id_select) - 1)\n updateSliderTextInput(session, \"id_select\", selected = newValue)\n })\n observeEvent(input$nex, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n newValue <- min(tail(selected_seq,1), as.numeric(input$id_select) + 1)\n updateSliderTextInput(session, \"id_select\", selected = newValue)\n })\n # 点击加入或重置颜色,更新textinput\n observeEvent(input$add_col, {\n newValue <- str_split(input$col_custom, \"[,,;、 ]\") %>%\n unlist() %>%\n str_trim() %>%\n .[nchar(.) > 0] %>%\n c(., input$sele_col) %>%\n .[!duplicated(.)] %>%\n paste0(collapse = \",\")\n updateTextInput(session, \"col_custom\", value = newValue)\n })\n observeEvent(input$reset_col, {\n newValue <- \"#4DBBD5, #00A087, #E64B35\"\n updateTextInput(session, \"col_custom\", value = newValue)\n })\n \n # 动态数据\n rv <- reactiveValues() #创建一个反应值对象rv,用于存储反应性的数据\n rv$value <- 1 #初始值为1\n observeEvent(c(input$id_select, input$showtype), {\n # 监听多个位置,如果有变动,则运行下面的内容,生成显示颜色方案的id\n if(input$showtype == \"id\"){\n # 如果此时页面为按id,则按选择的id更新id\n id <- input$id_select %>% as.numeric()\n rv$value <- id\n }\n })\n \n # table.1:所选配色方案\n output$colors_info <- renderReactable({\n showtype <- input$showtype\n custtext <- input$col_custom\n if(showtype == \"id\"){\n # 如果非自定义,则按id显示方案信息\n id <- rv$value\n colo_inf <- data.frame(id=id, \n colors_n=length(colors_nasc[[id]]), \n colors_hex=paste0(colors_nasc[[id]], collapse = \", \"),\n colors_show=paste0(colors_nasc[[id]], collapse = \", \"))\n } else{\n # 如果自定义\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n # 如果输入的字符不是颜色,则显示ERROR\n colo_inf <- data.frame(id=0, \n colors_n=\"ERROR\", \n colors_hex=\"ERROR\",\n colors_show=\"FFFFFF\")\n } else{\n # 如果未输入或输入的字符是颜色,则显示自定义的颜色信息\n colo_inf <- data.frame(id=0, \n colors_n=length(colsig), \n colors_hex=paste0(colsig, collapse = \", \"),\n colors_show=paste0(colsig, collapse = \", \"))\n }\n }\n # 显示表格\n reactable(colo_inf,\n columns = list(\n id = colDef(name = \"方案id\", width = 60, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_n = colDef(name = \"所含颜色数\", width = 90, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_hex = colDef(name = \"颜色HEX码\", width = 650, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_show=colDef(name = \"颜色预览\", align = \"center\", cell = function(value) {\n color_list <- strsplit(value, \", \")[[1]]\n color_divs <- lapply(color_list, function(color) {\n tags$div(style = paste(\"width: 20px; height: 20px; background-color:\", color, \"; display: inline-block; margin-right: 5px; border: 1px solid black;\"))\n })\n do.call(tagList, color_divs)\n }, style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\"))\n ),\n sortable = F, \n resizable = F,\n showPageSizeOptions = F,\n highlight = F,\n striped = T,\n bordered = T,\n compact = F,\n width = \"1020px\",\n fullWidth = F)\n })\n \n # fig.1:绘图效果\n output$plot_example <- renderPlot({\n showtype <- input$showtype\n custtext <- input$col_custom\n coloralp <- input$color_alpha %>% as.numeric()\n if(showtype == \"id\"){\n id <- rv$value\n pic <- examp_plot(id, colors_nasc, alp = coloralp)\n } else{\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n pic <- ggplot(data=NULL, aes(1,1))+\n geom_text(label=\"ERROR\", hjust=0.5, vjust=0.5, size=8)+\n theme_void()\n } else{\n pic <- examp_plot(0, NULL, colsig, alp = coloralp)\n }\n }\n return(pic)\n }) \n \n # fig.2:方案样式\n output$plot_color <- renderPlot({\n showtype <- input$showtype\n custtext <- input$col_custom\n if(showtype == \"id\"){\n id <- rv$value\n pic <- show_col(colors_nasc[[id]])\n } else{\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n pic <- ggplot(data=NULL, aes(1,1))+\n geom_text(label=\"ERROR\", hjust=0.5, vjust=0.5, size=8)+\n theme_void()\n } else{\n pic <- show_col(colsig)\n }\n }\n return(pic)\n }) \n \n # table.2:配色数据库详情\n output$colors_db <- renderReactable({\n showtype <- input$showtype\n colnum <- colors_sect[col_num == input$num_select,] #根据所选颜色数量更新数据库表格\n id_min <- colnum$min\n id_max <- colnum$max\n colo_db <- data.table(id=colors_table$col_id,\n colors_n=colors_table$col_num,\n colors_hex=map_chr(colors_nasc, ~paste0(.x, collapse = \", \")),\n colors_show=map_chr(colors_nasc, ~paste0(.x, collapse = \", \"))) %>% \n .[id>=id_min&id<=id_max,]\n class_name <- if (input$showtype == \"id\") \"reactable-hover\" else \"\" #当用户选择id时,才有hover属性\n reactable(colo_db,\n columns = list(\n id = colDef(name = \"方案id\", width = 60, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_n = colDef(name = \"所含颜色数\", width = 90, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_hex = colDef(name = \"颜色HEX码\", width = 650, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_show=colDef(name = \"颜色预览\", align = \"center\", cell = function(value) {\n color_list <- strsplit(value, \", \")[[1]]\n color_divs <- lapply(color_list, function(color) {\n tags$div(style = paste(\"width: 20px; height: 20px; background-color:\", color, \"; display: inline-block; margin-right: 5px; border: 1px solid black;\"))\n })\n do.call(tagList, color_divs)\n }, style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\"))\n ),\n class = class_name,\n language = reactableLang(searchPlaceholder = \"查找...\"),\n sortable = F,\n resizable = F,\n showPageSizeOptions = T,\n searchable = T,\n highlight = T,\n striped = T,\n bordered = T,\n compact = F,\n width = \"1020px\",\n fullWidth = F,\n theme = reactableTheme(searchInputStyle = list(\"margin-top\" = \"7px\", \"margin-right\" = \"7px\")),\n onClick = if(showtype != \"custom\"){\n JS(\"function(rowInfo, colInfo) {\n if (window.Shiny) {\n Shiny.setInputValue('selected_row', rowInfo.index + 1);\n window.scrollTo({\n top: 0, \n left: 0, \n behavior:'smooth'}); // 平滑滚动到页面顶部\n }\n }\")}\n )\n })\n \n # 点击表格更新绘图效果\n observeEvent(input$selected_row, {\n selected_row <- input$selected_row\n if (!is.null(selected_row)) {\n colnum <- colors_sect[col_num == input$num_select,] #所选颜色数量对应的id区间\n selected_id <- seq(colnum$min, colnum$max)[selected_row] #提取id区间,点击的行号,对应的准确id\n updateSliderTextInput(session, \"id_select\", selected = selected_id) #更新selected\n }\n })\n \n}\n\n# 输出app\nshinyApp(ui, server)", "middle_code": "function(id, colors_nasc=NULL, custom=c(\"\n if(id == 0){\n tcolor <- custom\n ncolor <- length(tcolor)\n } else{\n tcolor <- colors_nasc[[id]]\n ncolor <- length(tcolor)\n }\n dat_bar <- data.frame(a=sample(letters, ncolor, replace = F), \n b=runif(ncolor, 7, 10))\n p_bar <- ggplot(dat_bar, aes(a, b, fill=a))+\n geom_bar(color=\"black\", stat = \"identity\", alpha=alp)+\n scale_y_continuous(expand = c(0,0,0,0.1))+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Bar Chart with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_box <- data.frame(a=sample(letters, ncolor, replace = F),\n b=runif(ncolor*20, 7, 10))\n p_box <- ggplot(dat_box, aes(a, b, fill=a))+\n stat_boxplot(geom = \"errorbar\", linewidth=0.8, width = 0.3)+\n geom_boxplot(alpha=alp)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Boxplot with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_point <- data.frame(a=rep(runif(30), ncolor),\n b=rep(runif(30), ncolor),\n t=rep(sample(letters, ncolor, replace = F), 30))\n p_point <- ggplot(dat_point, aes(a, b, color=t, fill=t))+\n geom_point(shape=21, size=5, alpha=alp)+\n scale_color_manual(values = tcolor)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"scatterplot without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_line <- data.frame(a=rep(1:20, ncolor),\n b=rep(1:ncolor, each=20)+rnorm(20*ncolor, 0, 0.3), \n t=rep(sample(letters, ncolor), each=20))\n p_line <- ggplot(dat_line, aes(a, b, color=t, group=t))+\n geom_line(linewidth=1, alpha=alp)+\n scale_color_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Line chart without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n p <- grid.arrange(p_bar, p_box, p_point, p_line, padding=0, nrow = 2, ncol = 2)\n return(p)\n }", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "r", "sub_task_type": null}, "context_code": [], "task_instance_info": {"created_time": "2025-08-21 01:11:52", "created_task_model": "DeepSeek-R1", "function_skeleton": "function(id, colors_nasc=NULL, custom=c(\"#F5A889\", \"#ACD6EC\"), alp=0.1) {}", "code_description": "This code segment functions to: Generate multiple example visualizations using specified color schemes, applying them across different chart types while incorporating adjustable transparency settings and demonstrating color consistency across graphical elements."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```r\n[TASK_BEGIN]\nfunction(id, colors_nasc=NULL, custom=c(\"#F5A889\", \"#ACD6EC\"), alp=0.1) {\n cols <- if(!is.null(colors_nasc)) colors_nasc[[id]] else custom\n cols <- adjustcolor(cols, alpha.f = alp)\n \n # 生成示例数据\n set.seed(123)\n df_bar <- data.frame(\n group = factor(rep(c(\"A\",\"B\",\"C\",\"D\",\"E\"), each=3)),\n value = c(rnorm(15, mean=10))\n )\n df_scatter <- data.frame(\n x = rnorm(50), y = rnorm(50), \n group = factor(rep(1:5, each=10))\n )\n df_line <- data.frame(\n x = 1:10,\n y1 = cumsum(rnorm(10)),\n y2 = cumsum(rnorm(10)),\n y3 = cumsum(rnorm(10))\n )\n df_box <- data.frame(\n group = factor(rep(c(\"X\",\"Y\",\"Z\"), each=50)),\n value = c(rnorm(50, 5), rnorm(50, 6), rnorm(50, 7))\n )\n \n # 定义绘图主题\n mytheme <- theme_bw() + \n theme(panel.grid = element_blank(),\n legend.position = \"bottom\")\n \n # 各类图形绘制\n p1 <- ggplot(df_bar, aes(x=group, y=value, fill=group)) + \n geom_col() + \n scale_fill_manual(values = cols) +\n labs(title = \"Bar Plot\") + mytheme\n \n p2 <- ggplot(df_scatter, aes(x=x, y=y, color=group)) + \n geom_point(size=3) + \n scale_color_manual(values = cols) +\n labs(title = \"Scatter Plot\") + mytheme\n \n p3 <- ggplot(df_line, aes(x=x)) + \n geom_line(aes(y=y1), color=cols[1], linewidth=1.2) + \n geom_line(aes(y=y2), color=cols[2], linewidth=1.2) + \n geom_line(aes(y=y3), color=cols[3], linewidth=1.2) + \n labs(title = \"Line Plot\") + mytheme\n \n p4 <- ggplot(df_box, aes(x=group, y=value, fill=group)) + \n geom_boxplot() + \n scale_fill_manual(values = cols) + \n labs(title = \"Box Plot\") + mytheme\n \n # 合并图形\n grid.arrange(p1, p2, p3, p4, ncol=2, \n top = textGrob(\"Example Plots\", gp=gpar(fontsize=18, font=2)))\n}\n[TASK_END]\n```", "inference_time": "2025-08-21 01-12-21"}, "editdistance_info": {"edit_distance": 12.1805, "calculate_time": "2025-08-21 01:12:21", "true_code_clean": "function(id, colors_nasc=NULL, custom=c(\"\n if(id == 0){\n tcolor <- custom\n ncolor <- length(tcolor)\n } else{\n tcolor <- colors_nasc[[id]]\n ncolor <- length(tcolor)\n }\n dat_bar <- data.frame(a=sample(letters, ncolor, replace = F), \n b=runif(ncolor, 7, 10))\n p_bar <- ggplot(dat_bar, aes(a, b, fill=a))+\n geom_bar(color=\"black\", stat = \"identity\", alpha=alp)+\n scale_y_continuous(expand = c(0,0,0,0.1))+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Bar Chart with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_box <- data.frame(a=sample(letters, ncolor, replace = F),\n b=runif(ncolor*20, 7, 10))\n p_box <- ggplot(dat_box, aes(a, b, fill=a))+\n stat_boxplot(geom = \"errorbar\", linewidth=0.8, width = 0.3)+\n geom_boxplot(alpha=alp)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Boxplot with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_point <- data.frame(a=rep(runif(30), ncolor),\n b=rep(runif(30), ncolor),\n t=rep(sample(letters, ncolor, replace = F), 30))\n p_point <- ggplot(dat_point, aes(a, b, color=t, fill=t))+\n geom_point(shape=21, size=5, alpha=alp)+\n scale_color_manual(values = tcolor)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"scatterplot without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_line <- data.frame(a=rep(1:20, ncolor),\n b=rep(1:ncolor, each=20)+rnorm(20*ncolor, 0, 0.3), \n t=rep(sample(letters, ncolor), each=20))\n p_line <- ggplot(dat_line, aes(a, b, color=t, group=t))+\n geom_line(linewidth=1, alpha=alp)+\n scale_color_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Line chart without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n p <- grid.arrange(p_bar, p_box, p_point, p_line, padding=0, nrow = 2, ncol = 2)\n return(p)\n }", "predict_code_clean": "function(id, colors_nasc=NULL, custom=c(\"\n cols <- if(!is.null(colors_nasc)) colors_nasc[[id]] else custom\n cols <- adjustcolor(cols, alpha.f = alp)\n set.seed(123)\n df_bar <- data.frame(\n group = factor(rep(c(\"A\",\"B\",\"C\",\"D\",\"E\"), each=3)),\n value = c(rnorm(15, mean=10))\n )\n df_scatter <- data.frame(\n x = rnorm(50), y = rnorm(50), \n group = factor(rep(1:5, each=10))\n )\n df_line <- data.frame(\n x = 1:10,\n y1 = cumsum(rnorm(10)),\n y2 = cumsum(rnorm(10)),\n y3 = cumsum(rnorm(10))\n )\n df_box <- data.frame(\n group = factor(rep(c(\"X\",\"Y\",\"Z\"), each=50)),\n value = c(rnorm(50, 5), rnorm(50, 6), rnorm(50, 7))\n )\n mytheme <- theme_bw() + \n theme(panel.grid = element_blank(),\n legend.position = \"bottom\")\n p1 <- ggplot(df_bar, aes(x=group, y=value, fill=group)) + \n geom_col() + \n scale_fill_manual(values = cols) +\n labs(title = \"Bar Plot\") + mytheme\n p2 <- ggplot(df_scatter, aes(x=x, y=y, color=group)) + \n geom_point(size=3) + \n scale_color_manual(values = cols) +\n labs(title = \"Scatter Plot\") + mytheme\n p3 <- ggplot(df_line, aes(x=x)) + \n geom_line(aes(y=y1), color=cols[1], linewidth=1.2) + \n geom_line(aes(y=y2), color=cols[2], linewidth=1.2) + \n geom_line(aes(y=y3), color=cols[3], linewidth=1.2) + \n labs(title = \"Line Plot\") + mytheme\n p4 <- ggplot(df_box, aes(x=group, y=value, fill=group)) + \n geom_boxplot() + \n scale_fill_manual(values = cols) + \n labs(title = \"Box Plot\") + mytheme\n grid.arrange(p1, p2, p3, p4, ncol=2, \n top = textGrob(\"Example Plots\", gp=gpar(fontsize=18, font=2)))\n}"}}
{"repo_name": "colors", "file_name": "/colors/app.R", "inference_info": {"prefix_code": "library(shiny)\nlibrary(purrr)\nlibrary(scales)\nlibrary(ggplot2)\nlibrary(stringr)\nlibrary(magrittr)\nlibrary(reactable)\nlibrary(gridExtra)\nlibrary(htmltools)\nlibrary(data.table)\nlibrary(shinyWidgets)\nlibrary(colourpicker)\nlibrary(shinydashboard)\n\nui <- dashboardPage(\n dashboardHeader(title = \"科研绘图配色推荐器\",titleWidth = \"100%\"),\n dashboardSidebar(disable = TRUE, collapsed = TRUE),\n dashboardBody(\n uiOutput(\"uipanel\"),\n tags$hr(),\n tags$div(align = \"center\", \n tags$p(\"\\ua9 2021-2024, Lcpmgh, All rights reserved.\", style=\"height:8px\"),\n tags$div(align = \"center\",\n actionLink(inputId = \"\", label = \"lcpmgh \", icon = icon(\"github\"), onclick =\"window.open('https://github.com/lcpmgh')\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n actionLink(inputId = \"\", label = \"lcpmgh@gmail.com\", icon = icon(\"envelope\"), onclick =\"window.location.href='mailto:lcpmgh@gmail.com'\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n actionLink(inputId = \"\", label = \"lcpmgh.com\", icon = icon(\"home\"), onclick =\"window.location.href='http://lcpmgh.com/'\")\n ),\n tags$div(align = \"center\",\n tags$a(\"冀ICP备2022003075号\", target=\"_blank\", href=\"https://beian.miit.gov.cn\", style=\"color:#06c; display:inline;\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n # tags$img(src=\"gaba.png\"),\n tags$a(\"川公网安备51010702002736\", target=\"_blank\", href=\"http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=51010702002736\", style=\"color:#06c; display:inline;\")\n )\n )\n )\n)\n\nserver <- function(input, output, session){\n # 数据\n colors <- readLines(\"@colors.txt\") %>% str_split(\",\") %>% lapply(., sort) %>% .[!duplicated(.)] #读取、排序、去重\n colors_nasc <- map_int(colors, length) %>% order() %>% colors[.] #按子颜色数量排序\n colors_table <- data.frame(col_id=1:length(colors_nasc), col_num=map_int(colors_nasc, length)) #颜色序号数量表\n setDT(colors_table)\n colors_sect <- colors_table[, c(min(.SD),max(.SD)), .SDcols=1,by=\"col_num\"] %>% \n .[, type:=rep(c(\"min\", \"max\"), nrow(.)/2)] %>% \n dcast(., col_num~type, value.var = \"V1\") %>% \n rbind(data.table(col_num=\"all\", \n max=max(colors_table$col_id), \n min=min(colors_table$col_id)), .) #提取每个数量区间的id范围 \n # 函数\n examp_plot <- ", "suffix_code": " \n iscolors <- function(str){\n # 函数,使用str_detect检查str是否是正确的颜色HEX码\n if(nchar(str_trim(str))<1) return(F) #如果str是空,直接返回F\n colo <- str_split(str, \"[,,;、 ]\") %>% unlist() %>% str_trim() %>% .[nchar(.) > 0] %>% .[!duplicated(.)]\n sig <- str_detect(colo, \"^#[A-Fa-f0-9]{6}$\")\n if(any(!sig) | length(colo)>16){\n # 如不是颜色,返回F\n return(F)\n } else{\n # 如是颜色,返回颜色HEX\n return(colo)\n }\n }\n \n ##### ui #####\n output$uipanel <- renderUI({\n tagList(\n tags$head(tags$link(rel = \"shortcut icon\", href = \"pmgh.ico\")),\n tags$style(HTML(\".custom-margin {margin-bottom: 20px;}\")),\n tags$div(class = \"custom-margin\",\n HTML(\"<h3 style='display: inline;'>方案选择</h3>\"),\n HTML(paste(\"<h5 style='display: inline;'>(数据库内现有\", length(colors_nasc), \"种配色方案)</h5>\"))),\n radioButtons(inputId = \"showtype\",\n label = NULL,\n choices = list(\"配色数据库方案id\" = \"id\", \"自定义配色方案\" = \"custom\"),\n inline = T,\n selected = c(\"按数量\" = \"id\")),\n # 按id\n conditionalPanel(condition = \"input.showtype == 'id'\",\n selectInput(inputId = \"num_select\", \n label = \"选色颜色数量\", \n choices = colors_sect$col_num,\n selected = \"all\",\n multiple = FALSE),\n div(style = \"display: flex; align-items: center; width:700px\",\n sliderTextInput(inputId = \"id_select\", \n label = \"选择方案id\", \n choices = seq(colors_sect[[1, \"min\"]], colors_sect[[1, \"max\"]]),\n selected = colors_sect[[1, \"min\"]],\n width = \"500px\", \n grid = T),\n div(style = \"width: 15%; text-align: center; padding-left: 20px;\", \n shiny::actionButton(inputId = \"pre\", \n label = \"上一个\",\n icon = icon(\"angle-left\"))),\n div(style = \"width: 15%; text-align: center;\", \n shiny::actionButton(inputId = \"nex\", \n label = \"下一个\",\n icon = icon(\"angle-right\"))))),\n # 自定义\n conditionalPanel(condition = \"input.showtype == 'custom'\",\n div(style = \"display: flex; align-items: center; width:700px\",\n colourInput(inputId = \"sele_col\", \n label = \"追加颜色\", \n allowTransparent = T,\n width = \"174px\",\n value = \"skyblue\"),\n div(style = \"width: 15%; text-align: center; padding-left: 20px; padding-top: 10px;\", \n shiny::actionButton(inputId = \"add_col\", \n label = \"加入\",\n icon = icon(\"plus\"))),\n div(style = \"width: 15%; text-align: center; padding-left: 10px; padding-top: 10px;\", \n shiny::actionButton(inputId = \"reset_col\", \n label = \"重置\",\n icon = icon(\"redo\")))),\n textInput(inputId = \"col_custom\",\n label = \"自定义颜色(HEX码,多个颜色以逗号、顿号、空格、分号间隔,颜色数量不可超过16,结果将去重):\",\n width = \"1000px\",\n value = \"#4DBBD5, #00A087, #E64B35\")),\n # 选择结果\n h3(\"所选配色方案\"),\n reactableOutput(outputId = \"colors_info\"),\n # h3(\"颜色透明度\"),\n sliderTextInput(inputId = \"color_alpha\",\n label = \"颜色透明度(颜色图层的alpha值)\", \n choices = seq(0,1, by=0.05),\n selected = 1,\n grid = T,\n width = \"500px\"),\n div(style = \"display: flex; align-items: center; width:1021px\",\n div(style = \"width: 50%; text-align: center;\", \n h3(\"绘图效果\"),\n plotOutput(outputId = \"plot_example\", width = \"510px\", height = \"400px\")),\n div(style = \"width: 50%; text-align: center;\", \n h3(\"方案样式\"),\n plotOutput(outputId = \"plot_color\", width = \"510px\", height = \"400px\"))),\n h3(\"配色数据库(点击表格显示绘图效果)\"),\n tags$head(tags$style(HTML(\".reactable-hover .rt-tr-group:hover {cursor: pointer;}\"))), \n reactableOutput(outputId = \"colors_db\")\n )\n })\n \n ##### server #####\n # 点击颜色数量,更新下面sliderTextInput中备选id的区间\n observeEvent(input$num_select, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n updateSliderTextInput(session, \"id_select\",\n choices = selected_seq,\n selected = selected_seq[1])\n })\n # 点击上一个或下一个后,更新slider对应的值\n observeEvent(input$pre, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n newValue <- max(selected_seq[1], as.numeric(input$id_select) - 1)\n updateSliderTextInput(session, \"id_select\", selected = newValue)\n })\n observeEvent(input$nex, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n newValue <- min(tail(selected_seq,1), as.numeric(input$id_select) + 1)\n updateSliderTextInput(session, \"id_select\", selected = newValue)\n })\n # 点击加入或重置颜色,更新textinput\n observeEvent(input$add_col, {\n newValue <- str_split(input$col_custom, \"[,,;、 ]\") %>%\n unlist() %>%\n str_trim() %>%\n .[nchar(.) > 0] %>%\n c(., input$sele_col) %>%\n .[!duplicated(.)] %>%\n paste0(collapse = \",\")\n updateTextInput(session, \"col_custom\", value = newValue)\n })\n observeEvent(input$reset_col, {\n newValue <- \"#4DBBD5, #00A087, #E64B35\"\n updateTextInput(session, \"col_custom\", value = newValue)\n })\n \n # 动态数据\n rv <- reactiveValues() #创建一个反应值对象rv,用于存储反应性的数据\n rv$value <- 1 #初始值为1\n observeEvent(c(input$id_select, input$showtype), {\n # 监听多个位置,如果有变动,则运行下面的内容,生成显示颜色方案的id\n if(input$showtype == \"id\"){\n # 如果此时页面为按id,则按选择的id更新id\n id <- input$id_select %>% as.numeric()\n rv$value <- id\n }\n })\n \n # table.1:所选配色方案\n output$colors_info <- renderReactable({\n showtype <- input$showtype\n custtext <- input$col_custom\n if(showtype == \"id\"){\n # 如果非自定义,则按id显示方案信息\n id <- rv$value\n colo_inf <- data.frame(id=id, \n colors_n=length(colors_nasc[[id]]), \n colors_hex=paste0(colors_nasc[[id]], collapse = \", \"),\n colors_show=paste0(colors_nasc[[id]], collapse = \", \"))\n } else{\n # 如果自定义\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n # 如果输入的字符不是颜色,则显示ERROR\n colo_inf <- data.frame(id=0, \n colors_n=\"ERROR\", \n colors_hex=\"ERROR\",\n colors_show=\"FFFFFF\")\n } else{\n # 如果未输入或输入的字符是颜色,则显示自定义的颜色信息\n colo_inf <- data.frame(id=0, \n colors_n=length(colsig), \n colors_hex=paste0(colsig, collapse = \", \"),\n colors_show=paste0(colsig, collapse = \", \"))\n }\n }\n # 显示表格\n reactable(colo_inf,\n columns = list(\n id = colDef(name = \"方案id\", width = 60, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_n = colDef(name = \"所含颜色数\", width = 90, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_hex = colDef(name = \"颜色HEX码\", width = 650, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_show=colDef(name = \"颜色预览\", align = \"center\", cell = function(value) {\n color_list <- strsplit(value, \", \")[[1]]\n color_divs <- lapply(color_list, function(color) {\n tags$div(style = paste(\"width: 20px; height: 20px; background-color:\", color, \"; display: inline-block; margin-right: 5px; border: 1px solid black;\"))\n })\n do.call(tagList, color_divs)\n }, style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\"))\n ),\n sortable = F, \n resizable = F,\n showPageSizeOptions = F,\n highlight = F,\n striped = T,\n bordered = T,\n compact = F,\n width = \"1020px\",\n fullWidth = F)\n })\n \n # fig.1:绘图效果\n output$plot_example <- renderPlot({\n showtype <- input$showtype\n custtext <- input$col_custom\n coloralp <- input$color_alpha %>% as.numeric()\n if(showtype == \"id\"){\n id <- rv$value\n pic <- examp_plot(id, colors_nasc, alp = coloralp)\n } else{\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n pic <- ggplot(data=NULL, aes(1,1))+\n geom_text(label=\"ERROR\", hjust=0.5, vjust=0.5, size=8)+\n theme_void()\n } else{\n pic <- examp_plot(0, NULL, colsig, alp = coloralp)\n }\n }\n return(pic)\n }) \n \n # fig.2:方案样式\n output$plot_color <- renderPlot({\n showtype <- input$showtype\n custtext <- input$col_custom\n if(showtype == \"id\"){\n id <- rv$value\n pic <- show_col(colors_nasc[[id]])\n } else{\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n pic <- ggplot(data=NULL, aes(1,1))+\n geom_text(label=\"ERROR\", hjust=0.5, vjust=0.5, size=8)+\n theme_void()\n } else{\n pic <- show_col(colsig)\n }\n }\n return(pic)\n }) \n \n # table.2:配色数据库详情\n output$colors_db <- renderReactable({\n showtype <- input$showtype\n colnum <- colors_sect[col_num == input$num_select,] #根据所选颜色数量更新数据库表格\n id_min <- colnum$min\n id_max <- colnum$max\n colo_db <- data.table(id=colors_table$col_id,\n colors_n=colors_table$col_num,\n colors_hex=map_chr(colors_nasc, ~paste0(.x, collapse = \", \")),\n colors_show=map_chr(colors_nasc, ~paste0(.x, collapse = \", \"))) %>% \n .[id>=id_min&id<=id_max,]\n class_name <- if (input$showtype == \"id\") \"reactable-hover\" else \"\" #当用户选择id时,才有hover属性\n reactable(colo_db,\n columns = list(\n id = colDef(name = \"方案id\", width = 60, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_n = colDef(name = \"所含颜色数\", width = 90, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_hex = colDef(name = \"颜色HEX码\", width = 650, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_show=colDef(name = \"颜色预览\", align = \"center\", cell = function(value) {\n color_list <- strsplit(value, \", \")[[1]]\n color_divs <- lapply(color_list, function(color) {\n tags$div(style = paste(\"width: 20px; height: 20px; background-color:\", color, \"; display: inline-block; margin-right: 5px; border: 1px solid black;\"))\n })\n do.call(tagList, color_divs)\n }, style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\"))\n ),\n class = class_name,\n language = reactableLang(searchPlaceholder = \"查找...\"),\n sortable = F,\n resizable = F,\n showPageSizeOptions = T,\n searchable = T,\n highlight = T,\n striped = T,\n bordered = T,\n compact = F,\n width = \"1020px\",\n fullWidth = F,\n theme = reactableTheme(searchInputStyle = list(\"margin-top\" = \"7px\", \"margin-right\" = \"7px\")),\n onClick = if(showtype != \"custom\"){\n JS(\"function(rowInfo, colInfo) {\n if (window.Shiny) {\n Shiny.setInputValue('selected_row', rowInfo.index + 1);\n window.scrollTo({\n top: 0, \n left: 0, \n behavior:'smooth'}); // 平滑滚动到页面顶部\n }\n }\")}\n )\n })\n \n # 点击表格更新绘图效果\n observeEvent(input$selected_row, {\n selected_row <- input$selected_row\n if (!is.null(selected_row)) {\n colnum <- colors_sect[col_num == input$num_select,] #所选颜色数量对应的id区间\n selected_id <- seq(colnum$min, colnum$max)[selected_row] #提取id区间,点击的行号,对应的准确id\n updateSliderTextInput(session, \"id_select\", selected = selected_id) #更新selected\n }\n })\n \n}\n\n# 输出app\nshinyApp(ui, server)", "middle_code": "function(id, colors_nasc=NULL, custom=c(\"\n if(id == 0){\n tcolor <- custom\n ncolor <- length(tcolor)\n } else{\n tcolor <- colors_nasc[[id]]\n ncolor <- length(tcolor)\n }\n dat_bar <- data.frame(a=sample(letters, ncolor, replace = F), \n b=runif(ncolor, 7, 10))\n p_bar <- ggplot(dat_bar, aes(a, b, fill=a))+\n geom_bar(color=\"black\", stat = \"identity\", alpha=alp)+\n scale_y_continuous(expand = c(0,0,0,0.1))+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Bar Chart with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_box <- data.frame(a=sample(letters, ncolor, replace = F),\n b=runif(ncolor*20, 7, 10))\n p_box <- ggplot(dat_box, aes(a, b, fill=a))+\n stat_boxplot(geom = \"errorbar\", linewidth=0.8, width = 0.3)+\n geom_boxplot(alpha=alp)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Boxplot with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_point <- data.frame(a=rep(runif(30), ncolor),\n b=rep(runif(30), ncolor),\n t=rep(sample(letters, ncolor, replace = F), 30))\n p_point <- ggplot(dat_point, aes(a, b, color=t, fill=t))+\n geom_point(shape=21, size=5, alpha=alp)+\n scale_color_manual(values = tcolor)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"scatterplot without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_line <- data.frame(a=rep(1:20, ncolor),\n b=rep(1:ncolor, each=20)+rnorm(20*ncolor, 0, 0.3), \n t=rep(sample(letters, ncolor), each=20))\n p_line <- ggplot(dat_line, aes(a, b, color=t, group=t))+\n geom_line(linewidth=1, alpha=alp)+\n scale_color_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Line chart without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n p <- grid.arrange(p_bar, p_box, p_point, p_line, padding=0, nrow = 2, ncol = 2)\n return(p)\n }", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "r", "sub_task_type": null}, "context_code": [], "task_instance_info": {"created_time": "2025-08-21 01:11:52", "created_task_model": "DeepSeek-R1", "function_skeleton": "function(id, colors_nasc=NULL, custom=c(\"#F5A889\", \"#ACD6EC\"), alp=0.1) {}", "code_description": "This code segment functions to: Generate demonstration visualizations using specified color schemes by creating sample datasets and applying the selected palette across multiple chart types with configurable transparency settings."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```r\n[TASK_BEGIN]\nfunction(id, colors_nasc=NULL, custom=c(\"#F5A889\", \"#ACD6EC\"), alp=0.1) {\n if(!is.null(colors_nasc)){\n colo <- colors_nasc[[id]] # 从数据库获取颜色\n } else {\n colo <- custom # 使用自定义颜色\n }\n colo <- alpha(colo, alp) # 添加透明度\n \n # 创建示例数据\n data <- data.frame(\n category = factor(c(\"A\", \"B\", \"C\", \"D\")),\n value = c(25, 40, 35, 50)\n )\n \n # 生成示例柱状图\n ggplot(data, aes(x=category, y=value, fill=category)) +\n geom_col(color=\"black\", width=0.7) +\n scale_fill_manual(values = rep(colo, length.out=4)) + # 颜色循环使用\n geom_text(aes(label=value), vjust=-0.5, size=5) +\n ggtitle(ifelse(is.null(colors_nasc), \n \"自定义配色绘图示例\", \n paste0(\"数据库配色示例 (ID:\", id, \")\"))) +\n theme_minimal(base_size=14) +\n theme(axis.title = element_blank(),\n legend.position = \"none\",\n panel.grid.major.x = element_blank(),\n plot.title = element_text(hjust=0.5, face=\"bold\"))\n}\n[TASK_END]\n```", "inference_time": "2025-08-21 01-12-29"}, "editdistance_info": {"edit_distance": 12.1805, "calculate_time": "2025-08-21 01:12:29", "true_code_clean": "function(id, colors_nasc=NULL, custom=c(\"\n if(id == 0){\n tcolor <- custom\n ncolor <- length(tcolor)\n } else{\n tcolor <- colors_nasc[[id]]\n ncolor <- length(tcolor)\n }\n dat_bar <- data.frame(a=sample(letters, ncolor, replace = F), \n b=runif(ncolor, 7, 10))\n p_bar <- ggplot(dat_bar, aes(a, b, fill=a))+\n geom_bar(color=\"black\", stat = \"identity\", alpha=alp)+\n scale_y_continuous(expand = c(0,0,0,0.1))+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Bar Chart with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_box <- data.frame(a=sample(letters, ncolor, replace = F),\n b=runif(ncolor*20, 7, 10))\n p_box <- ggplot(dat_box, aes(a, b, fill=a))+\n stat_boxplot(geom = \"errorbar\", linewidth=0.8, width = 0.3)+\n geom_boxplot(alpha=alp)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Boxplot with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_point <- data.frame(a=rep(runif(30), ncolor),\n b=rep(runif(30), ncolor),\n t=rep(sample(letters, ncolor, replace = F), 30))\n p_point <- ggplot(dat_point, aes(a, b, color=t, fill=t))+\n geom_point(shape=21, size=5, alpha=alp)+\n scale_color_manual(values = tcolor)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"scatterplot without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n dat_line <- data.frame(a=rep(1:20, ncolor),\n b=rep(1:ncolor, each=20)+rnorm(20*ncolor, 0, 0.3), \n t=rep(sample(letters, ncolor), each=20))\n p_line <- ggplot(dat_line, aes(a, b, color=t, group=t))+\n geom_line(linewidth=1, alpha=alp)+\n scale_color_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Line chart without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n p <- grid.arrange(p_bar, p_box, p_point, p_line, padding=0, nrow = 2, ncol = 2)\n return(p)\n }", "predict_code_clean": "function(id, colors_nasc=NULL, custom=c(\"\n if(!is.null(colors_nasc)){\n colo <- colors_nasc[[id]] \n } else {\n colo <- custom \n }\n colo <- alpha(colo, alp) \n data <- data.frame(\n category = factor(c(\"A\", \"B\", \"C\", \"D\")),\n value = c(25, 40, 35, 50)\n )\n ggplot(data, aes(x=category, y=value, fill=category)) +\n geom_col(color=\"black\", width=0.7) +\n scale_fill_manual(values = rep(colo, length.out=4)) + \n geom_text(aes(label=value), vjust=-0.5, size=5) +\n ggtitle(ifelse(is.null(colors_nasc), \n \"自定义配色绘图示例\", \n paste0(\"数据库配色示例 (ID:\", id, \")\"))) +\n theme_minimal(base_size=14) +\n theme(axis.title = element_blank(),\n legend.position = \"none\",\n panel.grid.major.x = element_blank(),\n plot.title = element_text(hjust=0.5, face=\"bold\"))\n}"}}
{"repo_name": "colors", "file_name": "/colors/app.R", "inference_info": {"prefix_code": "library(shiny)\nlibrary(purrr)\nlibrary(scales)\nlibrary(ggplot2)\nlibrary(stringr)\nlibrary(magrittr)\nlibrary(reactable)\nlibrary(gridExtra)\nlibrary(htmltools)\nlibrary(data.table)\nlibrary(shinyWidgets)\nlibrary(colourpicker)\nlibrary(shinydashboard)\n\nui <- dashboardPage(\n dashboardHeader(title = \"科研绘图配色推荐器\",titleWidth = \"100%\"),\n dashboardSidebar(disable = TRUE, collapsed = TRUE),\n dashboardBody(\n uiOutput(\"uipanel\"),\n tags$hr(),\n tags$div(align = \"center\", \n tags$p(\"\\ua9 2021-2024, Lcpmgh, All rights reserved.\", style=\"height:8px\"),\n tags$div(align = \"center\",\n actionLink(inputId = \"\", label = \"lcpmgh \", icon = icon(\"github\"), onclick =\"window.open('https://github.com/lcpmgh')\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n actionLink(inputId = \"\", label = \"lcpmgh@gmail.com\", icon = icon(\"envelope\"), onclick =\"window.location.href='mailto:lcpmgh@gmail.com'\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n actionLink(inputId = \"\", label = \"lcpmgh.com\", icon = icon(\"home\"), onclick =\"window.location.href='http://lcpmgh.com/'\")\n ),\n tags$div(align = \"center\",\n tags$a(\"冀ICP备2022003075号\", target=\"_blank\", href=\"https://beian.miit.gov.cn\", style=\"color:#06c; display:inline;\"),\n tags$p(\" \", style = \"display:inline;white-space:pre\"),\n # tags$img(src=\"gaba.png\"),\n tags$a(\"川公网安备51010702002736\", target=\"_blank\", href=\"http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=51010702002736\", style=\"color:#06c; display:inline;\")\n )\n )\n )\n)\n\nserver <- function(input, output, session){\n # 数据\n colors <- readLines(\"@colors.txt\") %>% str_split(\",\") %>% lapply(., sort) %>% .[!duplicated(.)] #读取、排序、去重\n colors_nasc <- map_int(colors, length) %>% order() %>% colors[.] #按子颜色数量排序\n colors_table <- data.frame(col_id=1:length(colors_nasc), col_num=map_int(colors_nasc, length)) #颜色序号数量表\n setDT(colors_table)\n colors_sect <- colors_table[, c(min(.SD),max(.SD)), .SDcols=1,by=\"col_num\"] %>% \n .[, type:=rep(c(\"min\", \"max\"), nrow(.)/2)] %>% \n dcast(., col_num~type, value.var = \"V1\") %>% \n rbind(data.table(col_num=\"all\", \n max=max(colors_table$col_id), \n min=min(colors_table$col_id)), .) #提取每个数量区间的id范围 \n # 函数\n examp_plot <- function(id, colors_nasc=NULL, custom=c(\"#F5A889\", \"#ACD6EC\"), alp=0.1){\n # 函数,根据给定颜色id或自定义的颜色,画4个案例图\n # id=0时自定义颜色,否则按colors_nasc中的颜色\n if(id == 0){\n tcolor <- custom\n ncolor <- length(tcolor)\n } else{\n tcolor <- colors_nasc[[id]]\n ncolor <- length(tcolor)\n }\n # 图1.bar\n dat_bar <- data.frame(a=sample(letters, ncolor, replace = F), \n b=runif(ncolor, 7, 10))\n p_bar <- ggplot(dat_bar, aes(a, b, fill=a))+\n geom_bar(color=\"black\", stat = \"identity\", alpha=alp)+\n scale_y_continuous(expand = c(0,0,0,0.1))+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Bar Chart with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n # 图2.box\n dat_box <- data.frame(a=sample(letters, ncolor, replace = F),\n b=runif(ncolor*20, 7, 10))\n p_box <- ggplot(dat_box, aes(a, b, fill=a))+\n stat_boxplot(geom = \"errorbar\", linewidth=0.8, width = 0.3)+\n geom_boxplot(alpha=alp)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Boxplot with outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n # 图3.point\n dat_point <- data.frame(a=rep(runif(30), ncolor),\n b=rep(runif(30), ncolor),\n t=rep(sample(letters, ncolor, replace = F), 30))\n p_point <- ggplot(dat_point, aes(a, b, color=t, fill=t))+\n geom_point(shape=21, size=5, alpha=alp)+\n scale_color_manual(values = tcolor)+\n scale_fill_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"scatterplot without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n # 图4.line\n dat_line <- data.frame(a=rep(1:20, ncolor),\n b=rep(1:ncolor, each=20)+rnorm(20*ncolor, 0, 0.3), \n t=rep(sample(letters, ncolor), each=20))\n p_line <- ggplot(dat_line, aes(a, b, color=t, group=t))+\n geom_line(linewidth=1, alpha=alp)+\n scale_color_manual(values = tcolor)+\n labs(x=\"x-axis\", y=\"y-axis\", title = \"Line chart without outlines\")+\n theme_bw()+theme(plot.title = element_text(hjust = 0.5))\n # 合并\n p <- grid.arrange(p_bar, p_box, p_point, p_line, padding=0, nrow = 2, ncol = 2)\n return(p)\n } \n iscolors <- ", "suffix_code": "\n \n ##### ui #####\n output$uipanel <- renderUI({\n tagList(\n tags$head(tags$link(rel = \"shortcut icon\", href = \"pmgh.ico\")),\n tags$style(HTML(\".custom-margin {margin-bottom: 20px;}\")),\n tags$div(class = \"custom-margin\",\n HTML(\"<h3 style='display: inline;'>方案选择</h3>\"),\n HTML(paste(\"<h5 style='display: inline;'>(数据库内现有\", length(colors_nasc), \"种配色方案)</h5>\"))),\n radioButtons(inputId = \"showtype\",\n label = NULL,\n choices = list(\"配色数据库方案id\" = \"id\", \"自定义配色方案\" = \"custom\"),\n inline = T,\n selected = c(\"按数量\" = \"id\")),\n # 按id\n conditionalPanel(condition = \"input.showtype == 'id'\",\n selectInput(inputId = \"num_select\", \n label = \"选色颜色数量\", \n choices = colors_sect$col_num,\n selected = \"all\",\n multiple = FALSE),\n div(style = \"display: flex; align-items: center; width:700px\",\n sliderTextInput(inputId = \"id_select\", \n label = \"选择方案id\", \n choices = seq(colors_sect[[1, \"min\"]], colors_sect[[1, \"max\"]]),\n selected = colors_sect[[1, \"min\"]],\n width = \"500px\", \n grid = T),\n div(style = \"width: 15%; text-align: center; padding-left: 20px;\", \n shiny::actionButton(inputId = \"pre\", \n label = \"上一个\",\n icon = icon(\"angle-left\"))),\n div(style = \"width: 15%; text-align: center;\", \n shiny::actionButton(inputId = \"nex\", \n label = \"下一个\",\n icon = icon(\"angle-right\"))))),\n # 自定义\n conditionalPanel(condition = \"input.showtype == 'custom'\",\n div(style = \"display: flex; align-items: center; width:700px\",\n colourInput(inputId = \"sele_col\", \n label = \"追加颜色\", \n allowTransparent = T,\n width = \"174px\",\n value = \"skyblue\"),\n div(style = \"width: 15%; text-align: center; padding-left: 20px; padding-top: 10px;\", \n shiny::actionButton(inputId = \"add_col\", \n label = \"加入\",\n icon = icon(\"plus\"))),\n div(style = \"width: 15%; text-align: center; padding-left: 10px; padding-top: 10px;\", \n shiny::actionButton(inputId = \"reset_col\", \n label = \"重置\",\n icon = icon(\"redo\")))),\n textInput(inputId = \"col_custom\",\n label = \"自定义颜色(HEX码,多个颜色以逗号、顿号、空格、分号间隔,颜色数量不可超过16,结果将去重):\",\n width = \"1000px\",\n value = \"#4DBBD5, #00A087, #E64B35\")),\n # 选择结果\n h3(\"所选配色方案\"),\n reactableOutput(outputId = \"colors_info\"),\n # h3(\"颜色透明度\"),\n sliderTextInput(inputId = \"color_alpha\",\n label = \"颜色透明度(颜色图层的alpha值)\", \n choices = seq(0,1, by=0.05),\n selected = 1,\n grid = T,\n width = \"500px\"),\n div(style = \"display: flex; align-items: center; width:1021px\",\n div(style = \"width: 50%; text-align: center;\", \n h3(\"绘图效果\"),\n plotOutput(outputId = \"plot_example\", width = \"510px\", height = \"400px\")),\n div(style = \"width: 50%; text-align: center;\", \n h3(\"方案样式\"),\n plotOutput(outputId = \"plot_color\", width = \"510px\", height = \"400px\"))),\n h3(\"配色数据库(点击表格显示绘图效果)\"),\n tags$head(tags$style(HTML(\".reactable-hover .rt-tr-group:hover {cursor: pointer;}\"))), \n reactableOutput(outputId = \"colors_db\")\n )\n })\n \n ##### server #####\n # 点击颜色数量,更新下面sliderTextInput中备选id的区间\n observeEvent(input$num_select, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n updateSliderTextInput(session, \"id_select\",\n choices = selected_seq,\n selected = selected_seq[1])\n })\n # 点击上一个或下一个后,更新slider对应的值\n observeEvent(input$pre, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n newValue <- max(selected_seq[1], as.numeric(input$id_select) - 1)\n updateSliderTextInput(session, \"id_select\", selected = newValue)\n })\n observeEvent(input$nex, {\n selected_row <- colors_sect[col_num == input$num_select,]\n selected_seq <- seq(selected_row$min, selected_row$max)\n newValue <- min(tail(selected_seq,1), as.numeric(input$id_select) + 1)\n updateSliderTextInput(session, \"id_select\", selected = newValue)\n })\n # 点击加入或重置颜色,更新textinput\n observeEvent(input$add_col, {\n newValue <- str_split(input$col_custom, \"[,,;、 ]\") %>%\n unlist() %>%\n str_trim() %>%\n .[nchar(.) > 0] %>%\n c(., input$sele_col) %>%\n .[!duplicated(.)] %>%\n paste0(collapse = \",\")\n updateTextInput(session, \"col_custom\", value = newValue)\n })\n observeEvent(input$reset_col, {\n newValue <- \"#4DBBD5, #00A087, #E64B35\"\n updateTextInput(session, \"col_custom\", value = newValue)\n })\n \n # 动态数据\n rv <- reactiveValues() #创建一个反应值对象rv,用于存储反应性的数据\n rv$value <- 1 #初始值为1\n observeEvent(c(input$id_select, input$showtype), {\n # 监听多个位置,如果有变动,则运行下面的内容,生成显示颜色方案的id\n if(input$showtype == \"id\"){\n # 如果此时页面为按id,则按选择的id更新id\n id <- input$id_select %>% as.numeric()\n rv$value <- id\n }\n })\n \n # table.1:所选配色方案\n output$colors_info <- renderReactable({\n showtype <- input$showtype\n custtext <- input$col_custom\n if(showtype == \"id\"){\n # 如果非自定义,则按id显示方案信息\n id <- rv$value\n colo_inf <- data.frame(id=id, \n colors_n=length(colors_nasc[[id]]), \n colors_hex=paste0(colors_nasc[[id]], collapse = \", \"),\n colors_show=paste0(colors_nasc[[id]], collapse = \", \"))\n } else{\n # 如果自定义\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n # 如果输入的字符不是颜色,则显示ERROR\n colo_inf <- data.frame(id=0, \n colors_n=\"ERROR\", \n colors_hex=\"ERROR\",\n colors_show=\"FFFFFF\")\n } else{\n # 如果未输入或输入的字符是颜色,则显示自定义的颜色信息\n colo_inf <- data.frame(id=0, \n colors_n=length(colsig), \n colors_hex=paste0(colsig, collapse = \", \"),\n colors_show=paste0(colsig, collapse = \", \"))\n }\n }\n # 显示表格\n reactable(colo_inf,\n columns = list(\n id = colDef(name = \"方案id\", width = 60, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_n = colDef(name = \"所含颜色数\", width = 90, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_hex = colDef(name = \"颜色HEX码\", width = 650, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_show=colDef(name = \"颜色预览\", align = \"center\", cell = function(value) {\n color_list <- strsplit(value, \", \")[[1]]\n color_divs <- lapply(color_list, function(color) {\n tags$div(style = paste(\"width: 20px; height: 20px; background-color:\", color, \"; display: inline-block; margin-right: 5px; border: 1px solid black;\"))\n })\n do.call(tagList, color_divs)\n }, style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\"))\n ),\n sortable = F, \n resizable = F,\n showPageSizeOptions = F,\n highlight = F,\n striped = T,\n bordered = T,\n compact = F,\n width = \"1020px\",\n fullWidth = F)\n })\n \n # fig.1:绘图效果\n output$plot_example <- renderPlot({\n showtype <- input$showtype\n custtext <- input$col_custom\n coloralp <- input$color_alpha %>% as.numeric()\n if(showtype == \"id\"){\n id <- rv$value\n pic <- examp_plot(id, colors_nasc, alp = coloralp)\n } else{\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n pic <- ggplot(data=NULL, aes(1,1))+\n geom_text(label=\"ERROR\", hjust=0.5, vjust=0.5, size=8)+\n theme_void()\n } else{\n pic <- examp_plot(0, NULL, colsig, alp = coloralp)\n }\n }\n return(pic)\n }) \n \n # fig.2:方案样式\n output$plot_color <- renderPlot({\n showtype <- input$showtype\n custtext <- input$col_custom\n if(showtype == \"id\"){\n id <- rv$value\n pic <- show_col(colors_nasc[[id]])\n } else{\n colsig <- iscolors(custtext)\n if(isFALSE(colsig)){\n pic <- ggplot(data=NULL, aes(1,1))+\n geom_text(label=\"ERROR\", hjust=0.5, vjust=0.5, size=8)+\n theme_void()\n } else{\n pic <- show_col(colsig)\n }\n }\n return(pic)\n }) \n \n # table.2:配色数据库详情\n output$colors_db <- renderReactable({\n showtype <- input$showtype\n colnum <- colors_sect[col_num == input$num_select,] #根据所选颜色数量更新数据库表格\n id_min <- colnum$min\n id_max <- colnum$max\n colo_db <- data.table(id=colors_table$col_id,\n colors_n=colors_table$col_num,\n colors_hex=map_chr(colors_nasc, ~paste0(.x, collapse = \", \")),\n colors_show=map_chr(colors_nasc, ~paste0(.x, collapse = \", \"))) %>% \n .[id>=id_min&id<=id_max,]\n class_name <- if (input$showtype == \"id\") \"reactable-hover\" else \"\" #当用户选择id时,才有hover属性\n reactable(colo_db,\n columns = list(\n id = colDef(name = \"方案id\", width = 60, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_n = colDef(name = \"所含颜色数\", width = 90, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_hex = colDef(name = \"颜色HEX码\", width = 650, align = \"center\", style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\")),\n colors_show=colDef(name = \"颜色预览\", align = \"center\", cell = function(value) {\n color_list <- strsplit(value, \", \")[[1]]\n color_divs <- lapply(color_list, function(color) {\n tags$div(style = paste(\"width: 20px; height: 20px; background-color:\", color, \"; display: inline-block; margin-right: 5px; border: 1px solid black;\"))\n })\n do.call(tagList, color_divs)\n }, style = list(display = \"flex\", alignItems = \"center\", justifyContent = \"center\"))\n ),\n class = class_name,\n language = reactableLang(searchPlaceholder = \"查找...\"),\n sortable = F,\n resizable = F,\n showPageSizeOptions = T,\n searchable = T,\n highlight = T,\n striped = T,\n bordered = T,\n compact = F,\n width = \"1020px\",\n fullWidth = F,\n theme = reactableTheme(searchInputStyle = list(\"margin-top\" = \"7px\", \"margin-right\" = \"7px\")),\n onClick = if(showtype != \"custom\"){\n JS(\"function(rowInfo, colInfo) {\n if (window.Shiny) {\n Shiny.setInputValue('selected_row', rowInfo.index + 1);\n window.scrollTo({\n top: 0, \n left: 0, \n behavior:'smooth'}); // 平滑滚动到页面顶部\n }\n }\")}\n )\n })\n \n # 点击表格更新绘图效果\n observeEvent(input$selected_row, {\n selected_row <- input$selected_row\n if (!is.null(selected_row)) {\n colnum <- colors_sect[col_num == input$num_select,] #所选颜色数量对应的id区间\n selected_id <- seq(colnum$min, colnum$max)[selected_row] #提取id区间,点击的行号,对应的准确id\n updateSliderTextInput(session, \"id_select\", selected = selected_id) #更新selected\n }\n })\n \n}\n\n# 输出app\nshinyApp(ui, server)", "middle_code": "function(str){\n if(nchar(str_trim(str))<1) return(F) \n colo <- str_split(str, \"[,,;、 ]\") %>% unlist() %>% str_trim() %>% .[nchar(.) > 0] %>% .[!duplicated(.)]\n sig <- str_detect(colo, \"^\n if(any(!sig) | length(colo)>16){\n return(F)\n } else{\n return(colo)\n }\n }", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "r", "sub_task_type": null}, "context_code": [], "task_instance_info": {"created_time": "2025-08-21 01:11:52", "created_task_model": "DeepSeek-R1", "function_skeleton": "function(str) {}", "code_description": "This code segment functions to: Validate user-provided color strings by checking for proper hexadecimal format, removing duplicates, and enforcing constraints on maximum entries while parsing delimited input into standardized values."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```r\n[TASK_BEGIN]\nfunction(str) {\n colors <- strsplit(str, \"[,,;、 ]+\")[[1]] %>% \n str_trim() %>% \n .[. != \"\"]\n if(length(colors) == 0) return(FALSE)\n \n is_valid <- sapply(colors, function(color) {\n tryCatch(\n {\n grDevices::col2rgb(color)\n TRUE\n },\n error = function(e) FALSE\n )\n })\n \n if(all(is_valid)) {\n return(colors)\n } else {\n return(FALSE)\n }\n}\n[TASK_END]\n```", "inference_time": "2025-08-21 01-12-33"}, "editdistance_info": {"edit_distance": 12.931, "calculate_time": "2025-08-21 01:12:33", "true_code_clean": "function(str){\n if(nchar(str_trim(str))<1) return(F) \n colo <- str_split(str, \"[,,;、 ]\") %>% unlist() %>% str_trim() %>% .[nchar(.) > 0] %>% .[!duplicated(.)]\n sig <- str_detect(colo, \"^\n if(any(!sig) | length(colo)>16){\n return(F)\n } else{\n return(colo)\n }\n }", "predict_code_clean": "function(str) {\n colors <- strsplit(str, \"[,,;、 ]+\")[[1]] %>% \n str_trim() %>% \n .[. != \"\"]\n if(length(colors) == 0) return(FALSE)\n is_valid <- sapply(colors, function(color) {\n tryCatch(\n {\n grDevices::col2rgb(color)\n TRUE\n },\n error = function(e) FALSE\n )\n })\n if(all(is_valid)) {\n return(colors)\n } else {\n return(FALSE)\n }\n}"}}