Setting LD_LIBRARY_PATH from inside R - r

I have installed an R package but, in order to load it via library, the LD_LIBRARY_PATH needs to be set to the path where one of the libraries, called libhts.so.2 is located. The loading only works when editing the LD_LIBRARY_PATH before going into R, not after.
I have tried several different methods in solving this:
exporting a modified LD_LIBRARY_PATH from the configure script located in the R package.
Creating a soft link to the shared library within the same configure script.
Both have not worked and it seems to me that there is a variable that stores the results of the LD_LIBRARY_PATH once R is started. Maybe the solution is editing that variable.

With help from Hans Lub, the way to solve the problem is by using the dyn.load() function and supplying the full path to the library:
dyn.load('path_to_library')
and then, loading via library should work.

Related

Reticulate package, cannot install Python packages using py_install()

I am following the instructions here to install packages (I'm looking at the "Simple Installation") section: https://rstudio.github.io/reticulate/articles/python_packages.html
Here is my R code:
library(reticulate)
use_python(python="C:\\Users\\...\\AppData\\Local\\Programs\\Python\\Python39\\python.exe")
py_install("pandas")
However, I'm getting the following error:
Error: could not find a Python environment for
C:/Users/.../AppData/Local/Programs/Python/Python39/python.exe
How do I resolve this? I have used py_install() this way on another computer before, so I know it's possible and I would like to use it. However, I can't understand what I'm missing.
You might respond that I need to specify an environment when using py_install(), but the instructions in the link don't require that, and I know it can be done without specifying an environment every time. Do I need to somehow set a "default"?
EDIT: Just to clarify, I am on Windows.
You may not be connected to the proper environment. Depending on your OS, you will likely have to create a .Renviron file and direct R to look for it.
This post here, should help you diagnose the issue:
Unable to change Python path in reticulate

Leaving one library path for R on ubuntu

I am using ubuntu 14.04. I want to set up myself just one library path for R where all of the packages would be installed. I tried setting up environment variables R_LIBS, R_LIBS_USER in .bash_profile (of course running source ~/.bash_profile afterwards), tried adding .libPaths(.libPaths()[1]) to Rprofile.site, but the only thing that I achieve is that my own library is added to 3 other already present ones. Due to this I am getting errors when some packages are from other R version that was previously installed. I want to leave just one library path and prevent R from searching in all others. I found several threads about the issue but neither of them worked for me:
Remove a library from .libPaths() permanently without Rprofile.site
https://community.rstudio.com/t/reinstalling-packages-on-new-version-of-r/7670
I can try just uninstalling all of the packages and reinstalling them over, but I kind of want everything to be structured and organized anyway. I do not like very much that R shuffles libraries into different folders and want to have just one or max two that were defined solely by me.

Load my own R package

I made an R package for personal use, but the way I load it is by individual files. Such as:
source("../compr/R/compr.R")
source("../compr/R/error_df.R")
source("../compr/R/rmse.R")
I would like to load the entire package, which is called compr, as I would other libraries.
If you are using RStudio, I would suggest creating a project and setting it to your compr directory. After that you will be able to use devtools::load_all() to load your package directly.
If you don't want to do this, or you don't use RStudio devtools::load_all('path/to/compr') will also work.
P.S. compr directory needs to be the root of the package i.e. the place where your DESCRIPTION file is.

R setting library path via R_LIBS

I have read the R FAQS and other posts but I am a bit confused and would be grateful to know whether I did everything correctly.
In Windows, in order to modify the default library folder I created a file Renviron.site and put inside E:/Programs/R-3.3.0/etc.
The file has only one line saying
R_LIBS=E:/Rlibrary
When I open R and run .libPaths() I see E:/Rlibrary as [1] and the default R library E:/Programs/R-3.3.0/library as [2].
This should mean that from now on all packages I will install will go in E:/Rlibrary but at the same time I will be able to load and use both packages in this folder and those in the default location. Am I correct?
When you load a package via library, it will go through each directory in .libPaths() in turn to find the required package. If the package hasn't been found, you will get an error. This means you can have multiple versions of a package (in different directories), but the package that will be used is determined by the order of .libPaths().
Regarding how .libPaths() is constructed, from ?.R_LIBS
The library search path is initialized at startup from the
environment variable 'R_LIBS' (which should be a colon-separated
list of directories at which R library trees are rooted) followed
by those in environment variable 'R_LIBS_USER'. Only directories
which exist at the time will be included.

R how to use files in the same packages

I am planing to do all my R scripts in the same package in order to move it easily between my friends.
What I have done is created an R packages using R studio and the following files have been automatically generated:
projectName.Rproj
DESCRIPTION
man
NAMESPACE
R
Read-and-delete-me
I created a new R script and I save it in R folder. Now I want to add a new R script that uses functions that have been defined in the first script.
I created this new script and I tried to use on of the functions that are located in the other script. I got error that the function is not defined.
What I tried to solve the problem
I used the source command in the beginning of the new script like this:
source('something.R')
I got error message that something.R doesn't exist.
What is the solution please to include functions that exist in a different scripts ** but in the same packages**?
NoteI don't want to use relative paths because I want the package to be as portable as possible
Thanks a lot
You appear to misunderstand how package works: by having a file with a function in the R/ directory, it is already visible to other code in the package.
If you want to make it available to other packages, you can control this via the NAMESPACE file. All this is well-documented in Writing R Extensions which comes with your copy of R, and a number of additional books and tutorials you could peruse.

Resources