I`m a BSc student just getting into R. I've done a bunch of things already (mainly for Psych statistics, plotting and other basic stuff). Yet when I try to knit a script I very often get error Messages. At first it was the same two errors repeatedly;
"trying to use CRAN without setting a mirror"
&
"cannot open the connection Calls: ... withCallingHandlders -> withVIsible -> eval -> eval -> read.table -> file".
Both of which I finally managed to track down (taken from here: Can't resolve error in .Rmd file <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval ->
&
Set default CRAN mirror permanent in R)
And now that I'm finally over the technical issues, I keep getting bogged down in fundamental errors. Like RStudio not recognising basic commands like gather() and %>%, even though I have set the work directory, installed all the packages and put them in the workspace library. And when that didn't work, I copy/pasted someone else's, with the exact same codes that work and it still won't let me knit it to .hmtl.
I can't even check my basic work on any level and I feel like there must be some fundamental issue I'm missing. I've spent several hours a day troubleshooting introductory assignments (and even downloaded it on a different computer) and I keep getting these error messages every time I write any form of code.
Edit:
[Data/HW6.txt] is just a generic table from the teacher for homework 6.
dat_w <- read.table(file="data/HW6.txt", header=TRUE, sep=",")
dat_l <- dat_w %>%
gather(adi:kft, key=Test, value=Score) %>%
arrange(Gruppe, Subj)
To which I put the packages and the commands
library(tidyr)
library(dplyr)
But when I go to knit that, the problem I get is still:
Error in dat_w %>% gather(adi:kft, key = Test, value = Score) %>% arrange(Gruppe, :
could not find function "%>%"
Edit 2: search() results are:
[1] ".GlobalEnv" "package:dplyr" "package:tidyr" "tools:rstudio"
[5] "package:stats" "package:graphics" "package:grDevices" "package:utils"
[9] "package:datasets" "package:methods" "Autoloads" "package:base"
Related
Hello I am using R markdown and have ran into trouble.
When I try to knit this section of my document (the "example" variable is a data frame with a column called "text" which contains strings):
library(qdap)
frequent_terms <- freq_terms(example$text, 4)
frequent_terms
I get this error message:
Error in split_chain(match.callQ), env = env) :
could not find function "split_chain"
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> %>%
I have updated Rstudio and the relevant packages(such as magrittr) but I am still running into this issue, one I have never encountered.
How do I fix this error, or how to I interpret the error message, I am at a loss here. Thank you.
According to this issue, your problem should be solved if you add qdap first, and magrittr / tidyverse afterward because gdap uses an old version of the pipe and masks the most recent magrittr version.
However, it is not entirely clear since we do not have a complete reproducible example
I would like to load a library function within a script evaluated in a specified environment.
Example:
## foo.R
## -----
## blah blah
library(extrafont)
loadfonts()
Assuming for convenience the evaluation environment is the base environment:
sys.source("foo.R")
## Registering fonts with R
## Error in eval(expr, envir, enclos) : could not find function "loadfonts"
Replacing loadfonts() with extrafont:::loadfonts() works better, but still gives:
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'pdfFonts' of mode 'function' was not found
because loadfonts() requires pdfFonts() defined in grDevices.
This is both a not totally satisfactory answer and a long comment to #waterling.
The proposed solution is:
e<- new.env()
source("foo.R", local=e)
i.e.
source("foo.R", local=new.env())
which is substantially equivalent to:
sys.source("foo.R", envir=new.env())
It works for much the same reason why:
sys.source("foo.R", envir=as.environment("package:grDevices"))
As reported in the error (see question), the function not found, pdfFonts() is part of the package grDevices The sys.source above executes the script in the package:grDevices environment, hence the function is found. Instead by default sys.source(..., envir=baseenv()) and the base environment comes before grDevices, therefore pdfFonts() is not found.
A first problem is that I do not know in advance which functions will happen to be in my script.
In this case setting envir=new.env() is a more general approach. By default
new.env(parent=parent.frame()),
therefore it has the same parent of sys.source(), which is the global environment. So everything visible in the global environment is visible in the script with sys.source(..., envir=new.env()), that is every object created by the user and by the user loaded packages.
The problem here is that we are not insulating the script any more, which makes it less reproducible and stable. In fact, it depends on what is in R memory in the very moment we call sys.source.
To make the things more practical, it means foo.R might work just because we usually call it after bar.R.
A second problem is that this not an actual solution.
The question concerns how to run a script foo.R in an environment e and still access, when needed, to functions not belonging to e. Taking an e that (directly or through its parents) has access to these functions is actually a workaround, not a solution.
If this type of workaround is the only possible way to go, IMHO, the best is to make it dependent only on standard R packages.
At start, R shows:
search()
## [1] ".GlobalEnv" "package:stats" "package:graphics"
## [4] "package:grDevices" "package:utils" "package:datasets"
## [7] "package:methods" "Autoloads" "package:base"
that is eight official packages/environments.
New packages/environments, unless explicitly changing the default, go into the second slot and all those after the first one shift one position.
myEnv=new.env()
attach(myEnv)
search()
## [1] ".GlobalEnv" "myEnv" "package:stats"
## [4] "package:graphics" "package:grDevices" "package:utils"
## [7] "package:datasets" "package:methods" "Autoloads"
## [10] "package:base"
So we can take the last eight in the search path, which means taking the first of these eight inheriting the others. We need:
pos.to.env(length(search()) - 7)
## <environment: package:stats>
## attr(,"name")
## [1] "package:stats"
## attr(,"path")
## [1] "path/to//R/R-x.x.x/library/stats"
Therefore:
sys.source("foo.R", envir=new.env(parent=pos.to.env(length(search()) - 7)))
or one can take a standard R reference package, say stats, and its parents.
Therefore:
sys.source("foo.R", envir=new.env(parent=as.environment("package:stats")))
UPDATE
I found the
SOLUTION
As for the script:
#foo.R
#-----
library(extrafont)
f=function() loadfonts()
environment(f) = as.environment("package:extrafont")
f()
To execute in a new environment:
sys.source("foo.R", envir=new.env(parent=baseenv()))
f() now has access to all objects in the package extrafont and those loaded before it.
In sys.source() creating a new.env() with whatever parent is necessary to make environment() assignment work.
I am trying to import an excel sheet into r. I used the following code:
x <- loadWorkbook("x.xlsx")
b <- readWorksheet(x, sheet="b")
The first line works fine, however, running the second gives the following error:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘readWorksheet’ for signature ‘"jobjRef", "character"’
I have no missing values in that sheet.
For the purpose of reproducing, download trial.xlsx from https://github.com/ahmedfsalhin/1stpaper.
system info: Yosemite operating system.
It appears the "root cause" is that you should add code to specify both the function and the package it belongs to. Type XLConnect::loadWorkbook to select the one you want in this case. There's no 'confusion,' or random selection of duplicated function names in R. The choice depends on the load order of all loaded packages. Use search() to see the order in which packages are checked for the command you've entered.
E.g., at present I get
search()
[1] ".GlobalEnv" "package:caTools"
[3] "package:XLConnect" "package:XLConnectJars"
[5] "package:stats" "package:graphics"
[7] "package:datasets" "package:vecsets"
[9] "package:cgwtools" "package:grDevices"
[11] "package:utils" "package:methods"
[13] "Autoloads" "package:base"
You'll notice that anything in your environment (.GlobalEnv) is selected first, and that all loaded libraries override the base package, for example.
After lot of struggle found the solution to this. In R studio go to the packages and remove all the packages related to XLConnect and xlsx. Then instal only XLConnect packages by typing
install.packages("XLConnect", dependencies=TRUE)
After this the issue should not exist.
I had the same problem while testing out three different packages for loading .xlsx files into R (XLConnect, xlsx, gdata). The solution was to specify the namespace for loadWorkbook:
d3_wb = XLConnect::loadWorkbook("Megadataset_v3.xlsx")
d3 = readWorksheet(d3_wb, 1)
Then it works, no matter if xlsx is loaded/installed or not. The error is because there is also a function with the same name in xlsx and it is defaulting to using the wrong one, which depends on the order the packages were loaded.
Try this instead
x <- readWorksheetFromFile("x.xlsx") in XLConnenct package.
I am using code which depends on two packages that conflict. I would like to give one priority for only a short period of time and my plan is to just move it up to the front of search(). However, I can't just unload and reload. I tried that and it causes other problems, and running library on an already loaded package does not work.
Here is an example (the real use case involves non-CRAN packages):
library(ggplot2)
library(MASS)
> search()
[1] ".GlobalEnv" "package:MASS" "package:ggplot2"
[4] "package:stats" "package:graphics" "package:grDevices"
[7] "package:utils" "package:datasets" "package:methods"
[10] "Autoloads" "package:base"
How can I now move package:ggplot2 ahead of package:MASS without detaching/unloading ggplot2?
EDIT
Inside the function I need to call, say function1, there is an expression that makes further calls. I cannot edit those calls to append ::.
e.g.
unchangeable <- function1("abc") ~ function2("def")
Suppose mainFun is the one I want to call. I can do
mainFun(unchangeable)
but I cannot specify
mainFun::unchangeable
It is indeed possible to edit unchangeable by manipulating formula objects. But that is not ideal and I need a more general solution for an object of other types.
EDIT2:
Here is an example, which shows a similar problem.
library(mgcv)
library(gam)
y <- rnorm(100)
x <- rnorm(100)
thisformula <- y ~ s(x)
gamgam <- gam(thisformula)
# s <- mgcv::s
mgcvgam <- mgcv::gam(thisformula)
This gives me the error
Error: $ operator is invalid for atomic vectors
Uncommenting the line s <- mgcv::s solves the problem in this case. But in my more general case it doesn't, and in any case it seems like a hack. How can I have all functions that are called within mgcv::gam first be looked up in mgcv?
You can refer to the function in the specific package using ::. For example ggplot2::labs will always refer to that function under ggplot2 even if it is masked by some later package being loaded
Is there an equivalent of dir function (python) in R?
When I load a library in R like -
library(vrtest)
I want to know all the functions that are in that library.
In Python, dir(vrtest) would be a list of all attributes of vrtest.
I guess in general, I am looking for the best way to get help on R while running it in ESS on linux. I see all these man pages for the packages I have installed, but I am not sure how I can access them.
Thanks
help(package = packagename) will list all non-internal functions in a package.
Yes, use ls().
You can use search() to see what's in the search path:
> search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
You can search a particular package with the full name:
> ls("package:graphics")
[1] "abline" "arrows" "assocplot" "axis"
....
I also suggest that you look at this related question on stackoverflow which includes some more creative approaching to browsing the environment. If you're using ESS, then you can use Ess-rdired.
To get the help pages on a particular topic, you can either use help(function.name) or ?function.name. You will also find the help.search() function useful if you don't know the exact function name or package. And lastly, have a look at the sos package.
help(topic) #for documentation on a topic
?topic
summary(mydata) #an overview of data objects try
ls() # lists all objects in the local namespace
str(object) # structure of an object
ls.str() # structure of each object returned by ls()
apropos("mytopic") # string search of the documentation
All from the R reference card