R Shiny运行报错object of type 'closure' is not subsettable求助
错误根因
- UI结构定义存在语法错误:你在
fluidPage内部再次对ui变量赋值navbarPage,导致Shiny解析UI结构时出现异常 - 系数输出的响应式逻辑错误:
output$coef_value使用renderUI时,内部嵌套了renderText,最终返回的是一个函数对象(closure类型),Shiny渲染UI时无法识别该类型对象,就会抛出object of type 'closure' is not subsettable报错
修正后完整代码
library(shiny) library(shinythemes) library(dplyr) library(tidyverse) library(lubridate) library(stringr) function.test<-function(){ test <- structure( list(date1= c("2021-06-28","2021-06-28","2021-06-28","2021-06-28"), date2 = c("2021-07-01","2021-07-01","2021-07-02","2021-07-03"), Category = c("FDE","ABC","FDE","FDE"), Week= c("Friday","Monday","Wednesday","Wednesday"), DR1 = c(4,1,1,6), DR01 = c(4,1,1,3), DR02= c(4,2,2,4),DR03= c(9,5,5,2), DR04 = c(5,4,3,4),DR05 = c(5,4,2,4), DR06 = c(2,4,2,4),DR07 = c(2,5,4,2), DR08 = c(3,4,2,4),DR09 = c(2,3,4,2)), class = "data.frame", row.names = c(NA, -4L)) return(test) } f1 <- function(test, dmda, CategoryChosse) { x<-test %>% select(starts_with("DR0")) x<-cbind(test, setNames(test$DR1 - x, paste0(names(x), "_PV"))) PV<-select(x, date2,Week, Category, DR1, ends_with("PV")) med<-PV %>% group_by(Category,Week) %>% summarize(across(ends_with("PV"), median), .groups = "drop") SPV<-test%>% inner_join(med, by = c('Category', 'Week')) %>% mutate(across(matches("^DR0\\d+$"), ~.x + get(paste0(cur_column(), '_PV')), .names = '{col}_{col}_PV')) %>% select(date1:Category, DR01_DR01_PV:last_col()) SPV<-data.frame(SPV) mat1 <- test %>% filter(date2 == dmda, Category == CategoryChosse) %>% select(starts_with("DR0")) %>% pivot_longer(cols = everything()) %>% arrange(desc(row_number())) %>% mutate(cs = cumsum(value)) %>% filter(cs == 0) %>% pull(name) dropnames <- paste0(mat1,"_",mat1, "_PV") SPV <- SPV %>% filter(date2 == dmda, Category == CategoryChosse) %>% select(-any_of(dropnames)) newdados<-SPV %>% filter(date2 == ymd(dmda)) %>% group_by(Category) %>% summarize(across(starts_with("DR0"), sum), .groups = "drop") %>% pivot_longer(cols= -Category, names_pattern = "DR0(.+)", values_to = "val") %>% mutate(name = readr::parse_number(name)) colnames(newdados)[-1]<-c("Days","Numbers") if(as.Date(dmda) < min(as.Date(test$date1))){ newdados <- newdados %>% group_by(Category) %>% slice(1:max(Days)+1) %>% ungroup }else{ newdados <- newdados %>% group_by(Category) %>% slice((as.Date(dmda) - min(as.Date(test$date1) [ test$Category == first(Category)])):max(Days)+1) %>% ungroup } plot(Numbers ~ Days, xlim= c(0,45), ylim= c(0,30), xaxs='i',data = newdados,main = paste0(dmda, "-", CategoryChosse)) model <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = newdados, algorithm = "port") new.newdados <- data.frame(Days = with(newdados, seq(min(Days),max(Days),len = 45))) new.newdados <- rbind(0, new.newdados) lines(new.newdados$Days,predict(model,newdata = new.newdados),lwd=2) coef_val<-coef(model)[2] points(0, coef_val, col="red",pch=19,cex = 2,xpd=TRUE) text(.99,coef_val + 1,max(0, round(coef_val,1)), cex=1.1,pos=4,offset =1,col="black") return(coef_val) } ui <- fluidPage( shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE, br(), tabPanel("", sidebarLayout( sidebarPanel( uiOutput("date"), uiOutput("mycode"), uiOutput("coef_value"), br(), ), mainPanel( tabsetPanel( tabPanel("", plotOutput("graph",width = "100%", height = "600") ) ), )) )) server <- function(input, output,session) { mydados <- reactive(function.test()) output$date <- renderUI({ req(mydados()) all_dates <- seq(as.Date('2021-01-01'), as.Date('2021-01-15'), by = "day") disabled <- as.Date(setdiff(all_dates, as.Date(mydados()$date2)), origin = "1970-01-01") dateInput(input = "date2", label = h4("选择日期"), min = min(mydados()$date2), max = max(mydados()$date2), value = min(mydados()$date2), format = "dd-mm-yyyy", datesdisabled = disabled) }) output$coef_value <- renderUI({ req(input$date2,input$code) coef_val <- f1(mydados(),as.character(input$date2),as.character(input$code)) tags$h4(paste("系数值为", round(coef_val, 1))) }) output$mycode <- renderUI({ req(input$date2) test <- mydados() test2 <- test[as.Date(test$date2) %in% input$date2,] selectInput("code", label = h4("选择分类"),choices=unique(test2$Category)) }) output$graph <- renderPlot({ req(input$date2,input$code) f1(mydados(),as.character(input$date2),as.character(input$code)) }) } shinyApp(ui = ui, server = server)
内容的提问来源于stack exchange,提问作者Antonio
相关产品推荐
相关产品推荐

