How to install libpng-dev on Mac - r

I am trying to install the reticulate package on my Mac and it depends on the png package, which in turn depends on libpng. I installed libpng with brew but the png package fails due to a missing libpng-config:
/bin/sh: libpng-config: command not found
However I have this in /opt/homebrew/bin/libpng-config:
which libpng-config
/opt/homebrew/bin/libpng-config
I found this that specifies the need for libpng-dev but I have no idea how to install that on my Mac. Any help is appreciated.

Processes not started from a shell may not inherit environment variables from that shell. Start R in Terminal to make sure that your R process inherits PATH from the Terminal shell where you have run which. Something like
$ Rscript -e "install.packages(\"png\")"
should work, though you may need to select a CRAN mirror, in which case the above will throw an error. You can do that in the install.packages call, like so:
$ Rscript -e "install.packages(\"png\", repos = \"https://cloud.r-project.org\")"
or by setting a global option, like so:
$ Rscript -e "options(repos = \"https://cloud.r-project.org\"); install.packages(\"png\")"
For details, see the R for macOS FAQ and ?options.

Related

R: command not found

I want to install rJava but this fails with the following suggestion:
Make sure you have Java Development Kit installed and correctly registered in R.
If in doubt, re-run "R CMD javareconf" as root.
ERROR: configuration failed for package ‘rJava’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rJava’
* restoring previous
‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rJava’
Hence, I follow this suggestion (and the one everyone else seems to suggest) and run R CMD javereconf in the terminal but now I get the message that zsh: command not found: R.
How can I get R CMD javereconf to work?
Thanks.
EDIT: While I followed the blogpost suggested by #Till, I still struggle to run R CMD javereconf (same error). In the meantime, I figured that I should mention that I'm using MacOS Big Sur with an Apple M1 Chip.
When typing R.home() RStudio returns /Library/Frameworks/R.framework/Resources/R.
There is currently a bug in CRAN's R installation package that results in it not correctly installing symbolic links to R and Rscript for commandline use. I've just verified this by inspecting the postflight script in their 4.0.5 installation package.
Basically, there's a problem wherein the uname check doesn't know about operating system releases of 20 and above.
If your uname -r release version is over 20, this would explain why the installation silently failed. My recommendation would be to manually create the symbolic links the package is supposed to create by doing something like this:
if uname -r | grep '^2' >/dev/null; then
## 15.0 and higher don't allow messing with /usr/bin
## so use /usr/local/bin instead
if [ ! -e /usr/local/bin ]; then
mkdir -p /usr/local/bin
fi
cd /usr/local/bin
# create convenience links to R and Rscript
rm -f R Rscript
ln -s /Library/Frameworks/R.framework/Resources/bin/R .
ln -s /Library/Frameworks/R.framework/Resources/bin/Rscript .
fi
I'm going to file a bug with R-project shortly.
Find the location of R and Rscript on your disk by running R.home() on Rstudio's R console.
Then, follow this guide and copy the returned location from R.home() and paste on a new line on /etc/paths.
Then, restart the terminal and run R CMD javareconf

R CMD REMOVE has no effect

$ /usr/bin/R --vanilla CMD REMOVE xgboost
Removing from library ‘/home/arferk01/R/x86_64-pc-linux-gnu-library/3.5’
But the directory's still there:
$ ls /home/arferk01/R/x86_64-pc-linux-gnu-library/3.5/xgboost
data DESCRIPTION html libs Meta R
demo help INDEX LICENSE NAMESPACE
Likewise if I say /usr/bin/R --vanilla -e 'remove.packages("xgboost")'.
What's going on?
I'm running R 3.5.0.
I just figured it out: it was a permissions issue. I'd installed a package to my user library with root ownership because I'd forgotten the -H in sudo R CMD INSTALL. What's inscrutable is that CMD REMOVE gives no error message.

Trouble loading rJava into R and RStudio [duplicate]

