Loading R libraries [duplicate] - linux

I am running R on Windows, not as an administrator. When I install a package, the following command doesn't work:
> install.packages("zoo")
Installing package(s) into ‘C:/Program Files/R/R-2.15.2/library’
(as ‘lib’ is unspecified)
Warning in install.packages :
'lib = "C:/Program Files/R/R-2.15.2/library"' is not writable
To install a package, I have to specify a library location:
install.packages("zoo", lib="C:/software/Rpackages")
To load a package, I also have to specify the library location:
library("zoo", lib.loc="C:/software/Rpackages")
All of this is OK, but I wanted to see if I could add C:/software/Rpackages to the library path somehow and thus not have to type it each time.
As I searched online, I found that one way to do this is to edit the Rprofile.site file and to add the line
.libPaths("C:/software/Rpackages")
However, after doing this, and starting RStudio, this is the output that I get
> .libPaths()
[1] "C:/Program Files/R/R-2.15.2/library" "C:/Program Files/RStudio/R/library"
The .libPaths command that I added to the Rprofile.site doesn't seem to have had any effect! Why is this the case? Or more importantly, how can I fix the problem so that I can install and load packages without typing in the library location?
Note: if I start RStudio the .libPaths() command seems to work as it is supposed to
.libPaths("C:/software/Rpackages")
> .libPaths()
[1] "C:/software/Rpackages" "C:/Program Files/R/R-2.15.2/library"
Isn't that strange?

The proper solution is to set environment variable R_LIBS_USER to the value of the file path to your desired library folder as opposed to getting RStudio to recognize a Rprofile.site file.
To set environment variable R_LIBS_USER in Windows, go to the Control Panel (System Properties -> Advanced system properties -> Environment Variables -> User Variables) to a desired value (the path to your library folder), e.g.
Variable name: R_LIBS_USER
Variable value: C:/software/Rpackages
If for some reason you do not have access to the control panel, you can try running rundll32 sysdm.cpl,EditEnvironmentVariables from the command line on Windows and add the environment variable from there.
Setting R_LIBS_USER will ensure that the library shows up first in .libPaths() regardless of starting RStudio directly or by right-clicking an file and "Open With" to start RStudio.
The Rprofile solution can work if RStudio is always started by clicking the RStudio shortcut. In this case, setting the default working directory to the directory that houses your Rprofile will be sufficient. The Rprofile solution does not work when clicking on a file to start RStudio because that changes the working directory away from the default working directory.

I generally try to keep all of my packages in one library, but if you want to add a library why not append the new library (which must already exist in your filesystem) to the existing library path?
.libPaths( c( .libPaths(), "~/userLibrary") )
# obviously this would need to be a valid file directory in your OS
# min just happened to be on a Mac that day
Or (and this will make the userLibrary the first place to put new packages):
.libPaths( c( "~/userLibrary" , .libPaths() ) )
Then I get (at least back when I wrote this originally):
> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/2.15/Resources/library"
[2] "/Users/user_name/userLibrary"
The .libPaths function is a bit different than most other nongraphics functions. It works via side-effect. The functions Sys.getenv and Sys.setenv that report and alter the R environment variables have been split apart but .libPaths can either report or alter its target.
The information about the R startup process can be read at ?Startup help page and there is RStudio material at: https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio
In your case it appears that RStudio is not respecting the Rprofile.site settings or perhaps is overriding them by reading an .Rprofile setting from one of the RStudio defaults. It should also be mentioned that the result from this operation also appends the contents of calls to .Library and .Library.site, which is further reason why an RStudio- (or any other IDE or network installed-) hosted R might exhibit different behavior.
Since Sys.getenv() returns the current system environment for the R process, you can see the library and other paths with:
Sys.getenv()[ grep("LIB|PATH", names(Sys.getenv())) ]
The two that matter for storing and accessing packages are (now different on a Linux box):
R_LIBS_SITE /usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library
R_LIBS_USER /home/david/R/x86_64-pc-linux-gnu-library/3.5.1/

