R Shiny应用中均值、极值、标准差计算报错的解决求助
问题与解决方案
问题描述
开发了一个R Shiny应用,用于加载公司机密数据并动态生成多幅组合图表。已确认目标列存在且非空,但计算均值、标准差等统计量时出现报错,不过coord_cartesian(ylim = range(mydf()$par))能正常运行。
原代码
observe({ req(input$Parmeter) lapply(input$Parmeter, function(par){ p1<-ggplot(mydf(), aes_string(x ="Lot",y = par,group=1)) + geom_point(size=2,colour="#007A9D",shape=4)+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ theme( axis.text.y.left = element_text(color = "#007A9D"), axis.title.y.left = element_text(color = "#007A9D") ) p2 <- ggplot(mydf(), aes_string(y = par)) + geom_histogram(binwidth = 2) + labs(y = NULL) coord_cartesian(ylim = range(mydf()$par)) p3 <- ggplot(mydf()) + theme_void() + annotate( geom = "label", x = 0, y = .5, label = paste( "Mean = ", round(mean(mydf()$par), 1), "\n", "sigma = ", round(sd(mydf()$par), 1),"\n", "Min = ", round(min(mydf()$par), 1),"\n", "Max = ", round(max(mydf()$par), 1),"\n", "Cp/k = ", round(max(mydf()$par), 1) ), hjust = 0 ) output[[paste("plot", par, sep = "_")]] <- renderPlot({ p3 + p1 + p2 + plot_layout(widths = c(1, 3, 1)) }, width = 1800, height = 350) } ) })
报错信息
Warning in min(x, na.rm = na.rm) : no non-missing arguments to min; returning Inf Warning in max(x, na.rm = na.rm) : no non-missing arguments to max; returning -Inf Warning in mean.default(mydf()$par) : argument is not numeric or logical: returning NA Warning in min(mydf()$par) : no non-missing arguments to min; returning Inf Warning in max(mydf()$par) : no non-missing arguments to max; returning -Inf Warning in max(mydf()$par) : no non-missing arguments to max; returning -Inf
问题根源
- 列提取方式错误:
mydf()$par中$是按字面量匹配列名,而par是字符串变量,无法正确提取目标列,导致统计函数拿到非数值甚至空值。 - 语法错误:p2中
coord_cartesian是单独一行,未与前面的ggplot对象用+连接,实际未生效(原代码中它能运行但不影响图表,因为错误的range(mydf()$par)返回c(Inf, -Inf),相当于没有限制)。 - 未处理缺失值:即使列非空,可能存在NA值,统计函数默认不忽略NA会引发报错。
修复后的代码
observe({ req(input$Parmeter) lapply(input$Parmeter, function(par){ # 缓存reactive数据,避免重复调用 df <- mydf() # 用[[提取目标列,支持字符串变量名 target_col <- df[[par]] p1 <- ggplot(df, aes_string(x ="Lot", y = par, group=1)) + geom_point(size=2, colour="#007A9D", shape=4)+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ theme( axis.text.y.left = element_text(color = "#007A9D"), axis.title.y.left = element_text(color = "#007A9D") ) p2 <- ggplot(df, aes_string(y = par)) + geom_histogram(binwidth = 2) + labs(y = NULL) + # 连接coord_cartesian,添加na.rm处理缺失值 coord_cartesian(ylim = range(target_col, na.rm = TRUE)) p3 <- ggplot(df) + theme_void() + annotate( geom = "label", x = 0, y = .5, label = paste( "Mean = ", round(mean(target_col, na.rm = TRUE), 1), "\n", "sigma = ", round(sd(target_col, na.rm = TRUE), 1),"\n", "Min = ", round(min(target_col, na.rm = TRUE), 1),"\n", "Max = ", round(max(target_col, na.rm = TRUE), 1),"\n", "Cp/k = ", round(max(target_col, na.rm = TRUE), 1) ), hjust = 0 ) output[[paste("plot", par, sep = "_")]] <- renderPlot({ p3 + p1 + p2 + plot_layout(widths = c(1, 3, 1)) }, width = 1800, height = 350) }) })
关键修改点
- 用
df <- mydf()缓存reactive数据,减少重复调用带来的性能损耗和潜在问题 - 替换
mydf()$par为df[[par]],正确通过字符串变量提取列 - 所有统计函数(
mean/sd/min/max/range)添加na.rm=TRUE,处理数据中的缺失值 - 修正p2的语法,将
coord_cartesian通过+与ggplot对象连接,确保生效
内容的提问来源于stack exchange,提问作者samir
相关产品推荐
相关产品推荐

