Tensorflow works in R command line but not Rstudio - r

I have installed the tensorflow package.
The following commands work fine in R command line
library(tensorflow)
sess = tf$Session
However when I run the same 2 linesin Rstudio I get the following error
Error: Python module tensorflow was not found
Detected Python configuration:
...
numpy: [NOT FOUND]
I suspect its numpy related but I have no idea.
This is my Rstudio information
rstudio::versionInfo()
$version
[1] ‘0.98.1103’
$mode
[1] "desktop"
Do I need to use a different version of Rstudio or is there more setup required to get tensorflow to work?
A bit more details after reading
https://github.com/rstudio/tensorflow/issues/87
import('numpy')
Error in py_module_import(module, convert = convert) : ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: The specified path is invalid.
**************** FINAL EDIT *********************
I got this to work. But I have no idea why or how.
During install_tensorflow() in Rstudio, you are prompted to add the following to your PATH so I did
usr\appdata\local\programs\python\python36\Scripts
I removed it from my PATH and successfully ran tensorflow in Rstudio.
However, I added it back subsequently and could not replicate the error.
In the end I still don't know what is broken in Rstudio or what fixed it

Please use the following command to reload dynamic library paths
sudo ldconfig

Related

Error encountered with installing Tensorflow within R on linux

I am try to install tensorflow within R and subsequently a package called "cellassign". I have installed tensorflow, reticulate, and keras with the following command and they all seem to be installed without any hiccups. I am also able to load the packages after installing.
devtools::install_github("rstudio/reticulate")
devtools::install_github("rstudio/tensorflow")
devtools::install_github("rstudio/keras")
However, I keep encountering the following issue when trying to use any command within tensorflow or keras package.
tensorflow::install_tensorflow()
Error: 'activate' is not a conda command.
Did you mean "source activate /home/users/ntu/ling0086/.conda/envs/r_env" ?
+ . /app/anaconda/3/bin/activate
+ conda activate '/home/users/ntu/ling0086/.conda/envs/r_env'
Also ran into another error if I tried using the following
> tensorflow::tf_config()
Python environments searched for 'tensorflow' package:
/home/users/ntu/ling0086/.conda/envs/r_env/bin/python3.10
/usr/bin/python
Python exception encountered:
ModuleNotFoundError: No module named 'six'
Any idea why this might be so? I am not familiar with linux-environment variables and I do not have sudo privilege. I was told that my "R" is loaded in a virtual environment and this might affect the path finding to the right packages. But I am unsure as to how I should rectify the above error. Any help would be deeply appreciated.
Not sure if this is of any help, but how I would normally run R entails:
module load anaconda/3
source activate r_env
R

How to install package keras in R

I'm trying to install deep learning package keras on RStudio using this website. I installed keras using
install.packages("keras")
library(keras)
install_keras()
but when I tried to open the MNIST dataset
mnist <- dataset_mnist()
I keep getting the error
Error: ModuleNotFoundError: No module named 'absl'
I thought keras installed tensorflow but do I need to install tensorflow separately?
I had the same problem and it is solved by installing the package in two steps:
install keras: install.packages("keras")
keras::install_keras()
There you go!
If you follow the TUT and still got error, try running py_config() and check the python and libpython if it is pointing to an r-tensorflow environment. If not, best to try manually install keras in your manually set up conda environment.
Step 1: Install keras in your R just like in the link above.
#Open rstudio and run the following command
devtools::install_github("rstudio/keras")
#Don't close rstudio after running this, okay?
Step 2: Manually install keras (and tensorflow) in your machine ##. When i say “manual” it means using python specifically through conda. Here’s the link I followed: https://medium.com/i-want-to-be-the-very-best/installing-keras-tensorflow-using-anaconda-for-machine-learning-44ab28ff39cb .
In summary, the link will teach you to install anaconda, create an environment and install necessary libraries. Just follow it. I named my environment as “r-tensorflow” because that is the name of the environment that the install_keras() in R will do :)
Step 3: Point rstudio to use the python in your newly created environment using use_python() function
Open your rstudio (if you close it after following step 1) and type the following code
library(keras)
library(reticulate)
# in case you run into error run this : reticulate::py_discover_config("keras")
use_python("<yourpath>/Anaconda3/envs/r-tensorflow/Scripts/python.exe")
# change <yourpath> approriately
# write all the codes for building model in keras (or tensorflow) e.g. mnist<-dataset_mnist()
Important note on Step 3: If you still got the "not found module" after following step 3, you must start a new fresh R session and ensure to delete the workspace (.RData) because more likely your current script will still use the old python configuration though you used use_python
I had the same problem, but mine was solved by enclosing keras in double quotes.
install.packages("keras") ## worked for me,
install.packages(keras) ## never worked.
try:
install.packages("devtools")
devtools::install_github("rstudio/keras")
library(keras)
mnist<-dataset_mnist()
Please install "reticulate" library using command install.packages("reticulate") and then load using library(reticulate)
then install absl using command
conda_install('r-tensorflow','absl-py')