I managed to solve the problem by placing the code in the .Rprofile file in the default working directory.
First, I found the location of the default working directory
> getwd()
[1] "C:/Users/me/Documents"
Then I used a text editor to write a simple .Rprofile file with the following line in it
.libPaths("C:/software/Rpackages")
Finally, when I start R and run .libPaths() I get the desired output:
> .libPaths()
[1] "C:/software/Rpackages" "C:/Program Files/R/R-2.15.2/library"
[3] "C:/Program Files/RStudio/R/library"

https://superuser.com/questions/749283/change-rstudio-library-path-at-home-directory
Edit ~/.Renviron
R_LIBS_USER=/some/path

I found what I think is a solution here (thank you Carl Schwarz at SFU) for adding a personal library that is permanently (you don't have to define it each session) recognized whether using R or Rstudio, and Rstudio treats it as the default on my Mac machine. I hadn't seen it laid out this explicitly on SO, so I summarized the steps they provided, for Windows and then for Mac.
For a Windows 7 OS:
Create a directory on the drive where you want to have your personal library, e.g. C:\User\Rlibs (or another that you have permissions to)
Search for/go to "Edit environment variable for your account" in the Windows search bar to edit control panel settings
Click "New..." in the middle of the "Environmental Variables" window
In the "New User Variable" window, type R_LIBS for the "Variable name", and the path to the personal library directory you created, e.g. C:\User\Rlibs
Click OK and you should see the Variable/Value pair in the User variables window
Click OK again
Now when you start R (or Rstudio) and type the command .libPaths() you should see the personal library you created as well as the R system library.
For Mac:
In your "Home" or "username" directory create a folder called Rlibs
Launch the Terminal application
Type: echo "R_LIBS=~/Rlibs" > .Renviron Make sure the spelling and case matches.
Type ls -a to see the full list of files in the directory, which should now include .Renvrion
Verify that the .Renviron file has been set properly: more .Renviron
Launch R/Rstudio and type .libPaths() and you should see the new path to your personal library.

If you do not have admin-rights, it can also be helpful to open the Rprofile.site-file located in \R-3.1.0\etc and add:
.First <- function(){
.libPaths("your path here")
}
This evaluates the .libPath() command directly at start

just change the default folder for your R libraries in a directory with no Administrator rights, e.g.
.libPaths("C:/R/libs")

On Ubuntu, the recommended way of changing the default library path for a user, is to set the R_LIBS_USER variable in the ~/.Renviron file.
touch ~/.Renviron
echo "R_LIBS_USER=/custom/path/in/absolute/form" >> ~/.Renviron

I've had real trouble understanding this. gorkypl gave the correct solution above when I last re-installed my OS & Rstudio but this time round, setting my environment variable didn't resolve.
Uninstallled both R and Rstudio, creating directories C:\R and C:\Rstudio then reinstalled both.
Define R_LIBS_USER user variable to your prefered directory (as per gorkypl's answer) and restart your machine for User variable to be loaded. Open Rstudio, errors should be gone.
You can also use Sys.setenv() to modify R_LIBS_USER to the path of your alternative library which is easier and does not need to restart your computer.
To see what R_LIBS_USER is set to:
?Sys.getenv()
Reading help(Startup) is useful.

If your default package library has been changed after installing a new version of R or by any other means, you can append both the libraries to use all the packages with the help of the commands below.
Get the existing library path :
.libPaths()
Now,set the existing and the old path :
.libPaths(c(.libPaths(), "~/yourOldPath"))
Hope it helps.

I read the readme. In that they mentioned use .libPaths() in command line to check which paths are there. I had 2 library paths earlier. When I used the command .libpath("C:/Program Files/R/R-3.2.4revised/library") where I wanted, it changed the library path. When I typed in .libPaths() at the command line again it showed me the correct path. Hope this helps

getwd()
# [1] "C:/Users/..../software/My R studio"
copy the above link with double inverted comma
.libPaths(new="C:/Users/..../software/My R studio")
Your default path will change for installing pakages

If you want to change your library path permanently (without calling .libPath() every time when entering in R, this works for me:
create .Rprofile under your home directory. (~/.Rprofile)
type
.libPaths(c( .libPaths(), "your new path" ))
in .Rprofile file, save.
open R (any directory) and check, just type .libPaths(), you can find your libaray path is updated!

Since most of the answers here are related to Windows & Mac OS, (and considering that I also struggled with this) I decided to post the process that helped me solve this problem on my Arch Linux setup.
Step 1:
Do a global search of your system (e.g. ANGRYSearch) for the term Renviron (which is the configuration file where the settings for the user libraries are set).
It should return only two results at the following directory paths:
/etc/R/
/usr/lib/R/etc/
NOTE: The Renviron config files stored at 1 & 2 (above) are hot-linked to each other (which means changes made to one file will automatically be applied [ in the same form / structure ] to the other file when the file being edited is saved - [ you also need sudo rights for saving the file post-edit ] ).
Step 2:
Navigate into the 1st directory path ( /etc/R/ ) and open the Renviron file with your favourite text editor.
Once inside the Renviron file search for the R_LIBS_USER tag and update the text in the curly braces section to your desired directory path.
EXAMPLE:
... Change From ( original entry ):
R_LIBS_USER=${R_LIBS_USER-'~/R/x86_64-pc-linux-gnu-library/4.0'}
... Change To ( your desired entry ):
R_LIBS_USER=${R_LIBS_USER-'~/Apps/R/rUserLibs'}
Step 3:
Save the Renviron file you've just edited ... DONE !!

I had the same problem and I run into this. If you want to create another location c("C:/Users/mynewlocation") should be working as well. As mentioned in here "You should be able to right-click on the Rstudio.exe icon, click Properties, and select an option to always run Rstudio as administrator. Be sure you use that same icon whenever you want to open Rstudio."
myPaths <- .libPaths() # get the paths
myPaths <- c(myPaths[2], myPaths[1]) # switch them
.libPaths(myPaths) # reassign them

I was looking into this because R was having issues installing into the default location and was instead just putting the packages into the temp folder. It turned out to be the latest update for Mcaffee Endpoint Security which apparently has issues with R. You can disable the threat protection while you install the packages and it will work properly.

Related

Pycharm throws FileNotFoundError after refactor for some file [duplicate]

Recently, I'm unable to use relative paths in my code while using PyCharm. For instance, a simple open('test.txt', 'r') will not work - whereupon I am sure the file exists in the same level as the running py file. PyCharm will return this error.
FileNotFoundError: [Errno 2] No such file or directory:
After reading answers online on StackOverflow, I have tried multiple options including:
Changing test.txt to ./test.txt
Closing project, deleting the .idea folder, open the folder with code.
Reinstalling as well as installing the latest version of PyCharm.
Invalidating caches and restarting.
None of these options have worked for me. Is there someway I can tell PyCharm to refresh the current working directory (or even to see where it thinks the current working directory is)?
Edit: I should note that running the script in a terminal window will work. This appears to be a problem with PyCharm and not the script.
Change:
Run > Edit Configurations > Working directory,
which sets the working directory for a specific project. (This is on a Mac)
I have Pycharm 4.5, so things might have changed a bit.
Try going to Settings > Project > Project Structure
On this dialog, click your folder that has the source code in it, and then click the blue folder in the menu to note it as "source" folder. I believe this fixes a lot of the path issues in Pycharm
Here is the link to "content roots": https://www.jetbrains.com/pycharm/help/content-root.html
Current version 2019.2 somehow ignores "source root" from the "project structure". Here's how to actually enforce it:
Run -> Edit Configurations -> Python -> "Edit Templates" -> fill out "Working Directory"
__file__ refers to file path. So you can use the following to refer file in the same directory:
import os
dirpath = os.path.dirname(__file__)
filepath = os.path.join(dirpath, 'test.txt')
open(filepath, 'r')
In PyCharm, click on "run/edit configurations..."
Then find your script file in the "Python" dropdown menu. Check the "Working Directory" entry and change it if necessary.
EXACT ANSWER TO SOLVE THIS ISSUE ,,
GO TO EDIT CONFIGURATION (just LEFT side of GREEN CODE RUNNER ICON)
click on python (not any specific python script) ONLY SELECT PYTHON
then below right side click on [edit configuration templetes]
select current working dir by going into those blocks
It will change the CWD of all python file that exists in project folder..
then all file will understand the RELATIVE PATH that starts from your actual project name..
i hope this will resolve all your issue related path.
Sometimes it is different. I solved my problem by clicking "Run" at the Pycharm's toolbar and then "Edit Configurations..." and I change my Interpreter to another actual one. Just changing it in the settings does not help, but this opperation already does ;)
I too had the same issue few minutes ago...but,with the latest version of PyCharm it is resolved by simply using the relative path of that file..
For instance, a simple f = open('test', 'r') will work.
A little clarification for mac users. In mac, what #andere said above is correct for setting working directory. However, if your code is in a different folder, say working_dir/src/ (like classic java/scala file structure) in that case you still need to set your Sources Root. In mac's PyCharm this can be done by right clicking on the src/ folder > Mark Directory as > Sources Root. Helped me with lot of similar import issues. Hope this helps someone.

julia: system image file "sys.ji" not found

I am using the IDE Netbeans to code a project c++ under Linux(red hat 7). As I need to use some math functions, I try to embed the julia language into my c++ project.
Here is what I have done:
Download the Julia from here (I choose this: Generic Linux binaries)
Set project properties: build-->C++ Complier-->Include Directories, add the include of Julia, something like this: ../myjulia/include/julia
Add the libraries: open a terminal and type the command line: sudo ln -s ../myjulia/lib/julia/libjulia.so /usr/lib/libjulia.so
Now if I run my project, I will get this error: System image file "../myproject/dist/Debug/GNU-Linux-x86/../lib/julia/sys.ji" not found
I hve checked this file: ../myjulia/lib/julia, in this file, there are all of the lib files (libjulia.so etc) and a file named "sys.ji".
I ran into this issue after installing Julia v0.3.10 on a Windows machine and thought I'd post it in case it can help someone else.
When I ran julia.exe it gave me the error message listed above.
Problem:
I had created a system environment variable called JULIA_HOME and pointed it to the directory where Julia was installed. Then, I added %JULIA_HOME%\bin to my PATH.
Solution:
I pointed JULIA_HOME to the \bin directory under the Julia install directory. Then, I added %JULIA_HOME% to my PATH
A "hello world" example from here
Now we know that we need to setup the julia context with this code:
jl_init(NULL);
In fact this code may not setup a good context because the project can't find the system image file "sys.ji". So what we need to do is using another function instead of jl_init: jl_init_with_image. This function accept two parameters: the first is the path of the image file, the second is the name of the image file. So we should use it like this: jl_init_with_image("/thePathOfSys.ji", "sys.ji"); One more thing: the path of sys.ji must be the absolute path.

define NDK_ROOT in cocos2DX mutiplatform game environment

I have just started working with cocos2dx android and I am following wonderful tutorial of http://www.raywenderlich.com/33750/cocos2d-x-tutorial-for-ios-and-android-getting-started . Now, I have successfully run my first hello world demo project by following this link. I also set environment parameters:
NDK_ROOT_LOCAL="/MY ANDROID NDK PATH/"
ANDROID_SDK_ROOT_LOCAL="/MY ANDROID SDK PATH/"
I followed tutorial perfectly as given in it, still I am facing problem while running my project second time, means I have to export DNK_ROOT every time from terminal to run my project & it's really tired and seemed not working for my further implementation.. and while I run project it says please define NDK_ROOT though I already define
second thing
I also manually define these variables in my .bash profile (create-android-project.sh) this way
NDK_ROOT_LOCAL = "/MY ANDROID NDK PATH/"
ANDROID_SDK_ROOT_LOCAL = "/MY ANDROID SDK PATH/"
What am I missing in setting up this?
To make those variables permanent (so every terminal shell you open hereafter has then) use your favorite text editor to update your bash profile (I chose vi to keep it in the terminal)
NOTE: the use of "~" in a path is just shorthand for your user directory. In your case it appears to be synonymous with saying "~" = "/Users/alex"
vi ~/.bash_profile
add the following lines and save (update these names and paths to match your actual environment, I am assuming everything is in the root of your user directory here):
export NDK_ROOT_LOCAL=~/android-ndk-r10b
export ANDROID_SDK_ROOT_LOCAL=~/sdk
Use source to run the profile in the current terminal session or just open a new terminal
source ~/.bash_profile
You can test to see if the variables are defined here (use whatever you named them)
echo $NDK_ROOT_LOCAL
echo $ANDROID_SDK_ROOT_LOCAL
[EDIT: noted that paths need to be tuned to your environment]
this way i can define my NDK ROOT
export NDK_ROOT=/Users/alex/android-ndk-r8b
If you are using MAC OSX please consider adding NDK_ROOT variable in Environments file. Linux directly read it when the instance of bash is initiated but in MAC you need to add it in a bit more detail. Try adding it.

How to teach R find the texlive directory when using rstudio?

My operating system is linux mint 15, and I recently installed the texlive 2013. After installation, I appended the search path for texlive to ~/.bashrc
# set PATH
PATH=/usr/local/texlive/2013/bin/x86_64-linux:$PATH; export PATH
# set MANPATH
MANPATH=/usr/local/texlive/2013/texmf/doc/man:$MANPATH; export MANPATH
# set INFOPATH
INFOPATH=/usr/local/texlive/2013/texmf/doc/info:$INFOPATH; export INFOPATH
Then I could locate cmds such as pdflatex on xterm. However, when I wanted to use the pkg Sweave in rstudio, it always prompted that No Tex installation detected. I examined the $PATH in rstudio by Sys.getenv("PATH") and found out that the texlive/ directory was not appended to $PATH. So I guessed the problem was that the environment of Xsession doesn't take ~/.bashrc into considration. How to address this issue. Any help would be appreciated.
I have recently set up a configuration like yours.
The most correct solution is to put those lines into ~/.profile (or /etc/profile, for all users, as I did); this way all processes will inherit that PATH, not only those derived from bash.
You are right. R Studio runs in a shell that doesn't pay attention to the usual startup scripts. As far as I can tell, the appropriate place to change them globally (for all users) is R_HOME/etc/Renviron.site and for individual users, $HOME/.Renviron. (On my system, R_HOME is /usr/lib/R.)
Run ?Startup in the R Studio console for more details.

Installing TexLive 2010 in Ubuntu 10.10: problem with PATH

I'm followint these instructions to install Kile+TexLive 2010 with package manager on my Ubuntu Maverick: http://ubuntuforums.org/showthread.php?t=141934
The problem I have is that when I finish downloading all the packages to my computer, I have to edit the path but Ubuntu doesn't recognize it. The lines are the following:
PATH=/usr/local/texlive/2005/bin/i386-linux:$PATH
export PATH
I run echo $PATH and as long as I don't close the terminal, the path appears with the echoing, but if I close it, the path disappears. Nevertheless (whether I close the terminal or not), I'm supposed to run texhash but I am told that the command is not found. I already tried editing the path by adding the two lines above to both .bashrc in my home directory and to bash.bashrc in /etc/ directory.
I'm just following the instructions linked above, but I'm a linux rookie. Could anyone help, please?
in order to permanently change any environment variable under Ubuntu/Linux, you must modify the files you mentioned (for example ~/.profile). If you simply issue an export via the terminal, its effect will end once the terminal is closed. Sometime it is needed to perform a logout/login for the changes to take effect.
Also, mind the syntax of what you write in the above mentioned file(s), like "'s around $PATH.
Refer to this question: https://serverfault.com/questions/44275/how-to-add-a-directory-to-my-path-in-ubuntu

Resources