How can I define any input file to use in mendelian randomisation? - runtime-error

I have datasets from pgc and gwas, and I need to perform Mendelian Randomization over them but everytime I get an error. although i'm defining everything but it doesn't seem to work. As you can see in this image, no matter what type of file it is, i get the same errors

Related

R studio referencing old code, unused variable names, and other odd errors

Suddenly, R Studio has been giving me hard-to-reproduce errors in which it references old variable names and code that no longer exist in my script. Here is a short replicable example:
LoadInfectionsDrugs <- function() {
}
meta <- read.csv("tblhctmeta.csv")
infections <- read.csv("tblInfectionsCidPapers.csv")
drugs <- read.csv("tbldrug.csv")
workspace <- "w4v3.RData"
load(workspace)
print(head(input))
print(head(meta))
print(head(drugs))
print(head(infections))
LoadInfectionsDrugs() #This line generates the error - loads and prints correctly if this is commented out.
If I comment out the last line, it runs and prints just fine. If I leave the last line in, though, I get the error "Error in unique(input$PatientID): argument "input" is missing, with no default.
Thing is, the LoadInfectionsDrugs function used to (and normally does) have several arguments, including "input", which is a data frame with a column called PatientID. So this clearly seems to be referencing old code.
If I comment out the "load(workspace)" and "print(head(input))" lines, it also runs fine.
So my hypothesis is that, in my main code, when I call "save.image(workspace)", it is saving not only objects, such as the dataframes I'm creating, but also functions. Then, when I change the function in code, but load the workspace, it is replacing the functions I've defined in code with the definitions as they were when I last saved the workspace.
I didn't know that .RData files saved function definitions. I thought they just saved variables. Is there a way to save only the objects you've defined (i.e. dataframes), and not the functions?
https://www.oreilly.com/library/view/r-in-a/9781449377502/ch12s02.html
Today I discovered the difference between saving objects and saving my entire workspace. If you save the workspace, you save everything, including function definitions and such. If you change the code and then load the workspace, R will start using your old functions, not the ones you've got in your most recent version of the script.

Variables used in Test Package in R Studio

I am trying to fix an issue in an R project (that I'm not too familiar with). The test script that is executed when running "Test Package" in R-Studio uses a variable, let's call it x. From the result of the test I can tell that the data assigned to this variable is outdated and I want to update it.
Problem is, I just cannot figure out where this variable is actually defined. I have used grep for the entire source code of the package, and I only find the instance from the test script but no declaration. It is not defined in the script itself nor anywhere else in the source code.
When I just load the package, the variable is not defined. Somehow it is defined however when running the test, because only when I change the name in the test script into some dummy I get the error that it isn't defined.
Is there a specific place where I could look, or may be a simple trick how I could figure out where and how the variable is defined?
Thanks
Edit: the actual variable name is a bit complicated, it is not x
The find in files option in RStudio may help.
You can search through multiple files within a folder.
If you get too many matches to sort through (I'm really hoping your variable is not actually called x!), you can try using a regular expression.
enter image description here
Follow the pictures and you could solve the problem.

Existing object not found

I call a matrix in my code and R tells me that the object was not found, although it clearly exists.
Of course I checked for spelling errors and the typical issues. However, when I run parts of the line, where the error occurs, it finds the object perfectly and prints it as requested. Requesting the object itself does not produce the error either, only running that line of code.
print(object) also works before and after that line.
The line with the issue is:
a[h,k]<<-switch(as.character(a[h,k]-1),"0"=-1,a[h,k]-1)
a is the problematic matrix. It is used within a function, but even when I run the line independent of the function, it doesn't work either.
a[h,k] and switch(as.character(a[h,k]-1),"0"=-1,a[h,k]-1) properly work by themselves.

Formerly functional function suddenly doesn't work without code difference, throws object not found error from passed argument

I have a very odd problem. I designed a function a while back that has arguments defined as follows:
elisa<-function(
file="data.csv",
Out=paste("Elisa Analysis",Sys.time(),".xlsx"),
wd="~/Desktop",
out.folder=wd,
Rep=2,
standards=c(1,2),
orient="horizontal")
{
As you can see above, wd is defaulted to the value of the desktop, and out.folder is defaulted to the value of wd. This is so that users can specify where an output excel document goes separately from where the working directory is, but the default is that they are the same. What is weird is that I have used this function for a while, and I have several users who use this exact code (I installed it on their CPUs, and so I know that it is the exact same), and it works fine for them, but suddenly on my computer it is not working. It gives the following error when I run the function.
Error in setwd(out.folder) : object 'wd' not found
This is odd for a number of reasons. The first is that if I remove the functional enclosure and run the exact same code "naked," the code works just fine. The object wd is definitely still present by the time it gets to the line of code being referenced, which is simply just
setwd(out.folder)
Moreover, out.folder has the same definition as wd. What is even weirder is that if you read the error code, it doesn't say that out.folder is not defined or the object isn't found. It says that the other object, wd, is not found, which is odd because out.folder should have just been set to the value
"~/Desktop"
I am at a loss. A line of code early in the function uses wd just fine, but the error occurs hundreds of lines farther down at the line of code specified. If I remove it, everything functions fine, and if I run the function without the functional enclosure, everything functions fine.
Anybody have any clue what is going on?
Sample (only lines involved since the full code is so large it won't let me post it here)
elisa<-function(
file="data.csv",
Out=paste("Elisa Analysis",Sys.time(),".xlsx"),
wd="~/Desktop",
out.folder=wd,
Rep=2,
standards=c(1,2),
orient="horizontal")
{
setwd(wd)
setwd(out.folder)
setwd(wd)
}
I have found a way around it. This is really interesting. If you state the argument explicitly the same way as the default argument, the function won't work. In other words,
elisa(...,out.folder=wd)
will throw an error even though it is the default argument format. If you omit it when doing the function call, the function works, or if you give it the same value as wd without going through wd as an intermediary, it works. In other words,
elisa(...,wd="~/Desktop",out.folder="~/Desktop",...)
and
elisa(...,wd="~/Desktop",...)
both work. You just can't do
elisa(...,wd="~/Desktop",out.folder=wd,...)
even though the definition of the function arguments is
elisa<-function(
file="data.csv",
Out=paste("Elisa Analysis",Sys.time(),".xlsx"),
wd="~/Desktop",
out.folder=wd,
Rep=2,
standards=c(1,2),
orient="horizontal")
It makes no sense, but that's the way around it.

Automatic Plotting in *.r file

I do have a *.r file where I order it to coduct a Chi Square of Independence and write it to an html file. It's working fine but I'd like to add a graph.
Doing by hand in R with linecommands works perfectly, but the exact same commands do not work in the *.r file but i want it to do it automatically.
mat1 <-matrix(c(12,3,2,12),nrow=2,byrow=T)
attach(mat1)
png('independence.png')
barplot(mat1,beside=TRUE)
dev.off()
Is there an additional command necessary?
kind regards
If you have an error in a script with no try or tryCatch, then entrie script fails. By trying to attach a matrix, you throw an error with the message:
Error in attach(mat1) :
'attach' only works for lists, data frames and environments
So you should pay more attention to the error messages in interactive mode, and if you are planning to use .r files for production you should learn to use error handling routines in R. The `attach function is a common source of new user errors, although this error is not particularly common. The more common errors with its use involve regression functions where the authors of the functions are expecting entire objects, usually dataframes, to be passed to a data argument.

Resources