Validation using testinstalledbasic function in R - r

I am trying to test my R installation but I am having issues. Running the below code in 5 different machines:
> Sys.setenv(LC_COLLATE = "C", LANGUAGE = "en")
> library("tools")
> testInstalledBasic("both")
Gives the following message:
running regression tests running code in ‘reg-tests-1a.R’ running
code in ‘reg-tests-1b.R’ running code in ‘reg-tests-1c.R’ running
code in ‘reg-tests-2.R’ comparing ‘reg-tests-2.Rout’ to ‘reg-tests-2.Rout.save’ ...7256c7256
< ï..ColA ColB ColC
X...ColA ColB ColC Warning message: running command 'diff -bw "C:\Users\xd00010\AppData\Local\Temp\RtmpSSTr6F\Rdiffa2140205053b0"
"C:\Users\xd00010\AppData\Local\Temp\RtmpSSTr6F\Rdiffb2140c9a5185"'
had status 1
The issue seems to be my machine reads the "UTF-8-BOM" file with the ï symbol but the reference file in "R" tests has X instead. Any ideas how to resolve this?

I was able to get a bit forward but now have new issues. Here is the solution if anyone needs it:
options(encoding = "UTF-8")
Sys.setenv(LC_COLLATE = "C", LANGUAGE = "en",LC_TIME="C",LC_CTYPE="C")
library("tools")
testInstalledBasic("both")

Related

Error in Running NLRX (NetLogo) in Manjaro (Arch) Linux

I am attempting to run an NLRX simulation in Manjaro Linux (RNetLogo wouldn't work for some reason either), and am running into the following error when attempting to set up an dummy experiment:
cp: cannot stat '~/.netlogo/NetLogo 6.1.1/netlogo-headless.sh': No such file or directory
sed: can't read /tmp/Rtmpj15Yf7/netlogo-headless365385fb4bdc0.sh: No such file or directory
sed: can't read /tmp/Rtmpj15Yf7/netlogo-headless365385fb4bdc0.sh: No such file or directory
sh: /tmp/Rtmpj15Yf7/netlogo-headless365385fb4bdc0.sh: No such file or directory
Error in util_gather_results(nl, outfile, seed, siminputrow) :
Temporary output file /tmp/Rtmpj15Yf7/nlrx5493_1365385ab03157.csvnot found. On unix systems this can happen if the default system temp folder is used.
Try reassigning the default temp folder for this R session (unixtools package).
In addition: Warning message:
In system(NLcall, wait = TRUE) : error in running command
Given that I am running R 4.0.0, the Unixtools package doesn't work, so that's out of the question. How would I go about fixing this?
Code for those curious:
library(nlrx)
netlogopath <- file.path("~/.netlogo/NetLogo 6.1.1")
modelpath <- file.path(netlogopath, "app/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")
outpath <- file.path("/home/out")
nl <- nl(nlversion = "6.0.3",
nlpath = netlogopath,
modelpath = modelpath,
jvmmem = 1024)
nl#experiment <- experiment(expname="wolf-sheep",
outpath=outpath,
repetition=1,
tickmetrics="true",
idsetup="setup",
idgo="go",
runtime=50,
evalticks=seq(40,50),
metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"),
variables = list('initial-number-sheep' = list(min=50, max=150, qfun="qunif"),
'initial-number-wolves' = list(min=50, max=150, qfun="qunif")),
constants = list("model-version" = "\"sheep-wolves-grass\"",
"grass-regrowth-time" = 30,
"sheep-gain-from-food" = 4,
"wolf-gain-from-food" = 20,
"sheep-reproduce" = 4,
"wolf-reproduce" = 5,
"show-energy?" = "false"))
nl#simdesign <- simdesign_lhs(nl=nl,
samples=100,
nseeds=3,
precision=3)
results <- run_nl_all(nl = nl)
R Version for those who may want it:
platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 4
minor 0.0
year 2020
month 04
day 24
svn rev 78286
language R
version.string R version 4.0.0 (2020-04-24)
nickname Arbor Day
In case others find this helpful: I have encountered similar errors as the result of file path misspecification. For instance, double check model path. You may need to drop app/.

Error when using RNetlogo to start Netlogo

