r :Error: could not find function -User defined - r

I am trying to run the below simple function in R studio
a<-testfunc15(state,outcome)
{
st<-state
out<-outcome
print(st)
tempdff<-healthcareoutcome[healthcareoutcome$State==st,]
tempdff
}
When i copy and paste of the R prompt of the R studio ,i get an error :
Error: could not find function "testfunc15"
> {
+
+ st<-state
+ out<-outcome
+ print(st)
+ tempdff<-healthcareoutcome[healthcareoutcome$State==st,]
+ tempdff
+
+ }
Error: object 'state' not found
when i try to source it :source("testfunc15.R")
then i get this error :
Error in eval(expr, envir, enclos) : could not find function"testfunc15"
I am saving the file in the same getwd() as other functions ,other functions are working fine.
Where am i going wrong ?
I couldn't find an answer on stackoverflow though there were many questions with the same description.
Please help

The first line of your code is telling R to store the result of testfunc15(state, outcome) in a. It is not defining the function.
Also your function as written here doesn't exist. It should be, as mentionned by Dason, either :
testfunc15 = function (state, outcome) or testfunc15 <- function (state, outcome)
And the end could be return(tempdiff) or tempdiff

Related

Use of clusterProfiler : Error in testForValidKeytype(x, keytype)

I don't know if you have already encountered this kind of problem with clusterProfiler, but it seems that it doesn't recognize the keytype I indicate.
Having GIDs, indicating keyType="GID" as an argument, I get this error:
> GOenrich <- enrichGO(gene=GenesVector,
+ OrgDb="org.Cgigas.eg.db",
+ keyType="GID",
+ ont="BP",
+ pAdjustMethod="BH",
+ qvalueCutoff="0.05",
+ universe=GenesBackground,
+ readable=FALSE,
+ pool=FALSE)
Error in testForValidKeytype(x, keytype) :
Invalid keytype: GOALL. Please use the keytypes method to see a listing of valid arguments.
When I display the list of possible arguments, I get the following result:
> keytypes(org.Cgigas.eg.db)
[1] "EVIDENCE" "GID" "GO"
However, the same error is displayed for keyType="GO" or keyType="EVIDENCE", even though these arguments are not relevant to my study.
If you are using clusterProfiler and have any advice, I am listening.
For precision: I generated my Org.db using the makeOrgPackage() function of AnnotationForge.

Error in if (more || nchar(output) > 80) when using mgcv