Error installing and running rcpp

I'm pretty new to R so apologies for a stupid question. I'm trying to get rcpp running but I'm stuck in an endless loop of R asking me to re-install RTools.
I broadly followed the code in this blog post, although first time off I installed everything by hand & I've subsequently re-installed everything a few times over. I'm running Windows 7, R version 3.1.2, R Studio Version 0.98.1091 (not that this should matter much) and RTools 3.1.
An edited highlight of what my console looks like is as follows:
> library(installr)
Welcome to installr version 0.15.3...
> install.Rtools()
Loading required package: devtools
No need to install Rtools - You've got the relevant version of Rtools installed
> find_rtools()
[1] TRUE
> library(Rcpp)
> evalCpp("1+1")
Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput, :
Error 65535 occurred building shared library.
At which point a dialog box pops up saying:
Install Build Tools
Compiling C/C++ code for R requires installation of additional build tools.
Do you want to install the additional tools now?
And then I get directed to download and re-install RTools 3.1 from cran all over again.
I've seen that this can be an issue with the PATH variable but I've tried various things including:
Nothing (extra) in the PATH variable
Including both references to R (C:\Program Files\R\R-3.1.2\bin\x64) and RTools (C:\RBuildTools\3.1\bin;C:\RBuildTools\3.1\gcc-4.6.3\bin;) in the PATH. Once with RTools first, once with R first
Including just a reference to RTools in the PATH as the initial install file directed me to do.
Any ideas on things to try would be gratefully accepted!
EDIT
Following Dirk's comment it looks like I might have a problem with how RTools is installed. I've followed the instructions from several blogs / tutorials on how to install RTools; all to no avail (yet!)
This GitHub page gives some instructions on how install and check that the installation has worked. I've followed all the checks (see below for copy of the console) and it looks like I have a working install of RTools, but when I try to run evalCPP() again I get the same error as before directing me to install RTools.
> Sys.getenv('PATH')
[1] "C:\\Program Files\\R\\R-3.1.2\\bin\\x64;C:\\RTools\\bin;C:\\RTools\\gcc-4.6.3\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Enterprise Vault\\EVClient\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\VSShell\\Common7\\IDE\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\100\\Tools\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\DTS\\Binn\\;C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\Common7\\IDE\\PrivateAssemblies\\;C:\\Program Files\\Microsoft SQL Server\\110\\Tools\\Binn\\;C:\\Program Files\\Microsoft\\Web Platform Installer\\;C:\\Program Files (x86)\\Microsoft SDKs\\TypeScript\\1.0\\;C:\\Program Files\\Microsoft SQL Server\\100\\DTS\\Binn\\"
Warning message:
printing of extremely long output is truncated
> system('g++ -v')
Using built-in specs.
COLLECT_GCC=C:\RTools\GCC-46~1.3\bin\G__~1.EXE
COLLECT_LTO_WRAPPER=c:/rtools/gcc-46~1.3/bin/../libexec/gcc/i686-w64-mingw32/4.6.3/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: /data/gannet/ripley/Sources/mingw-test3/src/gcc/configure --host=i686-w64-mingw32 --build=x86_64-linux-gnu --target=i686-w64-mingw32 --with-sysroot=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/mingw32 --prefix=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/mingw32 --with-gmp=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/prereq_install --with-mpfr=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/prereq_install --with-mpc=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/prereq_install --disable-shared --enable-static --enable-targets=all --enable-languages=c,c++,fortran --enable-libgomp --enable-sjlj-exceptions --enable-fully-dynamic-string --disable-nls --disable-werror --enable-checking=release --disable-win32-registry --disable-rpath --disable-werror CFLAGS='-O2 -mtune=core2 -fomit-frame-pointer' LDFLAGS=
Thread model: win32
gcc version 4.6.3 20111208 (prerelease) (GCC)
> system('where make')
C:\Rtools\bin\make.exe
I've also had a look at Appendix D of R Installation and Admin. I can't see anything in here I've not already tried except for section D.4 which implies I might need to rebuild rcpp from source using my installed RTools. I don't have time to do this right away but will give it a try unless people say this route is not worth my time.
EDIT v2
So I tried building rcpp from source ... and that didn't work either. I downloaded both the package source and windows binaries from the CRAN Rcpp package page. In the R console I now get:
> install.packages(pkgs = "C:/Rcpp_0.11.4.tar.gz", repos = NULL, contriburl = NULL, type = "source", verbose = TRUE)
Installing package into ‘C:/Users/james.macadie/Documents/R/win-library/3.1’
(as ‘lib’ is unspecified)
system (cmd0): C:/PROGRA~1/R/R-31~1.2/bin/x64/R CMD INSTALL
Warning in install.packages :
package ‘C:/Rcpp_0.11.4.tar.gz’ is not available (for R version 3.1.2)
I guess my active questions now boil down to:
Reading the whole of the post above can anyone tell me how to fix things so it just works? Or, failing that...
What tests can I run to check I really do have Rtools installed correctly? Everything I have found on the internet suggests I do: system('where make') etc. However, the evidence of being unable to run evalCpp or other rccp functions suggests I don't.
What am I doing wrong when building from source? Should I try the command line option?
EDIT v3
Running evalCpp with showOutput= TRUE and verbose = TRUE I think I've tracked the error back to R CMD SHLIB not working. I followed this blog, which shows how to work with the command line R directly. However when I get to the line R CMD SHLIB sequence_examples.c the execution just skips straight to the next command line without doing anything, generating any files in the directory or throwing any errors. I tried running the --help options at the command line but get the same sort of error:
C:\Users\james.macadie> R --help
Or: R CMD command args
where 'command' is one of:
INSTALL Install add-on packages
REMOVE Remove add-on packages
SHLIB Make a DLL for use with dynload
BATCH Run R in batch mode
build Build add-on packages
check Check add-on packages
Rprof Post process R profiling files
Rdconv Convert Rd format to various other formats
Rdiff difference R output files
Rd2pdf Convert Rd format to PDF
Rd2txt Convert Rd format to pretty text
Stangle Extract S/R code from vignette
Sweave Process vignette documentation
config Obtain configuration information about R
open Open a file via Windows file associations
texify Process a latex file
Use
R CMD command --help
for usage information for each command.
C:\Users\james.macadie> R CMD SHLIB --help
C:\Users\james.macadie>
N.B. for people reading the earlier code samples higher up this post I have changed a few things since those code snapshots:
I've installed R directly into C:\R. It used to be in C:\Program Files\R\ but as has been suggested file paths with spaces in can cause problems
I'm referencing Rtools under C:\Rtools\ and not C:\RBuildTools\
Thanks for any suggestions, as ever
Had the same endless loop issue when trying to install Twitter's BreakoutDetection (which is also written in cpp)
fixed by executing the following
Sys.setenv(PATH="%PATH%;C:/Rtools/gcc-4.6.3/bin;c:/Rtools/bin")
and then answering "no" when presented with the following prompt:
"Install Build Tools Compiling C/C++ code for R requires installation of additional build tools. Do you want to install the additional tools now?"
Didn't try these actions independently so not sure if either on their own would have fixed the issue
I found that ensuring all of these were in my path fixed it. I did this with RStudio closed; I didn't reboot after.
C:\Program Files\R\R-3.1.3\bin\x64
C:\Program Files\R\R-3.1.3\bin
C:\RBuildTools\3.2\bin
C:\RBuildTools\3.2\gcc-4.6.3\bin64
C:\RBuildTools\3.2\gcc-4.6.3\bin
C:\RBuildTools\3.2\gcc-4.6.3\i686-w64-mingw32\bin
These are on my Win7-64bit computer. YMMV, and I'm mostly posting this to ensure others see it if they are having the same issue.
In the end it was something a bit left-field. Inspired by the following post, I had a look at the ComSpec environment variable. Not quite sure how, but I had it set to "cmd.exe".
Removing the double quotes, so it said cmd.exe, and then rebooting fixed everything.
Thanks to all who've tried to help.
I experienced the same problem, I fixed this problem by adding Rtools dir into env variables:
Sys.setenv(BINPREF = "C:/Rtools/mingw_$(WIN)/bin/")

