函数检查 (length (list) != 0) 总是返回 length = 0
Posted
技术标签:
【中文标题】函数检查 (length (list) != 0) 总是返回 length = 0【英文标题】:Function check for (length (list) != 0) always returns length = 0 【发布时间】:2022-01-14 17:16:06 【问题描述】:我在 r-markdown 脚本中使用 UDF 来绘制存储列表中的数据。当我运行我的函数主体时,一切正常,但是当我尝试使用已验证在主体中工作的已定义参数执行该函数时,我所有检查列表长度的逻辑都返回FALSE
。
这是我制作的一个示例数据集:
library(dplyr)
library(plotly)
plotdataframe <- list()
dataframerows <- list()
AllExplVarCoeffs <- list()
AllExplVarNames <- list()
NrmPolcts <- list()
AllExplVarCoeff <- list()
AllExplVarName <- list()
NrmPolct <- list()
for (i in 1:10)
for (j in 1:8)
for(n in 1:16)
if (n == 3 || n == 8 || n == 4 && j == 5 || n == 9 && j == 5)
AllExplVarName <-NULL
AllExplVarCoeff <- NULL
NrmPolct <- NULL
else
set.seed(10)
AllExplVarName <-c(1,2,3,4,5,6,7,8,9,10)
AllExplVarCoeff <- runif(10, min=-1, max=1)
NrmPolct <- c(.015,.005,.33,.32,.225,.025,.03,.05,0 ,0)
padvarcnt <-(length(AllExplVarCoeff)-length(NrmPolct))
for (l in seq_len(padvarcnt))
padval = l*0
NrmPolCt1 <- c(NrmPolCt,padval)
NrmPolct1 <- as.numeric(unlist(NrmPolct))
AllExplVarNames <- cbind(AllExplVarNames,AllExplVarName)
AllExplVarCoeffs <- cbind(AllExplVarCoeffs,AllExplVarCoeff)
NrmPolcts <- cbind(NrmPolcts,NrmPolct1)
dataframerows <- cbind(AllExplVarNames,AllExplVarCoeffs,NrmPolcts)
assign(paste("plotdataframe",i, j, n, sep="_"),cbind(plotdataframe,dataframerows))
plotdataframe <- list()
dataframerows <- list()
AllExplVarCoeffs <- list()
AllExplVarNames <- list()
NrmPolcts <- list()
assign(paste("AllExplVarNames",i, j, n, sep="_"),cbind(AllExplVarNames,AllExplVarName))
assign(paste("AllExplVarCoeffs",i ,j ,n, sep="_"),cbind(AllExplVarCoeffs,AllExplVarCoeff))
assign(paste("NrmPolcts",i, j, n, sep="_"),cbind(NrmPolcts,NrmPolct1))
AllExplVarCoeffs <- list()
AllExplVarNames <- list()
NrmPolcts <- list()
NULL
条目代表我的真实数据中的情况,其中“n”的值将返回一个空集。
我的函数是这样定义的(请忽略丑陋的情节,我时间紧迫,所以我没有让它看起来很好):
DrawTestExVar <- function(x,j)
print(paste("x=",x))
print(paste("j=",j))
StCrPltData <- ls(pattern=paste0("plotdataframe_",x,"_",j))
SCPD <- mget(StCrPltData)
for(n in 1:16)
print(paste("n=",n))
print(paste0('plotdataframe_',x,'_',j,'_',n))
if (length(unlist((SCPD[c(paste0('plotdataframe_',x,'_',j,'_',n))]))) != 0)
n1=n
j1=j
print(n1)
plot1 <- plot_ly(as.data.frame(SCPD[c(paste0('plotdataframe_',x,'_',j,'_',n1))])) %>%
add_trace(x=~unlist(SCPD[c(paste0('plotdataframe_',x,'_',j,'_',n1))][[1]][,'AllExplVarName']), y=~unlist(SCPD[c(paste0('plotdataframe_',x,'_',j,'_',n1))][[1]][,'AllExplVarCoeff']), yaxis="y", type="scatter", mode="markers", name="Coef Values") %>%
add_bars(x=~unlist(SCPD[c(paste0('plotdataframe_',x,'_',j,'_',n1))][[1]][,'AllExplVarName']), y=~unlist(SCPD[c(paste0('plotdataframe_',x,'_',j,'_',n1))][[1]][,'NrmPolct1']), yaxis="y2", name="% Polcy Count")
print(plot1)
else
print("Next")
但当我执行函数时:DrawTestExVar(5,3)
我注意到但长度 = 0 响应:
> DrawTestExVar(5,3)
[1] "x= 5"
[1] "j= 3"
[1] "n= 1"
[1] "plotdataframe_5_3_1"
[1] "Next"
[1] "n= 2"
[1] "plotdataframe_5_3_2"
[1] "Next"
[1] "n= 3"
[1] "plotdataframe_5_3_3"
[1] "Next"
[1] "n= 4"
[1] "plotdataframe_5_3_4"
[1] "Next"
[1] "n= 5"
[1] "plotdataframe_5_3_5"
[1] "Next"
[1] "n= 6"
[1] "plotdataframe_5_3_6"
[1] "Next"
[1] "n= 7"
[1] "plotdataframe_5_3_7"
[1] "Next"
[1] "n= 8"
[1] "plotdataframe_5_3_8"
[1] "Next"
[1] "n= 9"
[1] "plotdataframe_5_3_9"
[1] "Next"
[1] "n= 10"
[1] "plotdataframe_5_3_10"
[1] "Next"
[1] "n= 11"
[1] "plotdataframe_5_3_11"
[1] "Next"
[1] "n= 12"
[1] "plotdataframe_5_3_12"
[1] "Next"
[1] "n= 13"
[1] "plotdataframe_5_3_13"
[1] "Next"
[1] "n= 14"
[1] "plotdataframe_5_3_14"
[1] "Next"
[1] "n= 15"
[1] "plotdataframe_5_3_15"
[1] "Next"
[1] "n= 16"
[1] "plotdataframe_5_3_16"
[1] "Next"
我认为它相对简单;从全局环境中选择名称与输入模式匹配的列表,然后过滤掉返回长度为 0 列表的“n”值并在循环中绘制余数。我已经构建了另外两个函数来从存储数据中正常工作,所以我相信问题出在if else
循环中,问题似乎源于我如何定义或使用我的论点。我对 r 函数文档的深入研究都没有发现我做错了什么,而且我为使调用更明确所做的尝试都没有奏效。
想法?
更新 1:打印一些故障排除语句后,很明显错误实际上在于正在读取或应用的参数中。当我print()
运行 DrawTestExVar(5,3) 时 x 和 j 的值时,x=5 和 j=3,根据需要。 print()
循环中 n 的值表明它适当地递增并正确应用于粘贴命令,但所有 print(length(list))
返回长度为 0。
【问题讨论】:
【参考方案1】:答案在于函数的构建方式和访问数据的方式。列表参考
StCrPltData <- ls(pattern=paste0("plotdataframe_",x,"_",j))
和
SCPD <- mget(StCrPltData)
需要在函数之外发生,以便它可以实际访问数据帧。此外,可以使用函数调用中的定义完全删除 StCrPltData。函数参数应该看起来像
DrawTestExVar <- function(x,j,SCPD)
并调用函数
DrawTestExVar(5,3,mget(ls(pattern=paste0("plotdataframe_",5,"_",3))))
运行它会给出图(我不会显示,因为它们非常难看)和故障排除打印:
> DrawTestExVar(5,3,mget(ls(pattern=paste0("plotdataframe_",5,"_",3))))
[1] "x= 5"
[1] "j= 3"
[1] "n= 1"
[1] "plotdataframe_5_3_1"
[1] 1
[1] "n= 2"
[1] "plotdataframe_5_3_2"
[1] 2
[1] "n= 3"
[1] "plotdataframe_5_3_3"
[1] "Next"
[1] "n= 4"
[1] "plotdataframe_5_3_4"
[1] 4
[1] "n= 5"
[1] "plotdataframe_5_3_5"
[1] 5
[1] "n= 6"
[1] "plotdataframe_5_3_6"
[1] 6
[1] "n= 7"
[1] "plotdataframe_5_3_7"
[1] 7
[1] "n= 8"
[1] "plotdataframe_5_3_8"
[1] "Next"
[1] "n= 9"
[1] "plotdataframe_5_3_9"
[1] 9
[1] "n= 10"
[1] "plotdataframe_5_3_10"
[1] 10
[1] "n= 11"
[1] "plotdataframe_5_3_11"
[1] 11
[1] "n= 12"
[1] "plotdataframe_5_3_12"
[1] 12
[1] "n= 13"
[1] "plotdataframe_5_3_13"
[1] 13
[1] "n= 14"
[1] "plotdataframe_5_3_14"
[1] 14
[1] "n= 15"
[1] "plotdataframe_5_3_15"
[1] 15
[1] "n= 16"
[1] "plotdataframe_5_3_16"
[1] 16
遵循预期的输出,显示功能按预期工作。
【讨论】:
以上是关于函数检查 (length (list) != 0) 总是返回 length = 0的主要内容,如果未能解决你的问题,请参考以下文章