Rscript is not recognising libraries when using renv - r

I am working on Mac OSX, using v3.6.3 of R and using renv. In Rstudio and R, I can load the libraries of my installed packages, e.g library(ggplot2) works. However when I run a script using Rscript I get the message
Error in library(ggplot2) : there is no package called ‘ggplot2’
According to this SO answer, I need to make sure that the value of
Sys.getenv('R_LIBS_USER') in R.exe
is the same as the value of
Rscript.exe -e ".libPaths()"
But the value is the same, both are pointing to the renv-system-library in my project folder.
So how do I fix this?

It may be better to specify the lib.loc in library call
library(ggplot2, lib.loc = '/path/where/library/is/located')

I managed to solve this. akruns answer was useful, it did not work, but pointed me incorrect direction. The answer did not work because using it, I received the following error:
Error: package or namespace load failed for ‘ggplot2’:
.onLoad failed in loadNamespace() for 'pillar', details:
call: utils::packageVersion("vctrs")
error: there is no package called ‘vctrs’
Now vctrs was in the '/path/where/library/is/located' so I think dependent packages were not being loaded from that path but the default for Rscript. Putting a print(.libPaths() in the script gave
"/usr/local/Cellar/r/3.6.3_1/lib/R/library"
instead of
[1] "/Users/Chris/Sites/app_name/renv/library/R-3.6/x86_64-apple-darwin18.7.0"
[2] "/private/var/folders/5_/p_yl0439059b7_jdqzrm0hr40000gr/T/RtmptdHcWN/renv-system-library"
for .libPaths() in Rstudio. Looking at the ruby program that was actually running the Rscript program, I found it was being run with the --vanilla option, i.e
Rscript --vanilla script_name
Removing the --vanilla option fixed the problem. I think that scripts with the --vanilla option stopped working because I reinsalled R using brew to fix another problem I was having and as part of this issued this command:
brew link --overwrite r

Related

syntax error near unexpected token - R package Install

I am trying to install an R package along with its dependencies. But it is throwing error.
$ install.packages(rvest_0.3.5.tar.gz, dependencies=True)
-bash: syntax error near unexpected token `rvest_0.3.5.tar.gz,'
I am new to R please help me how can I download this along with it dependencies.
Before this, I tried following
$ R CMD INSTALL rvest_0.3.5.tar.gz
* installing to library ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library’
ERROR: dependencies ‘httr’, ‘magrittr’, ‘selectr’ are not available for package ‘rvest’
But it failed with dependent packages are missing error. And obviously it is cumbersome to install the dependent packages manually. Hence I tried package.install
You need to run it with quotes and capital TRUE:
install.packages("rvest_0.3.5.tar.gz", dependencies = TRUE)
Note this will only work if you have unix-like system and the file is located in your current working directory (check with running getwd() from your R session). Otherwise you need to provide full path to the file (like "~/somefolder/mydata/rvest_0.3.5.tar.gz").
If you run Windows, then you need .zip file instead of .tar.gz.
If you are connected to internet, just run:
install.packages("rvest")

Issue with loading Tidyverse in RStudio

How to do i resolve the following code error?
library(tidyverse)
Error: package or namespace load failed for ‘tidyverse’:
.onLoad failed in loadNamespace() for 'tidyselect', details:
call: is_string(x)
error: object 'rlang_is_string' not found
In addition: Warning message:
package ‘tidyverse’ was built under R version 3.5.3
For persistent errors of type, first, ensure you are working with the latest version of R. The installr package is a very convenient way to do this.
Then, start new R session (ideally, not in RStudio).
Uninstall tidyverse, tidyselect, and rlang
# if you are using multiple libraries, you may need to specify libpath,
# using the following: lib="~/R/win-library/3.6"
# you can check using the .libPaths() command
remove.packages("rlang")
remove.packages("tidyselect")
remove.packages("tidyverse")
and, reinstall them one by one with dependencies = TRUE
install.packages("rlang", dependencies = TRUE)
install.packages("tidyselect", dependencies = TRUE)
install.packages("tidyverse", dependencies = TRUE)
That should do it.
I also encounter a similar problem like you, which I'm unable to load the tidyverse too.
Hope this discuss from the tidyverse github issues maybe relevant to you.
https://github.com/tidyverse/googledrive/issues/275
Here's one of our typical explanations of this. Note that this is not specific to googledrive or rlang. It's an R + Windows gotcha. I suspect for you is rlang (at least).
Please note that on Windows, it is very important to quit or restart
all R processes before upgrading , because if any R process has
loaded, it will keep the .dll file open and the installer will not be
able to overwrite .dll. The error message when this happens is very
easy to overlook, and the new version will be partially installed: the
package description and R code will be updated but the compiled code
(in .dll) will not.

libicu and stringi on Fedora 24 causing R headaches

I recently upgraded to F24, and now in my R session I cannot get a few packages to load, sp. reshape2, latex2exp, knitr, and others.
The initial problem I found was that F24 uses libicu56 whereas these packages expect libicu54. I followed a suggestion in this thread to set the symbolic links with the desired version:
ln -s /usr/lib64/libicui18n.so.56 /usr/lib64/libicui18n.so.54
ln -s /usr/lib64/libicuuc.so.56 /usr/lib64/libicuuc.so.54
ln -s /usr/lib64/libicudata.so.56 /usr/lib64/libicudata.so.54
That initial error went away, but now I have this:
Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/home/uname/R/x86_64-redhat-linux-gnu-library/3.3/stringi/libs/stringi.so':
/home/uname/R/x86_64-redhat-linux-gnu-library/3.3/stringi/libs/stringi.so: undefined symbol: _ZTIN6icu_548ByteSinkE
This leads me to the stringi package for R, but I cannot get it to load - it gives the same error.
I have updated F24 and all the R packages as well.
Any ideas?
I was able to install the package stringi on fedora 24 by downloading the tar.gz package from CRAN and then run the following command:
R CMD INSTALL stringi_1.1.1.tar.gz --configure-args='--disable-pkg-config'
That just happened to me following an update of icu (Gentoo). Another solution is to remove and install again stringi, as it is looking for a specific library file that does not exist anymore.
remove.packages('stringi')
install.packages('stringi')
If your .Rprofile triggers library(stringi), then you must start a session using R --vanilla in order to do this, else it will keep failing.
The other solution with --disable-pkg-config works as well. It will make stringi build icu for itself rather than rely on the system's (the source ships with a copy of icu).

require(RQuantLib) fails

I am trying to load RQuantLib but I get the following error:
> require(RQuantLib)
Loading required package: RQuantLib
Error : .onLoad failed in loadNamespace() for 'RQuantLib', details:
call: if (is.character(qc) && nchar(qc) > 1) {
error: missing value where TRUE/FALSE needed
In addition: Warning message:
running command 'bash -c 'type -p quantlib-config' 2>/dev/null' had status 1
I am pretty new to programming in general so I am not sure what this means. I am working in an Mac OS Maverick environment, I downloaded the latest version of RQuantLib (0.3.12) and the "R Package Installer" says it is installed. (I installed RQuantLib from the terminal using "R CMD INSTALL RQuantLib" as using install.packages() from within R did not work - it gave me an error saying QuantLib was not configured, although it was running fine in from the terminal.) I am running R from the R console. QuantLib works fine and so does Rcpp.
I checked the "NAMESPACE" document in the RQuantLib folder and it says:
import(methods)
importFrom(Rcpp, Rcpp.plugin.maker)
useDynLib(RQuantLib)
exportPattern("*.default")
export(
##--arrays.R
"oldEuropeanOptionArrays",
"EuropeanOptionArrays",
"plotOptionSurface",
##--asian.R
"AsianOption",
##--bermudan.R
"BermudanSwaption",
"summary.G2Analytic",
"summary.HWAnalytic",
"summary.HWTree",
"summary.BKTree",
##--bond.R
"ZeroCouponBond",
"ZeroPriceByYield",
"ZeroYield",
"FixedRateBond",
"FixedRateBondYield",
"FixedRateBondPriceByYield",
"FloatingRateBond",
"ConvertibleZeroCouponBond",
"ConvertibleFixedCouponBond",
"ConvertibleFloatingCouponBond",
"CallableBond",
"FittedBondCurve",
##--calendars.R
"isBusinessDay", "businessDay",
"isHoliday",
"isWeekend",
"isEndOfMonth",
"getEndOfMonth", "endOfMonth",
"adjust",
"advance",
"businessDaysBetween",
"getHolidayList", "holidayList",
"setCalendarContext",
##--dayCounter.R
"dayCount",
"yearFraction",
"setEvaluationDate",
##--discount.R
"DiscountCurve",
"plot.DiscountCurve",
##--implied.R
"EuropeanOptionImpliedVolatility",
"AmericanOptionImpliedVolatility",
"BinaryOptionImpliedVolatility",
##--option.R
"EuropeanOption",
"AmericanOption",
"AmericanOption.default",
"BinaryOption",
"BarrierOption"
)
S3method("plot", "Option")
S3method("print", "Option")
S3method("summary", "Option")
S3method("plot", "Bond")
S3method("print", "Bond")
S3method("summary", "Bond")
If I try to specify the path where the Terminal wrote that it installed RQuantLib while requiring it I get the following error:
> require('RQuantLib', lib.loc = "C:/Users/dealmer/Library/R/3.1/library/RQuantLib/libs")
Loading required package: RQuantLib
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
no library trees found in 'lib.loc'
So, do I need to move something from there to where the other R libraries are? (There is already a folder named "RQuantLib" in the folder where the other R library folders are, that's where I found the NAMESPACE doc for example) The path that the Terminal gave me I can't follow in the Finder window however because it doesn't show the "Library" folder and I don't know how to make it do that.)
This is what .libPaths() gives me:
> .libPaths()
[1] "/Users/dealmer/Library/R/3.1/library"
[2] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library"
The R libraries are in [2].
I've tried googling but I can't find anything that seems helpful. Any help will be very appreciated. Thank you
- Dom
I think the issue is that on Linux we have quantlib-config in the $PATH:
edd#max:~$ quantlib-config --help
Usage: quantlib-config [--prefix[=DIR]] [--exec-prefix[=DIR]] [--version] \
[--libs] [--cflags]
edd#max:~$ quantlib-config --version
1.4
edd#max:~$
I don't own an OS X box, so I depend on someone like you help with a better configure setup. It should just work if you copy (or soft-link) quantlib-config into your path (eg /usr/local/bin), or adjust $PATH otherwise to include the directory it is in.
I had the same issue and after some hours of struggle I manage to use something not very rigorous but it works!
It seems that R has some issues evaluating system("bash -c 'type -p quantlib-config'").
After having installed QuantLib, typing the command bash -c 'type -p quantlib-config' in Terminal gives us the path of our quantlib-config which is for me /opt/local/bin/quantlib-config.
So I went back to my source folder for RQuantLib (version 0.4.0) and replaced qc in file R/inline.Rby:
qc <- as.character("/opt/local/bin/quantlib-config")
and rebuilt the package using the terminal command R CMD install RQuantLib/.
As I said, not very sexy but it works.
A restart of R and Rstudio is necessary.
Hope it helps!

Rscript issue - using different version of R?

I'm trying to load a library in an Rscript, but it's giving me a strange error. I'm running the 2.12.1 version of the Rscript binary, yet it complains that my package was built under version 2.12.1. Any idea what's going on here?
[17:55:13 trash] $ ./tmp.R
Loading required package: blah
Error: (converted from warning) package 'blah' was built under R version 2.12.1
[17:55:47 trash] $ cat tmp.R
#!/path/to/R/2.12.1/bin/Rscript --quiet
library(blah)
I figured it out with help from comments by #aL3xa and #Iterator. When I run whereis Rscript, I get:
Rscript: /usr/bin/Rscript /usr/bin/X11/Rscript
None of those represents the R installation I want to use (version 3.2.2 in this case), which would be located at
/myinstall/R-3.2.2/bin/
But if I run the Rscript command with the whole path, I force it to use the desired installation:
/myinstall/R-3.2.2/bin/Rscript tmp.R
which runs like a breeze.

Resources