How to import .ods?

I'm trying to import data (.ods file) and I know how to import .csv, .xls etc. but I strand all the time. I tried it with the package Gnumeric but R tells me that the required program 'ssconvert' cannot be found.
I wrote a package called readODS
which has a function read.ods that returns ods files as data.frames
(a list of data.frames, one per sheet)
It is available on CRAN, see here!
Source and bug reports here:
https://github.com/chainsawriot/readODS
I had the exact problem on Ubuntu Linux. It's frustratingly obvious:
sudo apt-get install gnumeric
As it happens, I had a bunch of obsolete packages, and went thru a bunch to update them. The gnumeric library requires the XML library, and my installation of XML library was built on 2.14, and my R wouldn't load anything built before 3.0. I don't think that is your issue, but if this solution above doesn't work, maybe you could also check that your packages are up-to-date. Anything I installed via sudo had to be removed via sudo, or I got permissions errors.
It's frustratingly obvious, because the error message is
Error in read.gnumeric.sheet(file = fname, head = TRUE, sheet.name = "Sheet1") :
Required program 'ssconvert' not found.
To emphasize it, the package documentation at http://cran.at.r-project.org/web/packages/gnumeric/gnumeric.pdf says
Requires an external program, 'ssconvert' (normally installed with 'gnumeric' (http://projects.gnome.org/gnumeric/) in 'PATH'.
How do you get ssconvert? Again, frustratingly obvious: it's normally installed with gnumeric. Go to the command line, and type ssconvert, and it'll remind you that:
user#Computer:~$ ssconvert
The program 'ssconvert' is currently not installed. You can install it by typing:
sudo apt-get install gnumeric
I checked, ssconvert isn't in the dependency packages (like gnumeric-common), it's in gnumeric proper, so you have to install the complete gnumeric package:
sudo apt-get install gnumeric
I haven't tried it but the ROpenOffice package looks like it might do the job: http://www.omegahat.org/ROpenOffice/

Not able to Install R package "Rgraphviz" on Mac OS X Lion

I have spent a lot of time trying to solve this, but still no luck. I am trying to get Rgraphviz to work with R 2.14.1 on a Max OS X Lion
These are the steps I took in order to install and use Rgraphviz on Mac OS X Lion:
Installed graphviz via: sudo port install graphviz
Checked that the installation was successful : pkg-config --modversion libgvc
Result: 2.28.0 <- That means its OK
Under R 2.14.1
source("http://www.bioconductor.org/biocLite.R")
biocLite("Rgraphviz")
Whenever I try to use the library I get the following error
Error : .onLoad failed in loadNamespace() for 'Rgraphviz', details:
call: value[[3L]](cond)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/2.14/Resources/library/Rgraphviz/libs/i386/Rgraphviz.so':
dlopen(/Library/Frameworks/R.framework/Versions/2.14/Resources/library/Rgraphviz/libs/i386/Rgraphviz.so, 6): Library not loaded: /usr/local/lib/libgvc.5.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/2.14/Resources/library/Rgraphviz/libs/i386/Rgraphviz.so
Check that (1) graphviz is installed on your system; (2) the installed version of
graphviz matches '2.26.3'; this is the version used to build this Rgraphviz
package; (3) graphviz is accessible to R, e.g., the path to the graphviz 'bin'
directory is in the system 'PATH' variable. See additional instructions in the
'README' file of the Rgraphviz 'source' distribution, available at
Some things I notice
The graphviz suggested in the "help" file is 2.26.3 and the graphviz installed by Mac Ports is 2.28.0, but as it is a newer version I don't think this makes a difference, and besides I can not find v 2.26.3
Suggestion number (3) seems to be the key, but I can't find/don't have a bin directory under graphbiz
Also when I run a locate Rgraphviz.so there are no results.
Any clue towards a solution will be much appreciated,
Thanks a lot!
Thanks for the final answer, the installation seems to work for me! (I still have to check that the package runs properly). Just a quick observation: Graphviz was installed in /usr/local on my machine so that the above command had to be entered as follows:
sudo R CMD INSTALL --configure-args='--with-graphviz=/usr/local' Rgraphviz_1.32.0.tar.gz
One more fail point. If you used Fink to install graphviz, you may still, like me, encounter separate build errors even after you use the configure arguments to point to graphviz, which now look like:
R CMD INSTALL --configure-args='--with-graphviz=/sw' Rgraphviz_1.34.0.tar.gz
The installation will fail during build errors for LL_funcs.c because it is looking for libraries that do not install unless you ALSO install graphviz-dev. You will see this:
In file included from LL_funcs.c:1:
common.h:31:17: error: gvc.h: No such file or directory
common.h:32:22: error: gvplugin.h: No such file or directory
common.h:35:20: error: gvcext.h: No such file or directory
common.h:39:20: error: gvcjob.h: No such file or directory
followed by many more build errors of course. Just be sure to
fink install graphviz-dev

Resources