I have built a simulation model in Netlogo and hope to optimize model parameters (around 30). Since Netlogo does not support automate multiple runs with different parameter sets, I was thinking using another platform (R/python/Java) to call Netlogo, analyze the simulated results, and find the optimal parameters.
However, none of them work so far...In R, I have encountered error when starting Netlogo using RNetLogo. I have tried all the potential solutions I can find online, but still haven't figured out the issue. Would appreciate it if someone can help.
Code:
library(RNetLogo)
nl.path = "C:/Program Files/NetLogo 5.3/app"
NLStart(nl.path, gui=FALSE, nl.jarname = 'NetLogo.jar')
Error message:
java.lang.NoClassDefFoundError: org/nlogo/api/Exceptions$Handler
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
Caused by: java.lang.ClassNotFoundException
at RJavaClassLoader.findClass(RJavaClassLoader.java:383)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
Version:
- system: Windows 10
- R: 3.3.3
- Netlogo: 5.3/5.3.1/6.0/6.0.2 (tried all of them, same error message)
- Java: 1.8.0_151-b12 (this is the one called in R, checked by .jcall)
- RNetlogo and rJava are most up-to-date as of 1/9/2018
Does it solve your problem, if you use the java environment included in the NetLogo installation?
Try this one before you install or load RNetLogo inside R:
Sys.setenv(JAVA_HOME="YOUR-INSTALLATION-PATH\\NetLogo 6.0.1\\runtime")
I'm definitely stuck on that same error with 5.3.1. Out of curiosity, does this work for you with version 6.0.2?
library(RNetLogo)
nl.path <- "C:/Program Files/NetLogo 6.0.2/app"
NLStart(nl.path, gui = FALSE, nl.jarname = "netlogo-6.0.2.jar")
There are a few diagnostics to run, but before that, copy all the .jar files from the Java/ folder in your Netlogo/ folder to the same folder where NetLogo 5.3.0.app is located.
Then make sure you have both /app in the dir path and the NL version in the nl.jarname argument.
# get packages from source
p<-c("rJava", "RNetLogo"); remove.packages(p)
install.packages("rJava", repos = "https://cran.r-project.org/", type="source")
install.packages("RNetLogo", repos = "https://cran.r-project.org/", type="source")
library(rJava);library(RNetLogo)
nl_path = "C:/Program Files/NetLogo 5.3.0/app"
ver_nl <- "5.3.0"
NLStart(nl_path,gui=F,nl.jarname = paste0("netlogo-",ver_nl,".jar")) # open netlogo without gui
# NLLoadModel("path-to-nl-model",nl.obj=NULL) # load model with nl.obj=NULL
Failing this, run these diagnostics in order. You should have no rJava errors. If you do, ensure you have the latest version of Java SE installed:
https://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html
# test 1
install.packages("rJava", repos = "https://cran.r-project.org/", type="source"); library(rJava)
.jinit()
.jnew( "java/awt/Point", 10L, 10L )
f <- .jnew("java/awt/Frame","Hello")
.jcall(f,,"setVisible",TRUE)
t1err <- geterrmessage()
# test 2
component <- .jnull()
component <- .jcast(component, new.class = "java/awt/Component")
message <- .jnew("java/lang/String","This is a JOptionPane test from rJava.")
message <- .jcast(message, new.class = "java/lang/Object")
title <- .jnew("java/lang/String","Test")
type <- .jnew("java/lang/Integer", as.integer(2))
f <- .jnew("javax/swing/JOptionPane")
.jcall(f,"showMessageDialog", component, message, title, .jsimplify(type))
t2err <- geterrmessage()
# # test 3
.jcall("java/lang/System", "S", "getProperty", "java.vm.version")
.jcall("java/lang/System", "S", "getProperty", "java.vm.name")
.jcall("java/lang/System", "S", "getProperty", "java.vm.info")
.jcall("java/lang/System", "S", "getProperty", "java.runtime.version")
.jcall("java/lang/System", "S", "getProperty", "sun.arch.data.model")
t3err <- geterrmessage()
# test 4
.jcall("java/lang/System", "S", "getProperty", "java.awt.headless")
Sys.getenv("NOAWT")
t4err <- geterrmessage()
errorlist <- function(){
if(geterrmessage()==t1err){stop("Failed Test 1 — Headless exception \n \n Wrong awt GUI support for Java/rJava",call.=T)}
if(geterrmessage()==t2err){stop("Failed Test 2 — Invalid method name for RcallMethod (unable to open dialog box)",call.=T)}
if(geterrmessage()==t3err){stop("Failed Test 3 — Old version of Java. \n \n Download latest version \n \n > https://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html.",call.=T)}
if(geterrmessage()==t4err){stop("Failed Test 4 — ",call.=T)}
}
Final alternative is to run RNetLogo from JGR, which works in headless mode for Mac.
install.packages('JGR',,'http://www.rforge.net/') # note the extra comma
library(JGR)
JGR::JGR()

