[英]C# R.NET Error : unexpected '}' Error
I'm using R.NET 1.6 on my program. 我在程序上使用R.NET 1.6。
I got error message "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" when I tried to run my whole script using source('')
so I wrote the entire code manually instead of calling the script because I read somewhere that it's one of R.NET bug. 当我尝试使用
source('')
运行整个脚本时,出现错误消息“试图读取或写入受保护的内存。这通常表明其他内存已损坏” ,因此我手动编写了整个代码,而不是调用脚本因为我在某处读到它是R.NET错误之一。 Here's the code : 这是代码:
engine.Evaluate("U=matrix(ncol=c,nrow=nrow(matdt))");
engine.Evaluate("for(i in 1:nrow(matdt)){for(j in 1:c){U[i,j]=runif(1)}}");
engine.Evaluate("totU=matrix(rowSums(U),ncol=1,nrow=nrow(U))");
engine.Evaluate("for(a in 1:nrow(U)){for(b in 1:ncol(U)){U[a,b]=U[a,b]/totU[a,]}}");
engine.Evaluate("P=0");
engine.Evaluate("iter=1");
engine.Evaluate("repeat{");
engine.Evaluate("V=matrix(ncol=ncol(matdt),nrow=c)");
engine.Evaluate("d=matrix(ncol=ncol(matdt),nrow=nrow(matdt))");
engine.Evaluate("Uk=matrix(ncol=ncol(U),nrow=nrow(U))");
engine.Evaluate("for(g in 1:nrow(U)){for(h in 1:ncol(U)){Uk[g,h]=(U[g,h])^(m)}}"); engine.Evaluate("Uks=matrix(colSums(Uk),ncol=ncol(Uk),nrow=1)");
engine.Evaluate("for(k in 1:c){for(j in 1:nrow(matdt)){d[j,]=Uk[j,k]*matdt[j,]}");
engine.Evaluate("V[k,]=colSums(d)/Uks[,k]}");
engine.Evaluate("dist=matrix(ncol=1,nrow=nrow(matdt))");
engine.Evaluate("alldist=matrix(ncol=c,nrow=nrow(matdt))");
engine.Evaluate("for(l in 1:c){for(n in 1:nrow(matdt)){dist[n,]=t(matdt[n,]-V[l,])%*%(matdt[n,]-V[l,])}");
engine.Evaluate("alldist[,l]=dist}");
engine.Evaluate("ud=matrix(ncol=c,nrow=nrow(matdt))");
engine.Evaluate("for(o in 1:nrow(matdt)){for(p in 1:c){ud[o,p]=alldist[o,p]*Uk[o,p]}}");
engine.Evaluate("Pi=sum(rowSums(ud))");
engine.Evaluate("P=abs(Pi-P)");
engine.Evaluate("alldist2=matrix(ncol=ncol(alldist),nrow=nrow(alldist))");
engine.Evaluate("for(q in 1:nrow(alldist)){for(r in 1:ncol(alldist)){alldist2[q,r]=(alldist[q,r])^((-1)/(m-1))}}");
engine.Evaluate("alldist2[alldist2==Inf] <- 0 ");
engine.Evaluate("totdist=matrix(rowSums(alldist2),ncol=1,nrow=nrow(alldist2))");
engine.Evaluate("for(s in 1:nrow(U)){for(t in 1:ncol(U)){U[s,t]=alldist2[s,t]/totdist[s,]}}");
engine.Evaluate("nilnol<-which(U==0,arr.in=TRUE)");
engine.Evaluate("if(nrow(nilnol)>0){nilnol=matrix(nilnol,ncol=ncol(nilnol))");
engine.Evaluate("for(nn in 1:nrow(nilnol)){"); engine.Evaluate("U[nilnol[nn,1],]=0"); engine.Evaluate("U[nilnol[nn,1],nilnol[nn,2]]=1}}");
engine.Evaluate("iter=iter+1");
engine.Evaluate("if(P<e | iter>mit){break}}");
I got error message at engine.Evaluate("V[k,]=colSums(d)/Uks[,k]}");
我在
engine.Evaluate("V[k,]=colSums(d)/Uks[,k]}");
收到错误消息engine.Evaluate("V[k,]=colSums(d)/Uks[,k]}");
saying unexpected '}' . 说出意外的'}' 。 It's the part of the loop :
这是循环的一部分:
engine.Evaluate("for(k in 1:c){for(j in 1:nrow(matdt)){d[j,]=Uk[j,k]*matdt[j,]}");
engine.Evaluate("V[k,]=colSums(d)/Uks[,k]}");
The code works fine when I tried to run it at R. Also if someone has solution for the bug problem it would be greatly appreciated. 当我尝试在R上运行该代码时,它工作正常。另外,如果有人对错误问题有解决方案,将不胜感激。
Rather than numerous calls to engine.Evaluate()
, you can construct longer strings of R commands and feed them into fewer calls. 您可以构造更长的R命令字符串并将其馈入到更少的调用中,而不是对
engine.Evaluate()
多次调用。 However, you'll need strategically placed semicolons; 但是,您将需要战略性地放置分号。 anywhere you would need a line break in regular R code you now need a semicolon.
在常规R代码中需要换行的任何地方,现在都需要使用分号。
In your case, you'll need a semicolon after the closing bracket of the for
loop. 在您的情况下,您需要在
for
循环的右括号后加上分号。
string cmd =
"for (k in 1:c) {" +
"for (j in 1:nrow(matdt)) {" +
"d[j, ] = Uk[j, k]*matdt[j, ]" +
"};" + /* <---- Note the semicolon */
"V[k, ] = colSums(d) / Uks[, k]" +
"};"; /* <---- Another just to be safe */
engine.Evaluate(cmd);
Since there are no embedded line breaks in the input string, the code that R is actually seeing is this: 由于输入字符串中没有嵌入的换行符,因此R实际看到的代码是这样的:
for (k in 1:c) {for (j in 1:nrow(matdt)) {d[j, ] = Uk[j, k]*matdt[j, ]};V[k, ] = colSums(d) / Uks[, k]};
An easier solution that may also work is to pass a C# string literal that contains line breaks. 一个更简单的解决方案(也可以使用)是传递包含换行符的C#字符串文字。
string cmd = @"for (k in 1:c) {
for (j in 1:nrow(matdt)) {
d[j, ] = Uk[k, j]*matdt[j, ]
}
V[k, ] = colSums(d) / Uks[, k]
}";
The line breaks are now embedded in the string, so when it's passed to engine.Evaluate()
, R will see the line breaks as well, rendering semicolons unnecessary. 现在,换行符已嵌入到字符串中,因此当将其传递给
engine.Evaluate()
,R也将看到换行符,从而无需使用分号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.