R could not find function "col2rgb" - r

When I started R it came with the following Error
Error in alpha(fill, alpha) : could not find function "col2rgb"
when I try to compile PDF in knitr I get the same error:
Error in alpha(fill, alpha) : could not find function "col2rgb"
Calls: .First ... grob -> gpar -> validGP -> numnotnull -> match -> alpha
Execution halted
I don't know how to isolate this problem, since it last I used R and no when I start it produces the error. From what I can gather the col2rgb should be in the base package {grDevices}
library(grDevices)
grDevices::col2rgb("white")
[,1]
red 255
green 255
blue 255
Which seems to work fine.
The error is independent of knitr
Anyone know a fix, or can give any suggestion as to how to isolate the problem?
Best Regards

You must have saved your workspace. Almost all experienced R users never do this.... so don't do that in the future.
For now, you'd find an .Rdata or .RData or similar in your working directory/folder and rename it (so if it contains important stuff you can still load(.) it manually afterward.
After that, starting R should not give your message anymore
(the grDevices package is one of the packages that are always loaded in R ((unless you specifically disable that; which may even be difficult in RStudio))).

Found it!
It was due to a source("") of some often used functions that was sourced at startup via the .Rprofile. when i moved the sourcing to after Rstudio had fully started it works - even knitr.

Related

R: Show sources of install.packages

I would like to read the source files of the install.packages function. More specifically, I'd like to understand exactly how this function downloads files from web repositories.
However, these source files seem hidden (debug(install.packages) is useless). Do you have any idea how i can access them? BTW I thought hiding source files was something not commonly done in R...
If you are running in RStudio, there are two functions with that name: the wrapper that RStudio put in place, and the original one from the utils package in base R.
You can see the original one by typing
utils::install.packages
The source is really long, so I won't post it.
You can see the RStudio replacement by leaving off the prefix:
> install.packages
function (...)
.rs.callAs(name, hook, original, ...)
<environment: 0x7f856709ec00>
Seeing what they put in their hook is a little trickier:
get("hook", envir = environment(install.packages))
It's not completely clear what you want to find from the source, but at some level R is going to use the download.file() function to download things. You can debug that to see what files it downloads.

R: Error in as.environment(pos) / Error during wrapup: no item called "package:[package]" on the search list

This is similar to this but with a followup question:
I'm preparing my package for submission to CRAN. In one script, it gives the titular error:
Error in as.environment(pos): no item called "package:shapefiles" on the search list
or
Error during wrapup: no item called "package:shapefiles" on the search list
If, like the similar thread linked above, I add
attachNamespace("shapefiles")
Then I no longer get that error, but instead I sometimes get the error or warning:
namespace(shapefiles) was already taken
I believe this happens if the function/script is run more than once. What's so weird about this to me is that
#' #import shapefiles
is in that script's markdown block,
Which means
import(shapefiles)
is in NAMESPACE, and
Imports:
shapefiles (>= 0.7),
is in DESCRIPTION. I have numerous other package dependencies which are imported the same way (correctly, I believe), and none of them produce this error. I don't see why this is behaving differently therefore, and would like to avoid having either issue before submitting to CRAN. The code that calls this package, in my script, is
shapefiles::read.shapefile(savename)
Script is here, for reference.
Thanks in advance for any ideas!
Edit: steps to reproduce:
setwd("/folder")
library(devtools)
install_github("SimonDedman/gbm.auto")
library(gbm.auto)
gbm.basemap(bounds = c(-81.7, -80.3, 24.7, 25.9),
savedir = "/folder")
I've just removed shapefiles:: from the code. The correct output is produced regardless.
Problem resolved itself after commenting out attachNamespace("shapefiles"). No idea why.

Installing / Using R Packages in Sublime Text 3

I am trying to use the R function dmvnorm (found in the mvtnorm package) in Sublime Text 3. I installed it and ran my code in RStudio, so I know the code is fine. In sublime, I entered:
install.packages('mvtnorm',repos='http://cran.us.r-project.org')
library(mvtnorm)
It looked as though it worked, but when I ran my code it said:
Error: could not find function "dmvnorm"
I'm using a Mac and my hunch is this is somehow related to specifying the path in Preferences -> Package Settings -> Sublime REPL -> Settings - User. The current path displayed as part of the error reads:
[path: /usr/bin:/bin:/usr/sbin:/sbin]
Thanks!
I had a hard time with this recently as well. I'm very new to R so I'm not sure this is the best answer, but to hold you off until someone gives a better one, did you include library('package.name') in the file (in sublime)? require('package.name') also works, but this appears not to be best practice for reasons described, e.g., here.
I hope this helps!

R warning: " 'package:stats' may not be available when loading"

I've been working with the arules package and RODBC package for a few days now in RStudio. I saved the workspace image multiple times in the meantime. But today I wanted to save my workspace image again and now I get this warning:
Warning message:
In save(list = ls(envir = .GlobalEnv, all.names = TRUE), file = outfile, :
'package:stats' may not be available when loading
I only used arules and RODBC functions and str_split_fixed from stringr and some standard stuff like data.frame.
I didn't even use the stats package at all. When reloading the workspace image, it doesn't seem affected by it. But the warning is bothering me anyway, especially since stats is supposed to be a standard package of R, how can it be unavailable... How can I resolve this warning?
As the commentator didn't post his comment as an answer, I will close this question myself: it's a bug in RStudio and knowing this thanks to SO I could close down RStudio and a couple R programming sessions later the warning message did not occur again.
This is now an official rstudio bug.
In my case (the repro in the bug) the root cause was that one of the objects I was trying to save referenced an rstudio-internal function (this isn’t very exotic. A plain options call returns a list that does that). RStudio functions live in tools:rstudio namespace, which inherits the stats package among others - hence the warning.
It is currently planned to be fixed as part of v1.4.

RcmdrPluginPackage Paths

My paths and files are as follows ...
E:R/R-2.15.1/library/Rcmdr/
E:R/R-2.15.1/library/RcmdrPlugin.Package/
E:R/R-2.15.1/MyLibrary/RcmdrPlugin.Package.zip
E:R/R-2.15.1/MyLibrary/RcmdrPlugin.Package/
where, in the name RcmdrPlugin.Package, I've used the word 'Package' to represent the name of the actual package being used.
The installation is as described above because (i) I'm not an expert at installing packages, (ii) I couldn't do a direct install from Cran because I wanted to put the package onto a USB stick; and, (iii) at work the Cran server is blocked (sic).
When I start the package from the GUI the Cmdr opens once and quickly closes (I don't know if this is relevant or normal) and opens again. Once open, I can operate the package via the Cmdr interface. It's a very nice package, everything works really well until I want to save the work. Then I get the following error,
Error in obj[i] : object of type 'closure' is not subsettable
I've been in contact with the people who developed (and are still developing) the package and they cannot reproduce the bug.
I strongly suspect that the problem lies in my 'crappy' install and file configuration, rather than with the package.
Can anyone please help me by suggesting how I would undo what I've done and do it properly in view of the constraints list above?
I appreciate that I can use Remove to get rid of the package but I don't want to start tinkering with something without having a greater understanding of what I'm doing.
Lastly, note that is error has been discussed a number of times on this list but not within this particular context.
I've managed to get it to work by following the instructions from here stackoverflow.com/questions/12820189/… where I set repos=NULL after I'd put the package into /MyLibrary/

Resources