script not running when called via shell vs. rstudio, generates r errors that I don't get when running in gui

(I'm not sure if this is an r or shell issue, forgive adding both tags, if you think I should remove one please comment and I'll do so)
I have a amazon hosted version of r at rstudio.example.com. I have written two scripts and they both run fine when I source them from within Rstudio interface.
When I ssh in to my scripts directory and run from there, the scripts generate some errors.
The purpose of the first script is to qdap::check_spelling of a column of text in a data frame, then get the frequency of that spelling error along with an example of the misspelt word:
library(tidyverse)
library(qdap)
# example data
exampledata <- data.frame(
id = 1:5,
text = c("cats dogs dgs cts oranges",
"orngs orngs cats dgs",
"bannanas, dogs",
"cats cts dgs bnnanas",
"ornges fruit")
)
# check for unique misspelt words using qdap
all.misspelts <- check_spelling(exampledata$text) %>% data.frame %>% select(row:not.found)
unique.misspelts <- unique(all.misspelts$not.found)
# for each misspelt word, get the first instance of it appearing for context/example of word in a sentence
contexts.misspellts.index <- lapply(unique.misspelts, function(x) {
filter(all.misspelts, grepl(paste0("\\b",x,"\\b"), not.found))[1, "row"]
}) %>% unlist
# join it all together in a data farem to write to a csv
contexts.misspelts.vector <- exampledata[contexts.misspellts.index, "text"]
freq.misspelts <- table(all.misspelts$not.found) %>% data.frame() %>% mutate(Var1 = as.character(Var1))
misspelts.done <- data.frame(unique.misspelts, contexts.misspelts.vector, stringsAsFactors = F) %>%
left_join(freq.misspelts, by = c("unique.misspelts" = "Var1")) %>% arrange(desc(Freq))
write.csv(x = misspelts.done, file="~/csvs/misspelts.example_data_done.csv", row.names=F, quote=F)
The final data frame looks like:
> print(misspelts.done)
unique.misspelts contexts.misspelts.vector Freq
1 dgs cats dogs dgs cts oranges 3
2 cts cats dogs dgs cts oranges 2
3 orngs orngs orngs cats dgs 2
4 bannanas bannanas, dogs 1
5 bnnanas cats cts dgs bnnanas 1
6 ornges ornges fruit 1
When I run this on my cloud instance of RStudio it runs with no issues and a csv file is generated in the directory specified on the last line of code.
When I run this in linux I get:
myname#ip-10-0-0-38:~$ r myscript.R
ident, sql
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
Error in grepl(paste0("\\b", x, "\\b"), not.found) :
object 'not.found' not found
In addition: Warning message:
In data.matrix(data) : NAs introduced by coercion
myname#ip-11-0-0-28:~/rscripts$
Looks like a problem with my grepl() function. But it works fine when running within Rstudio, just not when calling the script from shell.
But I'm also getting other errors in a separate script based on a dplyry verb (filter).
If anyone recognizes this issue please help! If any more information is required please let me know and I'll add.
P.S. I tried running the script in my shell locally and it worked. Could this be an issue with my Amazon server?
file in Shell:
shell$ r < input.R > output.CSV
I am not sure if work on R.
You can try!
Through trial and error I found that prepending the library name of each function solved this problem e.g. dplyr::select(). I don't know why but I wish I understood. This only had to be done when calling the script from ssh r myscript.R. On every other environment I tested this was not the case, including local terminal, local RStudio instance, hosted RStudio instance - all 3 of those did not need me to prepend the library, only when calling via ssh

R - Testing an installation

I am new to R. I have just installed R 3.2.2, and RStudio 0.99, under Windows 8.
As per section 3.2 Testing an Installation of Help contents,
I meant to execute the following 5 lines of commands.
Sys.setenv(LC_COLLATE = "C", LANGUAGE = "en")
library("tools")
testInstalledBasic("both")
testInstalledPackages(scope = "base", errorsAreFatal = FALSE)
testInstalledPackages(scope = "recommended", errorsAreFatal = FALSE)
The first 2 worked fine. The third threw an error, as shown here
> Sys.setenv(LC_COLLATE = "C", LANGUAGE = "en")
> library("tools")
> testInstalledBasic("both")
running strict specific tests
running code in ‘eval-etc.R’
unable to open output file
FAILED
[1] 1
Warning message:
running command '"C:/PROGRA~1/R/R-32~1.2/bin/x64/R" CMD BATCH --vanilla --no-timing "eval-etc.R" "eval-etc.Rout"' had status 2
>
What is the problem?
Could it be related to setting directories/permissions?
You seem to be missing the diff file from Rtools in the test folder. Simply copying/pasting should do the trick.