I have the same problem with this one but no solutions found yet
.
Error in if (more || nchar(output) > 80) { : missing value where TRUE/FALSE needed
I am conducting analysis using the mgcv package.
model1<-gam(fm_xsetz~total_pm2.5, data=analysis)
I can get the results by using the summary(). But when I try to open the model in the global environment, I get the warning:
Error in if (more || nchar(output) > 80) { : missing value where
TRUE/FALSE needed
Is anyone has the same problem?
FYI,when you use the following code:
library(geostatsp)
data(swissRain)
same problem happens!
I have/had a similar problem when I tried to view a List generated trough a function that computes the intersect/difference of two sets of 23000 observations each.
The function in question:
jeepers.creepers<-function(dfx,dfy,by.x,by.y){
SetX<-dfx[[by.x]]
SetY<-dfy[[by.y]]
Union.X.Y<-intersect(SetX,SetY)
Difference.in.X<-setdiff(SetX,Union.X.Y)
Difference.in.Y<-setdiff(SetY,Union.X.Y)
result<-list(Union.X.Y,Difference.in.X,Difference.in.Y)
names(result)<-c("Union of SetX and SetY",
"Unique in SetX",
"Unique in SetY")
return(result)
}
It gave me this error:
Error in if (more || nchar(output) > 80) { :
missing value where TRUE/FALSE needed
Nevertheless I could view the elements individually with
View(list$element)
I had a similar problem yet I could view it with:
view(as.data.frame(df))

How to create a loop function that will create data frames by using different sql queries

I try to create a loop that will create data frames by using different sql queries that have the same name exept the day number.For example here is the name for query that is for day 1 :SF_2013_Day1_BaseLine and this is for day 6: SF_2013_Day6_BaseLine. I wrote this code (below) but I got an error : Error: unexpected ',' in "for(i in 3," .So any Idea how can i get this code to work ?
Thank you
for (i in 1,3,6,10,14,21,30) {
SF_FinalviewQ3_2013_Day[i]_BaseLine<-sqlQuery(DB,"select * from [SF_2013_Day[i]_BaseLine]");
dim(SF_Day[i]_BaseLine)}
After a change based on #Pgibas edvice to this code :
for (i in c(1,3,6,10,14,21,30)) {
SF_FinalviewQ3_2013_Day[i]_BaseLine<-sqlQuery(DB,"select * from [SF_2013_Day[i]_BaseLine]");
dim(SF_Day[i]_BaseLine)}
I got this error: Error: unexpected input in "for(i in c(3,6)){glm_d[i]_" . What can I do to resolve the problem?
You need to resolve i within the names first:
for (i in c(1,3,6,10,14,21,30)) {
set <- sqlQuery(DB, paste0("select * from [SF_2013_Day[", i, "]_BaseLine]"))
eval(parse(text = paste0("SF_FinalviewQ3_2013_Day", i, "_BaseLine <- set"))
dim(set)
}

Error in eval(expr, envir, enclos) when using rmongodbHelper

I'm working with rmongodb and rmongodbHelper packages and i've built this function.
CUPS_CP_TAR36 <- function(codi,cant){
cups <- vector()
query <- json_to_bson('{"clLst.U_COD_POSTAL": codi, "clLst.TARIFA_ATR": {"$in": "3.0A","3.1A","6.1"]}}')
output <- json_to_bson('{"id":1}')
cursor <- mongo.find(mongo, sips, query, fields=output, limit=cant)
k = 0
while(mongo.cursor.next(cursor)){
k = k + 1
cups[k] <- mongo.bson.value(mongo.cursor.value(cursor), "_id")
}
return(cups)
}
but when I try to use it:
example <- CUPS_CP_TAR36(codi="08036", cant=10)
I get the following error and really dont know why, i'm not used to write my own functions:
Error in eval(expr, envir, enclos) : object 'codi' not found
Thanks!
Try latest rmongodb version from github. bson from json works out of the box. Moreover mongo.bson.to.list works fine for every case, so you dont't need to construct bson from json. I have plans to push new version of the package to CRAN next week.
library(devtools)
install_github("mongosoup/rmongodb")
query <- json_to_bson('{"clLst.U_COD_POSTAL": codi, ...}')
json_to_bson() expects a JSON string. The string you provide is not valid JSON, just as {"key":val} is not valid - while {"key":"val"} or {"key":3.14} are valid.
Of course codi isn't even intended by you to be a string in your case but to be a variable. But as you write the code there is no way for R to know about that.
So you might write:
query <- json_to_bson(sprintf('{"clLst.U_COD_POSTAL": "%s", ...}',codi))
The equivalent expression with your value of codi is then:
query <- json_to_bson('{"clLst.U_COD_POSTAL": "08036", ...}')
The reason why there is an error message involving eval() is b/c I wrote the interpretation of the JSON very, very simple - mind this "package" is merely a workaround and hopefully superfluous very soon.
The JSON is turned into a list()-expression using string-substitution and then eval()ed.
rmongodbHelper / R / json_to_list.R:
json_to_list <- function(json) {
json <- gsub("\n","",json)
json <- gsub("\\{\\}","list()",json)
[...]
json <- gsub("\\$","_$",json)
return(eval(parse(text = json)))
}
Further details: MongoDB – State of the R

Which library is the pr_DB object defined in?

I am completely new to R.
I am trying to use the dist object with a custom function based on the specification here, but I was unable to pass the custom function directly by name, so I tried to add it using the registry described here, but it appears that I am missing a library.
However, I'm not sure which library I need and cannot find a reference to find the name of the library.
Here's a code sample that I'm trying to run:
library(cluster)
myfun <- function(x,y) {
numDiffs <- 0;
for (i in x) {
if (x[i] != y[i])
numDiffs <- numDiffs + 1;
}
return(numDiffs);
}
summary(pr_DB)
pr_DB$set_entry(FUN = myfun, names = c("myfun", "vectorham"))
pr_DB$get_entry("MYFUN")
Here's the error:
Error in summary(pr_DB) : object 'pr_DB' not found
Execution halted
You need to learn the conventions used by R help pages. That "{proxy}" at the top of the page you linked to is really the answer to your question. The convention for the help page construction is "topic {package_name}".

Resources