且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我无法理解的错误。 “已经在评估中的承诺......”

更新时间:2023-02-01 19:08:04

有人可以纠正我这个问题,但我认为您将Primus和Name作为对象传递给函数,并且正在查找.GlobalEnv中的这些对象,因此未找到它们,因此你的功能没有执行你的大部分指令(并且什么也没有返回)。

相反,试试这个...

 字符串<  -  data [,column] ## data $ column是一个字符向量
found = str_detect(字符串,模式)
yez = rownames(data [which(found == TRUE),])
hhh = as.numeric(yez)+ 1
return(hhh)
}

然后你必须使用如下的函数:

  namezz(Primus,data = data)#在这种情况下,列的默认值是Name b $ b  

传递data = data的问题是在这里很好地解释了。从那篇文章摘录(他们引用testparams你会引用数据)... ... $ / b>


其中一个最重要的事情了解对函数的
参数的评估是
,提供的参数和默认参数的处理方式不同。
提供给函数的参数
在评估框架中评估调用
函数,函数的默认参数
在函数的评估框架中进行评估。



参数testparams,当不匹配时参数被传递给
,默认值是变量testparams
在foo定义的环境中查找 not 的值,而 not
中调用foo的环境,而是在本地环境中
在函数被调用时创建并且参数映射到
值时创建 - 并且在此环境中,testparams是一个参数ter,这是
已经在评估中,因此递归查找错误。


I am trying to write a function that finds pattern in names, with the help of stringr package. My function looks like following:

namezz=function(thepatternx,data=data,column=Name){

  library(stringr)

  thepattern=as.character(quote(thepatternx))

  pattern <- thepattern
  strings <- data$column ##data$column is a character vector
  found=str_detect(strings, pattern)
  yez= rownames(data[which(found==TRUE),])
  hhh=as.numeric(yez)+1
  return(hhh)

}

When I call the function I get an error:

namezz(Primus)

Error in namezz(Primus) : 
  promise already under evaluation: recursive default argument reference or earlier problems?

Can't understand the error, and what I am doing wrong.. Thanks in advance for any guidelines:)

EDIT: If I instead write it like this:

namezz=function(thepatternx,data,Name){

  library(stringr)

  thepattern=as.character(quote(thepatternx))

  pattern <- thepattern
  strings <- data$Name  #####data$column is a character vector
  found=str_detect(strings, pattern)
  yez= rownames(data[which(found==TRUE),])
  hhh=as.numeric(yez)+1
  return(hhh)

}

I get:

namezz(Primus,data,Name)

numeric(0)

Which is not correct, because if I do the procedure without putting it in a function I get the rows:

pattern="Primus"
strings <- data$Name
mja=str_detect(strings, pattern)
yez= rownames(data[which(mja==TRUE),])
hhh=as.numeric(yez)+1

    [1] 2 3 4 5 6 7 8 9

Here is a dput:

dput(head(data))
structure(list(Year = 1901:1906, Name = c(">>Primus<< sbk", ">>Primus<< sbk", 
">>Primus<< sbk", ">>Primus<< sbk", ">>Primus<< sbk", ">>Primus<< sbk"
), Established = c(1899L, 1899L, 1899L, 1899L, 1899L, 1899L), 
    Bolagskod = c(2L, 2L, 2L, 2L, 2L, 2L), Kategori = c(2L, 0L, 
    0L, 0L, 0L, 0L), BranschTillhörighet = c(2L, 2L, 2L, 2L, 
    2L, 2L), Startår = c(1901L, 1901L, 1901L, 1901L, 1901L, 1901L
    ), Stoppår = c(1908L, 1908L, 1908L, 1908L, 1908L, 1908L), 
    Ranges = c("8  1901 - 1908  >>Primus<< sbk", "8  1901 - 1908  >>Primus<< sbk", 
    "8  1901 - 1908  >>Primus<< sbk", "8  1901 - 1908  >>Primus<< sbk", 
    "8  1901 - 1908  >>Primus<< sbk", "8  1901 - 1908  >>Primus<< sbk"
    ), Years.present = c("1901  1902  1903  1904  1905  1906  1907  1908", 
    "1901  1902  1903  1904  1905  1906  1907  1908", "1901  1902  1903  1904  1905  1906  1907  1908", 
    "1901  1902  1903  1904  1905  1906  1907  1908", "1901  1902  1903  1904  1905  1906  1907  1908", 
    "1901  1902  1903  1904  1905  1906  1907  1908"), Delägare.män. = c(267L, 
    271L, 317L, 339L, 339L, 345L), Delägare.kvinnor. = c(246L, 
    251L, 236L, 244L, 260L, NA), Sjukdomsfall.män. = c(66L, 61L, 
    100L, 103L, 106L, 82L), Sjukdomsfall.kvinnor. = c(59L, 55L, 
    60L, 71L, 85L, 60L), Sjukdagar.män. = c(1686L, 1918L, 2149L, 
    2212L, 2331L, 1890L), Sjukdagar.kvinnor. = c(1681L, 1197L, 
    1589L, 1904L, 2282L, 1750L), Inkomster.InträdesAvgifter. = c(303L, 
    NA, NA, NA, NA, NA), Inkomster.RegelbundnaAvgifter. = c(4901L, 
    4939L, 5172L, 5687L, 5728L, 5879L), Inkomster.UtdebiteradeAvgifter. = c(1313L, 
    1045L, 1141L, 2024L, 1462L, 1934L), Inkomster.Böter. = c(241L, 
    NA, NA, NA, NA, NA), SummaMedl.avg. = c(6758L, 5984L, 6313L, 
    7711L, 7190L, 7813L), Inkomster.BidragStatKommun. = c(366L, 
    440L, 456L, 464L, 476L, 493L), Inkomster.Räntor. = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), Inkomster.Övrigt. = c(24L, 722L, 874L, 605L, 805L, 647L
    ), Inkomster.Summa. = c(7148L, 7146L, 7644L, 8781L, 8472L, 
    8954L), DiffIntäkter.SummaMotVerkligSumma. = c(0L, 0L, -1L, 
    -1L, -1L, -1L), Utgifter.Sjukhjälp. = c(4735L, 4450L, 5300L, 
    5870L, 6560L, 5200L), Utgifter.Begravningshjälp. = c(1200L, 
    795L, 1045L, 1810L, 955L, 1675L), Utgifter.Arvoden. = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), Utgifter.Förvaltning. = c(956L, 972L, 1038L, 1156L, 1523L, 
    1171L), Utgifter.Övrigt. = c(25L, NA, 20L, 5L, NA, NA), Utgifter.Behållning. = c(231, 
    929, 240, -59, -565, 908), Utgifter.SummaÖvrigt.Behållning. = c(256L, 
    929L, 260L, -54L, -565L, 908L), Utgifter.Summa. = c(7148L, 
    6217L, 7403L, 8841L, 9038L, 8046L), KOLL = c(-1L, 0L, 0L, 
    0L, 0L, 0L), Tillgångar.KontantIKassa. = c(835L, 1765L, 2006L, 
    1946L, 1380L, 2259L), Tillgångar.KontantMedelBank. = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), Tillgångar.Totalt. = c(836L, 1765L, 2006L, 1946L, 1468L, 
    2348L), Skulder.Totalt. = c(NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_), TillgångarÖverSkulder = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    )), .Names = c("Year", "Name", "Established", "Bolagskod", 
"Kategori", "BranschTillhörighet", "Startår", "Stoppår", "Ranges", 
"Years.present", "Delägare.män.", "Delägare.kvinnor.", "Sjukdomsfall.män.", 
"Sjukdomsfall.kvinnor.", "Sjukdagar.män.", "Sjukdagar.kvinnor.", 
"Inkomster.InträdesAvgifter.", "Inkomster.RegelbundnaAvgifter.", 
"Inkomster.UtdebiteradeAvgifter.", "Inkomster.Böter.", "SummaMedl.avg.", 
"Inkomster.BidragStatKommun.", "Inkomster.Räntor.", "Inkomster.Övrigt.", 
"Inkomster.Summa.", "DiffIntäkter.SummaMotVerkligSumma.", "Utgifter.Sjukhjälp.", 
"Utgifter.Begravningshjälp.", "Utgifter.Arvoden.", "Utgifter.Förvaltning.", 
"Utgifter.Övrigt.", "Utgifter.Behållning.", "Utgifter.SummaÖvrigt.Behållning.", 
"Utgifter.Summa.", "KOLL", "Tillgångar.KontantIKassa.", "Tillgångar.KontantMedelBank.", 
"Tillgångar.Totalt.", "Skulder.Totalt.", "TillgångarÖverSkulder"
), row.names = c(NA, 6L), class = "data.frame")

Edit

This works:

namezz=function(thepatternx,data,Name){

  library(stringr)

  thepattern=thepatternx

  pattern <- thepattern
  strings <- data$Name
  mja=str_detect(strings, pattern)
  yez= rownames(data[which(mja==TRUE),])
  hhh=as.numeric(yez)+1
  return(hhh)  

} 

namezz("Primus",data,Name)
[1] 2 3 4 5 6 7 8 9

But how can i pass Primus without the quotation marks, namezz(Primus,data,Name)?. Thinking about something as in my question, but as.character(quote()) does not work..

Someone can correct me on this, but I think that you are passing Primus and Name as objects to the function and it is looking in the .GlobalEnv for those objects and is not finding them, therefore your function is failing to carry out most of your instructions (and is returning nothing). I have edited your function a bit.

Instead try this...

 namezz <- function( pattern = " ", data , column= "Name" ){
   library(stringr)
   strings <- data[ , column ] ##data$column is a character vector
   found = str_detect( strings , pattern )
   yez = rownames( data[ which( found==TRUE ) , ] )
   hhh = as.numeric( yez ) + 1
   return( hhh )
 }

Then you must use the function like so:

namezz( "Primus" , data = data ) #In this case the default for column is "Name" as you want

The problem with passing data = data is explained very nicely here. An excerpt from that post (where they refer to testparams you would refer to data)...

"One of the most important things to know about the evaluation of arguments to a function is that supplied arguments and default arguments are treated differently. The supplied arguments to a function are evaluated in the evaluation frame of the calling function. The default arguments to a function are evaluated in the evaluation frame of the function."

the parameter testparams, when no matching argument is passed, is given the default value which is the value of the variable testparams looked-up not in the environment where foo is defined, and not in the environment where foo is called, but rather in the local environment created when the function is called and where parameters are mapped to values -- and in this environment, testparams is a parameter, which is already being under evaluation, hence the recursive lookup error.