Installing packages upon starting R session - r

I am fairly new to R programming. I am trying to customize my R setup so that when an R session is started a few packages are installed at the beginning. I know that there is a .First() function that I can write in the Rprofile.site file. However, upon adding my install package code inside the .First() function, the package does not get installed.
Furthermore, it seems to go into a loop of trying to create the package and it creates a lock file in the library folder in R. This causes my computer to really slow down (almost to the point where it is frozen) because it keeps trying to install that package.
Here is the code that I have added to the end of the Rprofile.site file.
.First <- function() {
install.packages("customPackage.tar.gz", repos=NULL, type="source")
cat("\nWelcome to R on ", date(), "\n")
}
I even tried adding the install.packages line just by itself in the file (without having a .First() function) to no avail.
The customPackage.tar.gz refers to a package I built up using existing code that I have written up. Since this is a custom package, the repos is NULL. If I do not include this line in my .First() function and just run the command after launching the R session, the package gets installed just fine in the R/R-2.15.0/library folder.
There are several custom packages that I need to have installed upon the beginning of an R session, and that's why it is important that I add all these installation lines of code in the Rprofile.site file. Any ideas on how I can do this? Everywhere I have looked online on customizing the Rprofile.site file shows examples of just using libraries that already exist (For example, library(R2HTML)), but nothing for installing new libraries. Thanks for the help!
Edit: Thanks for the help guys! I actually do need to install these packages in multiple machines for each user and instead of having them manually install the package once, I figured it'll be good to do it in the site file. I tried Justin's suggestion for checking the package first and that worked! Thanks for the help again!

Unless you're switching from computer to computer you should have this package already in your library (that is once you install a package once it should always be there). Use installed.packages() [,1] or library() to see all packages in your library. If you see it there then use this:
.First <- function() {
require(customPackage)
cat("\nWelcome to R on ", date(), "\n")
}

Related

R: Error in install.packages : cannot open the connection

I was trying to install the package RINDSEL but I am unable to install it and I keep getting the following error:
Error in install.packages : cannot open the connection
I downloaded the package from:
rindsel_1.0_2.zip | Integrated Breeding Platform
and loaded it from the directory. Other packages from the directory can be installed but just not this one.
Is the package corrupt or could there be any other error?
I would really be grateful for any help. Thanks in advance
Rename the zip file RinSel Software into Rindsel. That's the name specified in the discription file.
Then, you can install the package in R with the command
install.packages("C:/path/to/Rindsel.zip",repos=NULL,type="win.binary")
That works fine... at first (!!!).
Problem with the Rindsel package is. It is quite old. It was build with R 2.13.1. Therefore, if you want to load the library which would be the next step to use the package in R you will get the error:
Problems building package (Error: "package has been build before R-3.0.0")
My suggestion: Contact the authors of the package and ask them if they can either provide the source file that you can build the package by yourself or if they can bundle the Rindsel package with a newer R version.
(Or you could try to hunt down an old R version and see if you can get the thing running with an old R... However, I would not seriously suggest to do that. It would probably result in conflicting dependencies with the other required packages...)
EDIT 15-02-2018: OP asked if one can build an R package with sources that are presumeably the Rindsel source files.
Yes, basically, you could do that. You would have to make a your own description and namespace file and put the source file in the R folder than invoke the command in R to build it....
But it's not neccessary with the script files provided by the link the OP posted.
OP, just run the scripts in R! It's quite easy.
Download the zip-file and extract it on your machine.
Go to that directory. The R command would be
setwd('path/to/your/directory')
Than run the R script, e.g, the KNIndex.r. It's simple:
source('KNIndex.r')
Then the script will run and produce some output / prompts.
For future readers,
I was able to fix the error by running RStudio with administrative privileges to get the command to work.
If that does not fix it, you might wish to try
Installing "r tools" if that is not installed already. That can be downloaded from
https://cran.r-project.org/bin/windows/Rtools/
Download a relevant package that you are trying to install (e.g., tidyverse_1.3.0.zip) from https://cran.rstudio.com/
and install that from local path
It can also be installed directly from the web using install.packages("https://cran.rstudio.com/bin/windows/contrib/4.0/tidyverse_1.3.0.zip")
I had the same problem. R was not able to extract and compile the package files to the default installation directory for some system-specific reasons (not R related).
I was able to fix this by specifying the installation directory of the package lib using:
install.packages("your package", lib = 'path/to your/required/installation/directory')
You can then load the package by specifying the lib.loc option while loading it:
library('your package', lib.loc='path/to your/required/installation/directory')
A better solution:
Create a new environment variable (if you are using windows) R_LIBS_USER with the following directory path/to your/required/installation/directory.
This will change the default installation directory of the packages and make it easier to load and install them without specifying the location everytime.

