I used lapply to run a function 100 times with below argument:
lapply(1:100, myfunc)
but I got this error message:
error in match.fun(FUN) : object 'myfunc' not found
I used list.files() to check that if function is in working directory and it was there. So I don't know why I got this message.
This sounds vaguely like you're expecting R to behave like Matlab (based on my hazy memories of Matlab).
Just because a file containing a function is in your working directory does not mean anything about whether R knows about it. You need to source() the file containing the function, and then verify that the function exists in your workspace using ls().
So for example, if the file containing the function is in "foo/bar/myfile.R" then I'd run:
source("foo/bar/myfile.R")
and the verify that the function is in my workspace by running ls().
Related
During a process, I need to copy a directory using fs::dir_copy(currentPath, newPath). Some of the time, I get the following error message:
Error in link_copy(links, path(new_path[[i]], path_rel(links, path[[i]])), :
all(is_link(path)) is not TRUE
This only happens some of the time, not always. What's more, if I replace my directory with a manual copy of itself (i.e. manually copy directory, delete original, rename the copy), then my code will work.
Could someone please explain why this could be happening? Is there a way I can sidestep that error once and for all?
This does not answer your question but may help you to further analyse what is going on. One of the great things of R is that you can easily inspect source code. Especially in a situation where unusual things appear to happen, looking at the code may be useful.
In your case, we can inspect the source code of fs::dir_copy and trace back to which code generates the error message. If you type fs::dir_copy (no parenthesis) into the console, R will print the R code of the function (if the function is not primitive). Doing that here will show you that the fs:dir_copy function calls the fs::link_copyfunction. Makes sense, as the error message comes from this function.
We can now print this function with fs::link_copy
This function generates the error message in this line:
stopifnot(all(is_link(path)))
From the error message we know that all(is_link(path)) returns FALSE. Next step is to have a look at the fs::is_link function. Here we see that the error may come from the call to the setNames function, which depends on the fs::file_info function: res <- file_info(path)
Here we see that setNames is called with a condition depending on what the file_infofunction returned:
setNames(!is.na(res$type) & res$type == "symlink", res$path)
This looks unusual as setNames takes an object and a character vector as formals. But then, I am not the developer of these functions ;-)
Perhaps there are issues with the file type on your machine and fs::file_info(path) returns something unexpected under some conditions.
I know how much you love the here() package... I'm just starting to catching on.
I am trying to write an (relatively) automated code for cleaning data and I would like to use here() and parse(). But parse() doesn't seem to like having here() within it.
cleaning <- parse(file = here("folder", "source-code.R"))
and I get the error:
Error in here("folder", "source-code.R"):
unused argument ("source-code.R")
If I set my working directory as the folder containing "source-code.R" and remove the here() argument, the process works just fine.
also, I've tried removing the "folder" and just calling the "source-code.R", but then I get the error:
Error in parse(file = here("source-code.R")) :
'file' must be a character string or connection
thanks for your help!
Thanks to MrFlick who recognized the naming conflict. Using here::here() solved it!
I have four lists and a very long self-written function. length(list) is over 40.000.
An error occurs when running the function over the lists. I know how to repair the function, but I need to know at which position of the list list[[x]]==? the error occured, so I know which parameters went into the function.
Is there a call to find that out?
You can use recover in the options, i.e. run the following:
options(error = recover)
just before running your function over the list.
This will open the debugger just when the error occurs. Therefore, you can access the environment in that exact iteration (and get access to all objects / arguments) and see what went wrong.
I have wrote a bunch of functions for my project for users to invoke interactively.
Now I'd like to have them safely included in the session.
I want to avoid a scenario where user types rm(list=ls()) and erases my functions from memory.
Initially I tried to save function to another environment and attach that environment to search path but along the way I changed something and R is not longer able to find my function.
My code is split among multiple files but snippet below ilustrates how are things organised, normally user will run app.R because it contains refernces to other files, configurations etc.:
./funs.R
id.mapping.env <- new.env(parent = emptyenv())
attach(id.mapping.env)
id.mapping.env$test_function<- function() {
print("It works")
}
./app.R
source("./funs.R")
test_function
If I run app.R I get error:
Error: could not find function "test_function"
Why won't R find my function? Do I have to resort to writing my own package to ensure all of my functions can be found?
I am not familiar with writing packages, if this is needed could you give me a tiny demo/tutorial(which I am googling right now).
I am currently checking my package. R CMD check gives me the following warning:
* checking for missing documentation entries ... WARNING Undocumented code objects:
followed by a list of functions.
The problem seems to be that I have 3 lists containing functions. These, however, are just small functions that exist only because I tried to modularise my code as far as possible. I would like (and have seen that previously in other packages) to just give the function list + documentation without having to provide a documentation for every single tiny function bit.
Is there a way to do this?
The mistake was that within the function list I had
funclist <- list(function1 <- function1(){})
This lead to function1 being sourced upon sourcing funclist. However, when one writes
funclist <- list(function1 = function1(){})
function1 is not sourced and can be addressed via funclist$function1() then.