I recently "upgraded" from OSX Mountain Lion to Yosemite and from R 3.1.3 to 3.2. Immediately after the upgrade, when I opened R or RStudio I got a pop-up message saying that I needed to install Java 6. In addition, loading rJava or any package that depends on rJava (e.g., xlsx) caused RStudio to crash (R also crashed when I tried this by opening R.app directly).
After trying a few fixes found on Stack Overflow and elsewhere (more details below), I am at a point where loading rJava or any package that depends on rJava no longer causes R to crash, but results in the following error:
library(rJava)
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: #rpath/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so
Reason: image not found
Error: package or namespace load failed for ‘rJava’
However, if I invoke R from the command line and then load rJava or any package that depends on rJava, it seems to work (or at least I don't get any error messages).
I've tried a number of different attempted fixes, some of them a few times, and can't quite remember exactly what I did in what order (didn't realize this would be such a morass and wasn't really keeping track), but here's the gist of it:
Added the following to my .bash_profile (per this SO answer):
export JAVA_HOME="/usr/libexec/java_home -v 1.8"
export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/server
Reconfigured java from the command line as follows:
sudo R CMD javareconf -n
Checked options("java.home") and discovered this was set to NULL. I tried setting it to the following (per this SO question):
options("java.home"="/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre")
Installed the latest Java Development Kit and reinstalled rJava from source (can't remember where I found that one).
At some point while trying all of these, I was able to load rJava without crashing R, but instead got the error message posted above. In addition, when I quit RStudio, it would seem to close normally, but then an "RStudio quit unexpectedly" message would pop up, indicating that the program had crashed while trying to close.
I finally decided to install Java for OS X 2014-001 (Java 6), as I seemed to be running out of options. Now, when I opened R or RStudio the "This software needs Java 6" pop-up message no longer appeared. However, I was still getting the .onLoad failed in loadNamespace() for 'rJava' error message posted above.
In reviewing some of the posts I'd already looked at, I noticed another SO answer that I'd missed before, which recommended opening RStudio with the following command line code that gives RStudio the correct path to java:
LD_LIBRARY_PATH=$(/usr/libexec/java_home)/jre/lib/server: open -a RStudio
That opened an RStudio window and I was also able to load rJava and packages that depend on it without getting an error.
Lastly, I tried running R from the command line (which I hadn't done before). It turns out that on the command line, loading rJava or any package that depends on rJava works and does not throw any errors.
So, I can now get rJava to work if I open RStudio from the command line with the code that gives RStudio the java path (as noted above). However, I'd like to find a way to fix the underlying problem, whatever it may be, so that RStudio can be opened in the usual Mac way, without needing a command line kludge. I'm also concerned that having an old version of Java installed could cause problems down the road.
Does anyone have any ideas about how to diagnose and solve this issue?
I had the same problem and went through the same steps as you. The final step to allow starting RStudio through Finder/Spotlight was to link libjvm.dylib to /usr/local/lib:
sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
-f flag is added to force overwriting existing file/link
On OSX El Capitan 10.11, the user doesn't have permission to write to /usr/lib. So instead, use the following command:
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
You can load the libjvm.dylib explictly, by run like
dyn.load('/Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/jre/lib/server/libjvm.dylib')
library(rJava)
On macOS High Sierra (10.13.1) and Java Version 9 you have to use a slightly different JVM path (notice the missing jre folder in the path compared to the instructions for earlier Java versions):
sudo ln -f -s $(/usr/libexec/java_home)/lib/server/libjvm.dylib /usr/local/lib
You also have to notify R about the JVM:
MY_R_VERSION=$(Rscript -e "cat(with(R.version, sprintf('%s.%s', major, substring(minor, 1, 1))))")
ln -s /usr/local/lib/libjvm.dylib /Library/Frameworks/R.framework/Versions/$MY_R_VERSION/Resources/lib/
Following command works:
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
After that, in RStudio, loading rJava works through loading of the "xlsx" package.
library("xlsx")
P.S. #1 Environment: Mac OS X El Capitan 10.11.3+ with RStudio 0.99.491+ and R 3.2.3+. (I've now tested this also on macOS Sierra (10.12) and R.3.3.1.)
P.S. #2 I find that openxlsx is much faster and doesn't rely on Java to work, so I'm now using that package. Hope that helps others.
You should be able to use the CRAN rJava, without needing to recompile rJava or do any additional steps by linking the shared library to the R frameworks library directory.
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /Library/Frameworks/R.framework/Libraries
This is how I configured it working properly on two Macs running Mac OS X El Capitan (10.11.6):
Uninstall 'rJava' by issuing the following commands in a terminal window:
Rscript -e 'remove.packages("rJava")'
sudo Rscript -e 'remove.packages("rJava")'
Download and install the Java software from Oracle: https://www.java.com/en/download/mac_download.jsp
Add the following lines to /Users/<userid>/.bashrc using your favorite editor:
# Set JAVA_HOME so rJava package can find it
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)/jre
Close and restart any terminal, R, and RStudio windows (to pick up the changes to .bashrc).
Run the following command in a Terminal window:
sudo ln -sf $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
Run the following command in a Terminal window:
sudo Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")'
Here is the easy steps for it:
remove the rJava package: remove.packages(rJava)
close R
install latest Java on you mac
open terminal and type this command: sudo R CMD javareconf
Open R and install rJava with this command:
install.packages("rJava", dependencies=TRUE, type="source")
The image not found issue exists with a brand-new install on OSX High Sierra with the latest Java SE 10 JDK.
I was able to solve the path issue with rJava using the fix found on the rJava Github issues page: https://github.com/s-u/rJava/issues/78
R CMD javareconf JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers
Here what worked for me on MAC:
in your ~/.profile or ~/.bashrc add this line:
%export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home
1.1 % source .profile (or % source .bashrc)
% sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
remove.packages(rJava)
remove.packages(Rweka)
From the terminal enter this command:
%sudo R CMD javareconf
install.packages("rJava", dependencies = TRUE, type = "source")
install.packages("rJava", dependencies = TRUE, type = "source")
Load your library("rJava", "RWeka")
Best of luck.
Maybe another simple answer which does not touch your filesystem:
$ install_name_tool -add_rpath /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/server /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so
(Substitute jdk1.8.0_131.jdk with your JDK path.)
Check the location of libjvm.dylib file.
Try this, in my case this worked:
dyn.load('/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home/lib/server/libjvm.dylib')
library(rJava)
I went through all the troubleshooting I could find, then installed jdk-11.0.1_osx-x64_bin.dmg from: Oracle downloads
Everything worked perfectly after that.
Check the version of the library that was not loaded when you run library('rJava') and match it to the java version you need installed.
I noticed that 'rJava' is pretty strict about JDK version while loading in Rstudio. In my case (Mac OS. 10.14.6 - Mojave) I had installed jdk-13 which was tested against jdk-11.
I have linked my JDK version to a non-existent directory that was assumed to be valid by Rstudio (JDK 11):
sudo ln -sf /Library/Java/JavaVirtualMachines/jdk-13.jdk/ /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk
and suprisingly it works like a charm...
BTW., there was no problem to run 'rJava' following the previous instructions in native R.
Here you can download the legacy Java version 6 for El Capitan: https://support.apple.com/kb/DL1572?viewlocale=en_US&locale=en_US
After doing so, the rJava package works for me.
This line below has solved the same problem I was having with rJava package as some others in this discussion did. I'm sure there are more than one solution to this problem and I sincerely thank them all for their contributions because sometimes one line like the one below saves a lot of time from going to trash!
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
Run the following on the command line: sudo R CMD javareconf
Several solutions above have mentioned this, but they also suggest that one remove and subsequently re-install the rJava package. I found those additional steps to be unnecessary.
You can change the reference in R by using the following command:
sudo install_name_tool -change /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home/lib/server/libjvm.dylib /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/rJava.so

How to set up conda-installed R for use with RStudio?

I've been trying to set up my R using conda (eventually to use with Beaker Notebook) and I want to be able to use RStudio with my conda-installed version of R.
My method of installing R:
conda install -c r r
conda install -c r r-essentials
conda install -c r r-rserve
conda install -c r r-devtools
conda install -c r r-rcurl
conda install -c r r-RJSONIO
conda install -c r r-jpeg
conda install -c r r-png
conda install -c r r-roxygen2
conda install --channel https://conda.anaconda.org/bioconda bioconductor-edger
I ran that version of R (I only installed this version)
> version
_
platform x86_64-apple-darwin11.0.0
arch x86_64
os darwin11.0.0
system x86_64, darwin11.0.0
status
major 3
minor 3.1
year 2016
month 06
day 21
svn rev 70800
language R
version.string R version 3.3.1 (2016-06-21)
nickname Bug in Your Hair
Running R in Jupyter is kind of buggy. For example, when it outputs errors, it outputs to stdout and splits every character in the string with a linebreak. I want to use RStudio but I don't want to install another version of R.
How can I route my conda version of R into RStudio?
Here's my .bash_profile not sure if this will be useful:
$ cat ~/.bash_profile
# added by Anaconda3 4.0.0 installer
export PATH="/Users/jespinoz/anaconda/bin:$PATH"
export RSTUDIO_WHICH_R=/Users/jespinoz/anaconda/bin/R
I've been trying to follow these tutorials but I am lost. I'm really not too familiar with environment variables and such things.
(1) https://support.rstudio.com/hc/en-us/community/posts/207830688-Using-RStudio-with-conda
(2) Launch mac eclipse with environment variables set
when I looked for my R it directed me to:
$ which R
/Users/jespinoz/anaconda/bin/R
but the directions from (1) is using this path which is very confusing:
/Users/jespinoz/anaconda/lib/R/bin/R
I tried doing what this guy did and added this to my .bash_profile but it didn't work. I even made a .bashrc but it still didn't work (I sourced both after I added the lines)
export RSTUDIO_WHICH_R=/Users/jespinoz/anaconda/bin/R
How to tell RStudio to use R version from Anaconda
Unfortunately, anaconda has no tutorial for this in https://docs.continuum.io/anaconda/ide_integration
See https://anaconda.org/r/rstudio:
$ conda install -c r rstudio
Then from command line:
$ rstudio
(It is how I installed it and it works.)
So long as which R shows up a working R interpreter (which it should do if you have installed the r package from conda and activated your environment) then launching rstudio from that same environment should pick it up just fine.
For a test, on ArchLinux, I built and installed: https://aur.archlinux.org/packages/rstudio-desktop-git/
.. then force removed the R interpreter (pacman -Rdd r), then installed r from conda (conda install -c r r) and it worked fine. I then closed my terminal and opened a new one (so that the correct conda environment was not activated and successfully launched RStudio with the following command: RSTUDIO_WHICH_R=/home/ray/r_3_3_1-x64-3.5/bin/R rstudio
I think the crux is to launch RStudio from the right environment? Your ~/.bash_profile and ~/.bashrc are only sourced when you run bash. For environment variables to be set so that the your desktop environment knows about them, on Linux, you should put them in ~/.profile or else in /etc/pam.d (you may need to logout or shutdown after making those changes) and on OS X, you should check out https://apple.stackexchange.com/q/57385
Update: ADD THIS TO ~/.bash_profile !
export RSTUDIO_WHICH_R="/Users/jespinoz/anaconda/bin/R"
launchctl setenv RSTUDIO_WHICH_R $RSTUDIO_WHICH_R
Credits to #Z-Shiyi for the last line https://github.com/conda/conda/issues/3316#issuecomment-241246755
An addition to what #Ray Donnelly said above. Basically, it has to be executed from the correct environment (i.e. run it from the terminal).
You can either:
(A) Put this in your ~/.bash_profile
export RSTUDIO_WHICH_R=/Users/[yourusername]/anaconda/bin/R (if youre using conda but you could put any R path)
(B) then type this in the terminal after it's been sourced (either restart terminal or do source .bash_profile): open -a RStudio
That should work.
or you can do what I did:
(A) open up automator (sorry if you're not on a mac; this will only work on mac)
(B) use a Run Shell Script
(C) then delete cat that's already in there and put in:
export RSTUDIO_WHICH_R=/Users/[yourusername]/anaconda/bin/R
open -a RStudio
(D) Save it as something like run_rstudio.app then just run that and it should work:
Launch RStudio from Activated Conda Environment
At least for Mac OS X, I find that it is sufficient to activate the environment in a shell session, then launch RStudio.
$ conda activate my_r_env
$ /Applications/RStudio.app/Contents/MacOS/RStudio
Once in R, one can verify that values of R.home() and .libPaths() point to the environment-specific locations.
The advantage here is that you aren't fixed to whatever was last set in the environment variables, e.g., via .bash_profile. Instead, one can have many R-based environments and switch between them (or run multiple ones simultaneously) without tampering with global settings.
Suggested Alias for Convenience
Perhaps the only global setting I might recommend is to add an alias for rstudio to your .bash_profile so you don't have to type the full path every time, like
alias rstudio='/Applications/RStudio.app/Contents/MacOS/RStudio &'
which enables one to then do
$ conda activate my_r_env
$ rstudio
$
where the & enables one to continue using the shell, or close it, without affecting the RStudio instance.
Update: The Anaconda Distribution now has packages for RStudio so you should be able to use that and not have to jump through any hoops at all. You can also install it directly the Anaconda Navigator.
Making a soft link works for me:
ln -s /opt/miniconda3/envs/r-4.2/bin/R /usr/bin/R
if is up to any good (now)... conda has the package rstudioapi which brings Rstudio to your local environment, and picks up the local/default r-base installed of your active environment.
you can install it (once your environment is activated) by typing:
conda install -c conda-forge r-rstudioapi
then you just type (inside your environment): rstudio

How to specify lib directory when installing development version R Packages from github repository

In Ubuntu, I am installing all the R packages in the directory, /usr/lib/R/site-library by specifying lib option in install.packages().
But when I try to install the development version of the R packages using, install_github(), it always installs in a local repository of the system user.
.libPaths() has 4 directories including the local repository. So, I have 2 questions,
Will it install in any of the other 3 repositories if i remove the local repository from .libPaths()?
Is there any way to specify installation library path in install_github()?
I am using Ubuntu 12.04 64bit and R 3.0.1
----------------------UPDATE--------------------------------
Unable to remove the local repository from .libPaths()
If I try to install using install_github() in RStudio, it installs in the local repository since lib is not specified.
If I try to install using install_github() as non-root user, it installs in the local repository since lib is not specified.
If I try to install using install_github() as root user, it installs in the /usr/local/lib/R/site-library since lib is not specified.
Is there any to specify installation lib?
To add specified library paths in devtools, we need to use with_libpaths()
Arguments for with_libpaths() are, with_libpaths(new, code)
Following is an example for using with_libpaths(),
library(devtools)
with_libpaths(new = "/usr/lib/R/site-library/", install_github('rCharts', 'ramnathv'))
Courtesy: Hadley, here :)
And other than with_libpaths(), there are more options for in devtools::with_something()
in_dir: working directory
with_collate: collation order
with_envvar: environmental variables
with_libpaths: library paths, replacing current libpaths
with_lib: library paths, prepending to current libpaths
with_locale: any locale setting
with_options: options
with_path: PATH environment variable
with_par: graphics parameters
More explanations here
install_github takes a ... argument that passes to devtools::install. devtools::install has an args argument.
args
An optional character vector of additional command line arguments to be passed to R CMD install. This defaults to the value of the option "devtools.install.args".
R CMD install takes a library argument
Options:
-h, --help print short help message and exit
-v, --version print INSTALL version info and exit
-c, --clean remove files created during installation
--preclean remove files created during a previous run
-d, --debug turn on debugging messages
and build a debug DLL
-l, --library=LIB install packages to library tree LIB
So the following should work:
devtools::install_github("repo", args = c('--library="./mypath/gdfgdg/"'))
however it doesnt appear to be replacing the call to R CMD install
"C:/PROGRA~1/R/R-31~1.0/bin/x64/R" --vanilla CMD INSTALL \
"C:\Users\john\AppData\Local\Temp\RtmpucrXMD/RSelenium_1.3.2.tar.gz" \
--library="C:/Users/john/Documents/R/win-library/3.1" --install-tests \
--library="C:/Users/john/Desktop/"
This is more of a workaround, but I found a way using the command-line version of R.
Starting from Ubuntu:
sudo -i R
the trick (I found) is to use -i option
Then from R:
.libPaths()
my local R directory does not appear; the default directory is the one that I want.
Then, I install.packages() or install_github() with impunity.
Hope this helps,
Ian

Resources