how to read a SAS data (.sas7bat) in R [duplicate]

I am trying to embed RInside to my application on win7 64-bit system but when I initialize an RInside:
Rin = new RInside(argc, argv);
the following message appears:
Error in loadNamespace(name) : there is no package called 'Rcpp'
This error only occurs with Windows.
I think you get that issue when your .libPaths() differ--in other words run the .libPaths() function to see the paths stored by R for its use. Then check where RInside is installed, and make sure Rcpp is installed there too. It is a setup issue.
In other words, it should work if you have Rcpp and RInside installed where the basic R libraries are. Otherwise you have to tell the (embedded) R session about the other location (and before it starts).
There are more Windows users on the list, so you could try asking on rcpp-devel.
First get your default library locations by command ".Library" in R.
Get Rcpp package from https://cran.r-project.org/web/packages/Rcpp/index.html.
Unzip and copy folder "Rcpp" to your default library locations obtained from step1.
Now you are ready to install packages which have dependencies on Rcpp.
Dirk is right in this case, BUT if the .libPaths() does not work, then please also check if you have the latest packages.
I am posting this as an ancillary answer backup which I ran into with the shiny package backend switch of their code needing Rcpp!
In this case of getting the "no package" error message, I fixed it by:
Selecting devtools package and then using this line below. (if you don't have devtools then get it with install.packages("devtools")
devtools::install_github("rstudio/shiny")
The development version of the package handled this better, and added the package as a dependency.
Mods - I realize this is an answer to an old question, but I might help others not wasting an hour like I just did.
You might find it easy if the answers are for both R studio users and non R studio users.
R Studio users
First get your default library locations by command ".Library" in R.
Get Rcpp package from https://cran.r-project.org/web/packages/Rcpp/index.html.
Unzip and copy folder "Rcpp" to your default library locations obtained from step 1, you will find another folder named library, paste the unzip folder in it.
Non R studio Users
First get your default library locations by command ".libPath" in R.
Get Rcpp package from https://cran.r-project.org/web/packages/Rcpp/index.html.
Unzip and copy folder "Rcpp" to your default library locations obtained from step 1, you will find another folder named library, paste the unzip folder in it.
I was also getting this error while trying to run the 'ggplot' function from the ggplot2 package. After trying the suggestions posted here and elsewhere (checking file paths, restarting R, clearing out my environment, etc.) and encountering several other cryptic error messages, it turned out that I needed to download the latest version of base R for Windows (v3.4.1) and update my version of R-Studio to the latest version also (v1.0.153).
After doing this my 'ggplot' function was working again and I was able to render my figure from R Studio without any further issues.
I was also getting this message when trying to use ggplot. I first updating both my R for Windows to 3.4.3. Then updating R studio to version 1.1.423; then, updating all of the packages and being sure to access the R version 3.4.3 from R studio, I still got the message. None of these things fixed the error. I was ready to give up until I noticed that I was calling library(ggplot) and had ggplot::ggplot in my code. THIS WAS THE PROBLEM. I changed it to library(ggplot2) and the instance to ggplot2::ggplot(...). THIS FIXEd the problems.
I was facing a similar issue, and I simply installed the said package. It's working perfectly for me.

Error in loadNamespace(name) : there is no package called 'Rcpp'

I am trying to embed RInside to my application on win7 64-bit system but when I initialize an RInside:
Rin = new RInside(argc, argv);
the following message appears:
Error in loadNamespace(name) : there is no package called 'Rcpp'
This error only occurs with Windows.
I think you get that issue when your .libPaths() differ--in other words run the .libPaths() function to see the paths stored by R for its use. Then check where RInside is installed, and make sure Rcpp is installed there too. It is a setup issue.
In other words, it should work if you have Rcpp and RInside installed where the basic R libraries are. Otherwise you have to tell the (embedded) R session about the other location (and before it starts).
There are more Windows users on the list, so you could try asking on rcpp-devel.
First get your default library locations by command ".Library" in R.
Get Rcpp package from https://cran.r-project.org/web/packages/Rcpp/index.html.
Unzip and copy folder "Rcpp" to your default library locations obtained from step1.
Now you are ready to install packages which have dependencies on Rcpp.
Dirk is right in this case, BUT if the .libPaths() does not work, then please also check if you have the latest packages.
I am posting this as an ancillary answer backup which I ran into with the shiny package backend switch of their code needing Rcpp!
In this case of getting the "no package" error message, I fixed it by:
Selecting devtools package and then using this line below. (if you don't have devtools then get it with install.packages("devtools")
devtools::install_github("rstudio/shiny")
The development version of the package handled this better, and added the package as a dependency.
Mods - I realize this is an answer to an old question, but I might help others not wasting an hour like I just did.
You might find it easy if the answers are for both R studio users and non R studio users.
R Studio users
First get your default library locations by command ".Library" in R.
Get Rcpp package from https://cran.r-project.org/web/packages/Rcpp/index.html.
Unzip and copy folder "Rcpp" to your default library locations obtained from step 1, you will find another folder named library, paste the unzip folder in it.
Non R studio Users
First get your default library locations by command ".libPath" in R.
Get Rcpp package from https://cran.r-project.org/web/packages/Rcpp/index.html.
Unzip and copy folder "Rcpp" to your default library locations obtained from step 1, you will find another folder named library, paste the unzip folder in it.
I was also getting this error while trying to run the 'ggplot' function from the ggplot2 package. After trying the suggestions posted here and elsewhere (checking file paths, restarting R, clearing out my environment, etc.) and encountering several other cryptic error messages, it turned out that I needed to download the latest version of base R for Windows (v3.4.1) and update my version of R-Studio to the latest version also (v1.0.153).
After doing this my 'ggplot' function was working again and I was able to render my figure from R Studio without any further issues.
I was also getting this message when trying to use ggplot. I first updating both my R for Windows to 3.4.3. Then updating R studio to version 1.1.423; then, updating all of the packages and being sure to access the R version 3.4.3 from R studio, I still got the message. None of these things fixed the error. I was ready to give up until I noticed that I was calling library(ggplot) and had ggplot::ggplot in my code. THIS WAS THE PROBLEM. I changed it to library(ggplot2) and the instance to ggplot2::ggplot(...). THIS FIXEd the problems.
I was facing a similar issue, and I simply installed the said package. It's working perfectly for me.

How to remove R package that has dependencies on other packages?

I've installed a R package (PathRanker) that depends on packages (xml,Rgraphviz,Rcurl). I've noticed that the all the packages' folders inside the library shows some sort of a locked sign.
I tried to remove PathRanker using the R CMD REMOVE but it didnt works. I tried to reinstall the package (which I kindda mess around with the code) it also didnt works. So I think it got something to do with the dependency packages. Can someone help me? thanks...
You might have installed the packages as root (if you are using Linux), and trying to remove the package as normal user.
Start an R session as root (sudo R), and try to remove (see: ?remove.packages) the packages in this environment. Or from console:sudo R CMD REMOVE (package name)
If still not working, please write more about the error messages.

How do I fix a bug in an open source R package? [duplicate]

This question already has answers here:
How to edit and debug R library sources
(3 answers)
Closed 6 years ago.
I'd like to fix a package, but can't figure out how to edit it. I can download the source from R-Forge as a .tar.gz and unzip it. There is an "R" directory with the source and also a "tests" directory.
How do I include the sources in my own project to test my edits?
How do I run the tests? The tests each start with "library(blotter)". How do I make that load the library from the sources I've downloaded.
The recommended process is described in some detail in the manual 'Writing R Extensions' that came with your R installation.
There are also numerous tutorials all over the web.
No need to re-zip the source. Just load the package again from the source on your drive:
install.packages(/path/to/package, repos = NULL, type="source")
your method of doing the for loop to loop across R files will work in some situations but in others it may not. For example, if there is compiled non R code then looping across the *.R files may not work properly. So it's generally better to just install.packages() again.
BTW, if you clone a version control repo to your hard drive you generally don't get the zip file. So the same method is used to install.
What I have done is the following (IIRC) remove the old package remove.packages. Re zip your new package to a tar.gz file again (with your changed sources). Install the new package with install.packages with pkg = path to your zipped library, and repos=NULL.
When I'm doing testing on a package, I use a little bash script that looks like this:
#!/bin/bash
#build the package from source
R CMD build ../pkgdirectory/
#remove the old version and install the new one
R CMD REMOVE pkgname
R CMD INSTALL pkgname_0.7.tar.gz
Replace package name with the appropriate names. Save it as "make" and then run it whenever you finish a major edit. Then your testing scripts can use library(pkgname) just like normal.
I found that I could load all the sources to save me from the ridiculous repackaging by doing:
for (file in dir("../R", pattern="*.R", full.names=TRUE)) {
source(file)
}

Resources