Rstan not working with multi-core support - r

I installed R version 3.5.1 (on Windows 7 x64 desktop PC) following by Rtools 3.5 and rstan (via install.packages())
Rstan is failing with multi-core support like so:
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = 3) # or any number above 2
fit <- stan(file = '8schools.stan', data = schools_dat,
iter = 3, chains = 4)
with error:
Error in checkForRemoteErrors(val) :
4 nodes produced errors; first error: unable to load shared object 'C:/Users/ubashir/AppData/Local/Temp/RtmpIHGdm6/file194833bbb82.dll':
LoadLibrary failure: Access is denied.
However if i change the options line to:
options(mc.cores = 1) # or any other number up to 8
The code works. Anyone know a solution besides using only 1 core?
Thanks

There exist two relevant issues on the rstan GitHub project site: Issue #491 and Issue #492.
This issue seems to be related to an empty /etc/hosts file. I don't own a Windows machine, but this file seems to reside in c:\Windows\System32\Drivers\etc\hosts.
You need to make sure that the file contains a line:
127.0.0.1 localhost
If necessary edit and save the file, and try again. (You potentially might have to do the whole restart Windows shenanigans...)

Related

R/RStudio unable to run, with looping socketConnection error

A few days ago, I was having an error running models in R using 'brms', which said that my posterior samples didn't exist. Upon reading further, these links (1, 2, 3, 4) led me to think it was an rstan problem playing with my macOS (Catalina 10.15.6).
I followed their instructions, namely:
-updated packages Rcpp, rstan, arm, and brms
-followed these workaround instructions to alter the 'parallel' settings for stan: https://github.com/rstudio/rstudio/issues/6692
-updated R and RStudio, since this problem was supposedly fixed a few months ago with R 4.0
-updated XCode 11, Quartz 11, GNU Fortran 8.2
-updated latest macOS Catalina bug fixes
-ran sudo rm -rf [path to R] to uninstall R
-tried to do a thorough uninstall of all R and RStudio files, including deleting files in my Library/Frameworks folder, any .plist files in Library/Preferences, and any .Rprofile, .Rscript, .Rapp, .Rhistory, or .Renvirons files
-reinstalled R and RStudio after restart
Now, instead of having a "blank slate" to start from, I am experiencing some super weird behaviors. First, RStudio opens on a completely white blank screen and never loads. Second, when I try to open R directly either via terminal or with R Console, I get stuck in a loop for nearly 20 min that says:
Error in socketConnection("localhost", port = port, server = TRUE, blocking = TRUE, :
file descriptor is too large for select()
Calls: <Anonymous> ... makePSOCKcluster -> newPSOCKnode -> socketConnection
Execution halted
Error in socketConnection("localhost", port = port, server = TRUE, blocking = TRUE, :
cannot open the connection
Calls: <Anonymous> ... makePSOCKcluster -> newPSOCKnode -> socketConnection
In addition: Warning message:
In socketConnection("localhost", port = port, server = TRUE, blocking = TRUE, :
port 11537 cannot be opened
Execution halted
At the very end, when it finally stops looping forever, it says:
/Library/Frameworks/R.framework/Versions/4.0/Resources/bin/R: cannot make pipe for command substitution: Too many open files
ERROR: option '-e' requires a non-empty argument
rm: /var/folders/54/km__8z8x78x8_ct1pw8w8bbh0000gn/T//RtmpVORdTy: Too many open files
I can't access a console or enter anything in R to try to troubleshoot. Moreover, it causes a massive slowdown to my computer and Activity Monitor shows more than 150 'R' processes running, which don't go away after quitting R, only after using 'killall R' in Terminal.
However, someone in IT helped me determine that it's something in my Mac user library or preferences, because we created a brand new user on my machine, installed R and RStudio, and had no problems loading them.
I am just a psychology grad student, so I really don't understand the back end that makes R work and I am totally baffled by these symptoms.
I suspect that these links (5, 6, 7) might help, but I don't know how to execute the solutions because right now I can't enter or run anything in R without triggering that endless loop of 'Execution halted.'
I could really use a hand, thanks!

Running phylocom in R

I'm trying to perform a bladj in R language using the phylocom interface but, when I run (as in the package's example) the following commands
path <- "~/Mac/Courses_Rice/EcolJClub563/Fall10/phylometa_stuff/R_tutorial/bladjing/"
(tree2 <- rbladj(tree, path = path, fixroot = TRUE))
I only get the following message:
Error: phylocom not found - make sure your path is correct
I also downloaded the software into my computer and substitute the path with the path to the location of the executable, but I only get the same error.
I am running on Windows 10 (64 bits)
Can anybody tell me PLEASE how to fix it?
Best!
Paolo

Linking R to ImageMagick

Trying to use the awesome gganimate package in R, but was having trouble getting my animation to run. Reverted to trying to run the base example but couldn't get it to link up with ImageMagick.
Input:
library(gapminder)
b = ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year))+ geom_point() +scale_x_log10()
gg_animate(b)
And the output:
I cannot find ImageMagick with convert = "convert"
Error in file(file, "rb") : cannot open the connection
In addition: Warning messages:
1: running command 'C:\windows\system32\cmd.exe /c convert --version' had status 4
2: In find_magic() : ImageMagick not installed yet!
3: In im.convert(img.files, output = movie.name, convert = convert, :
Please install ImageMagick first or put its bin path into the system PATH variable
4: In normalizePath(path.expand(path), winslash, mustWork) :
path[1]="filed42411bd2b88.gif": The system cannot find the file specified
From the research I have done it seems that the issue has something to do with declaring convert.exe and the windows path, I'm just not sure what to fix to get this code to work.
I have installed ImageMagick-7.0.2-Q16 for Windows (I am running Windows 7).
What am I missing?
You don't need to declare convert.exe; Windows has a native cli tool with the same name to convert a FAT partition into an NTFS partition, not what you want.
(NB: I forget to which directory IM installs itself in windows, so please verify the path I provide before blindly using this code.)
If this is a one-time thing, you can make it available with something like:
Sys.setenv(PATH = paste("c:/Program Files/ImageMagick/bin",
Sys.getenv("PATH"), sep = ";"))
If you want a more permanent solution, you have two options:
Save the above code in a .Rprofile file, either in your home directory (affects all of your future R sessions) or in the directory of your R project needing this tool. (You either need to also run this code on the console or restart the R session for the change to be noticed in your code.)
Add it to the windows path. There are lots of places to find help with this, including https://stackoverflow.com/a/28545224/3358272.
Edit: it came to my attention that the windows install no longer provides the convenience applications (e.g., convert.exe, mogrify.exe) that the unixy install does. (I'm using IM on windows installed via msys2, so I guess it's preferring the unixy-method ... I should have known.) So that suggests a third option:
Since gapminder is using gganimate which is calling animation::im.convert, its source says you can specify the command executable via something like ani.options(convert = shQuote('c:/program files/imagemagick/magick.exe')).

Why R is unable to load a shared object?

I am trying to use XLConnect library in R. If I execute
library(XLConnect)
I get the following error message:
JAVA_HOME cannot be determined from the Registry
To resolve this problem I set first the JAVA_HOME variable:
Sys.setenv(JAVA_HOME='C:/Program Files (x86)/Java/jre1.8.0_65')
library(XLConnect)
It looks like it helps me to come further but then I get another problem:
unable to load shared object 'C:/Program Files/R/R-3.2.2/library/rJava/libs/x64/rJava.dll'
It wonder why R cannot load rJava.dll. At least this file is located in the folder where R searches for it:
C:\Program Files\R\R-3.2.2\library\rJava\libs\x64
ADDED
Please note that the rJava.dll file exists and it is located there, where R is searching for it. I guess that the problem is in incompatibility between 32bit and 64bit versions. I assume that because R complains:
% 1 is not a valid Win32 application
Well, why do R expect it to be a Win32 application`? First, my OS is 64bit, second my Java is also for the 64bit and finally, the `rJava.dll` object is located in the folder withx64` in the name (so, I assume it is also a 64bit version).
I faced the same issue . Please locate jvm.dll
should be in (your JRE version could be different )
C:\Program Files (x86)\Java\jre1.8.0_65\bin\client
or
C:\Program Files (x86)\Java\jre1.8.0_65\bin\server
add this path to your windows system path and you are good to go .but keep in mind the version of jre and R should be consistent,if your java is in Program Files its 64 bit so launch from 64 bit R if its in Program Files (x86)its 32 bit so use 32 bit R
like in my case it showed error in 64 bit
but worked perfectly in 32 bit
You did use / instead of \.
Sys.setenv(JAVA_HOME='C:\\Program Files (x86)\\Java\\jre1.8.0_65')
library(XLConnect)
I am using UNIX. Therefore I cannot test it by myself but your path might be wrong as well.
According to this post you can search it by using this:
find.java <- function() {
for (root in c("HLM", "HCU")) for (key in c("Software\\JavaSoft\\Java Runtime Environment",
"Software\\JavaSoft\\Java Development Kit")) {
hive <- try(utils::readRegistry(key, root, 2),
silent = TRUE)
if (!inherits(hive, "try-error"))
return(hive)
}
hive
}
credit goes to #nograpes for the function and this article for helping me giving you the answer.

Warning message In download.file: download had nonzero exit status

I am downloading data from data.gov website and I get following two types of errors in the process:
fileUrl <- "http://catalog.data.gov/dataset/expenditures-on-children-by-families"
download.file(fileUrl,destfile=".data/studentdata.csv",method="curl")
Warning message:
In download.file(fileUrl, destfile = ".data/studentdata.csv", method = "curl") :
download had nonzero exit status
I tried to remove the method="curl" as suggested in other forum, but again I get this new error
download.file(fileUrl,destfile=".data/studentdata.csv")
Error in download.file(fileUrl, destfile = ".data/studentdata.csv") :
cannot open destfile '.data/studentdata.csv', reason 'No such file or directory'
I think there are two major factors why your curl doesn't work well.
First, the problem is on your URL. fileUrl <- "http://catalog.data.gov/dataset/expenditures-on-children-by-families". In your URL, it is not referred to a csv file. So, they won't work even if you set the destination into a csv file such as destfile = ".data/studentdata.csv"
I have an example of getting a csv dataset using the same code (different dataset):
DataURL<- "https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD" (This link refers to a rows.csv file)
download.file(DataURL, destfile="./data/rows.csv", method="curl") (The method is quite same, using curl)
Second, previously I had the same problem that the curl does not work, even I used a proper URL that refers to a csv file. However, when I diagnosed a bit deeper, I found something interesting fact about why my curl method cannot work properly. It was my R session program. I used a 32-bit R, in which the error occurs. Later then, I tried to change the session into a 64-bit R. Amazingly, and the download status was running at that time. To see your R session architecture (whether you are using 32-bit or 64-bit), type in your R:
sessionInfo()
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-ming32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
You have to switch your R, from 32-bit to 64-bit to avoid 'curl' call had nonzero exit status. You go to your R directory folder, and then you run a 64-bit R.
If you are using a Windows OS and installing the R in a default path folder, you can run this C:\Program Files\R\R-3.5.3\bin\x64\R.exe. (I used a version of 3.5.3, so it may be different with your version)
If you are using R-studio, you can switch the R session on the menubar Tools -> Global Options -> R version -> Change -> Use your machine's default version of R64 (64-bit) -> OK. Then restart your R-studio.
However, it depends on your OS architecture. If you are using a 32-bit OS, hence you have to find another way to solve this.
So looking at the code for download.file(...), if you specify method="curl" the function tries to use the curl shell command. If this command does not exist on your system, you will get the error above.
If you do not specify a method, the default is to use an internal R method to download, which evidently works on your system. In that case, the function is trying to put the file in .data/studentdata.csv but evidently there is not .data directory. Try taking out the ..
When this download works, you will get a text/html file, not a csv file. Your url points to a web page, not a download link. That page does have a download link, but unfortunately it is a pdf, not a csv.
Finally, if your goal is to have the data in R (is it?), and if the link actually produces a csv file, you could more easily use
df <- read.csv(fileUrl)
If I'm not very much mistaken you just have a simple typo here. I suspect you have a "data" directory, not a ".data" directory - in which case your only problem is that your destfile string needs to begin "./data", not ".data".
I was having the same problem.
Then I realized that I forget to create the "data" directory!
So try adding this above your fileURL line to create the directory first.
if(!file.exists("data")){
dir.create("data")
}
Also, if you are running a Mac, then you want to keep method="curl" when downloading a https file. I don't believe Windows has that problem hence the suggestions to remove it.
Try this:
file<-'http://catalog.data.gov/dataset/expenditures-on-children-by-families'
file<- read.csv(file)

Resources