Error trying to read a PDF using readPDF from the tm package

(Windows 7 / R version 3.0.1)
Below the commands and the resulting error:
> library(tm)
> pdf <- readPDF(PdftotextOptions = "-layout")
> dat <- pdf(elem = list(uri = "17214.pdf"), language="de", id="id1")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'C:\Users\Raffael\AppData\Local\Temp
\RtmpS8Uql1\pdfinfo167c2bc159f8': No such file or directory
How do I solve this issue?
EDIT I
(As suggested by Ben and described here)
I downloaded Xpdf copied the 32bit version to
C:\Program Files (x86)\xpdf32
and the 64bit version to
C:\Program Files\xpdf64
The environment variables pdfinfo and pdftotext are referring to the respective executables either 32bit (tested with R 32bit) or to 64bit (tested with R 64bit)
EDIT II
One very confusing observation is that starting from a fresh session (tm not loaded) the last command alone will produce the error:
> dat <- pdf(elem = list(uri = "17214.pdf"), language="de", id="id1")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'C:\Users\Raffael\AppData\Local\Temp\RtmpKi5GnL
\pdfinfode8283c422f': No such file or directory
I don't understand this at all because the function variable is not defined by tm.readPDF yet. Below you'll find the function pdf refers to "naturally" and to what is returned by tm.readPDF:
> pdf
function (elem, language, id)
{
meta <- tm:::pdfinfo(elem$uri)
content <- system2("pdftotext", c(PdftotextOptions, shQuote(elem$uri),
"-"), stdout = TRUE)
PlainTextDocument(content, meta$Author, meta$CreationDate,
meta$Subject, meta$Title, id, meta$Creator, language)
}
<environment: 0x0674bd8c>
> library(tm)
> pdf <- readPDF(PdftotextOptions = "-layout")
> pdf
function (elem, language, id)
{
meta <- tm:::pdfinfo(elem$uri)
content <- system2("pdftotext", c(PdftotextOptions, shQuote(elem$uri),
"-"), stdout = TRUE)
PlainTextDocument(content, meta$Author, meta$CreationDate,
meta$Subject, meta$Title, id, meta$Creator, language)
}
<environment: 0x0c3d7364>
Apparently there is no difference - then why use readPDF at all?
EDIT III
The pdf file is located here: C:\Users\Raffael\Documents
> getwd()
[1] "C:/Users/Raffael/Documents"
EDIT IV
First instruction in pdf() is a call to tm:::pdfinfo() - and there the error is caused within the first few lines:
> outfile <- tempfile("pdfinfo")
> on.exit(unlink(outfile))
> status <- system2("pdfinfo", shQuote(normalizePath("C:/Users/Raffael/Documents/17214.pdf")),
+ stdout = outfile)
> tags <- c("Title", "Subject", "Keywords", "Author", "Creator",
+ "Producer", "CreationDate", "ModDate", "Tagged", "Form",
+ "Pages", "Encrypted", "Page size", "File size", "Optimized",
+ "PDF version")
> re <- sprintf("^(%s)", paste(sprintf("%-16s", sprintf("%s:",
+ tags)), collapse = "|"))
> lines <- readLines(outfile, warn = FALSE)
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'C:\Users\Raffael\AppData\Local\Temp\RtmpquRYX6\pdfinfo8d419174450': No such file or direc
Apparently tempfile() simply doesn't create a file.
> outfile <- tempfile("pdfinfo")
> outfile
[1] "C:\\Users\\Raffael\\AppData\\Local\\Temp\\RtmpquRYX6\\pdfinfo8d437bd65d9"
The folder C:\Users\Raffael\AppData\Local\Temp\RtmpquRYX6 exists and holds some files but none is named pdfinfo8d437bd65d9.
Intersting, on my machine after a fresh start pdf is a function to convert an image to a PDF:
getAnywhere(pdf)
A single object matching ‘pdf’ was found
It was found in the following places
package:grDevices
namespace:grDevices [etc.]
But back to the problem of reading in PDF files as text, fiddling with the PATH is a bit hit-and-miss (and annoying if you work across several different computers), so I think the simplest and safest method is to call pdf2text using system as Tony Breyal describes here.
In your case it would be (note the two sets of quotes):
system(paste('"C:/Program Files/xpdf64/pdftotext.exe"',
'"C:/Users/Raffael/Documents/17214.pdf"'), wait=FALSE)
This could easily be extended with an *apply function or loop if you have many PDF files.

Resources