xlsx package under Ubuntu cannot set values - r

I am running the following script
library(xlsx);
wb <- loadWorkbook("/home/.../MyFile.xlsx") #works fine
sh <- getSheets(wb) #works fine
rw <- getRows(sh[[1]]) #-works fine
rc <- getCells(rw) # works fine
v <- lapply(rc, getCellValue) # works fine
v['21.4'] #works fine, returns the correct value
setCellValue(rc['21.4'], 'Hallo') #fails
Error in .jcall(cell, "V", "setCellValue", value) :
java.lang.ClassNotFoundException
after repeating the command
Error in .jcall(cell, "V", "setCellValue", value) :
RcallMethod: cannot determine object class
It happens in RStudio and in the classical R terminal under Ubuntu 12.04 32Bit.
Please help!

I have the same problem in Windows 7 x64 and I think that there is a mistake in 'xlsx' documentation. However instead of
setCellValue(rc['21.4'], 'Hallo')
try
setCellValue(rc[['21.4']], 'Hallo')
Worked for me.
Kuba

Related

How do i clear a description argument?

Good afternoon, I received an R script from my professor to act on files on my computer. More specifically it is supposed to go into the file and read the data and perform two very specific calculations and add two values to the spreadsheet to my understanding. It seems like the pathing issue is clear but i have a new error when i attempt to run the code
source("C:/Users/JNunl/OneDrive/Desktop/DASCRIPT.R")
Error in file(file, "rt") : invalid 'description' argument
I have launched R studio
Created a new project with the working directory being the same folder the programs are in
Made sure i had downloaded the right libraries needed to run the script
Double checked the path was correct
Launched the script
Imported the dataset(idk if that nessesary but have done it anyway)
Run the line of code in question and it returns this error
I have tried to remove the last section with the file name and it does run that but i get errors for the rest of the program
Below is the code in question:
library(seacarb)
library(readxl)
library(lubridate)
setwd("~/")
myf=list.files(pattern="csv")
para=c("YEAR", "MONTH", "STATION_ID", "STN_DEPTH_M", "LATITUDE", "LONGITUDE","SAMPLING_DATE","SAMPLE_DEPTH_M","DEPTH_CODE", "MEDIUM", "SAMPLE_ID", "VALUE_1", "VALUE_2", "VALUE_3")
result=NULL
for(i in 1: length(myf)){
out=read.csv(myf[i], header=T)[para]
result=rbind(result, out)
}
colnames(result)=c("Yr","Mn", "Stn","Std","Lat","Lon","Sdt","Spd","Dc", "Med", "SID", "Alk", "pH", "T")
a=subset(result, Med =="surface water")
a$Alk=as.numeric(a$Alk)
a$pH=as.numeric(a$pH)
a$T=as.numeric(a$T)
a$Mn=month(as.Date(a$Sdt))
a=subset(a, Alk>50 & Alk<125)
a=subset(a, !is.na(Alk) & !is.na(pH) & !is.na(T))
b=carb(flag=8, var1=a$pH, var2=a$Alk/50000, T=a$T, S=0.2, P=1, Pt=0.3e-9, Sit =10e-6, k1k2="r", pHscale ="F")
a$DIC=b$DIC*12000
a$pCO2=b$pCO2
stns=read_excel("stn.xlsx")
bsn<-function(x) if(nrow(subset(stns, STATION==x))>0) subset(stns, STATION==x)$BASIN else NA
lon<-function(x) if(nrow(subset(stns, STATION==x))>0) mean(subset(stns, STATION==x)$LONGITUDE) else NA
lat<-function(x) if(nrow(subset(stns, STATION==x))>0) mean(subset(stns, STATION==x)$LATITUDE) else NA
a$Basin=unlist(mapply(bsn, a$Stn), use.names=F)
a$Lat=unlist(mapply(lat, a$Stn), use.names=F)
a$Lon=unlist(mapply(lon, a$Stn), use.names=F)
Additionally here is a screenshot of the file that i would like r to act on
sample data
I will be back around to anwser any questions
Thank you for your time

Validation using testinstalledbasic function in 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")

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()

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 while using transformation function in R

I was working with baby names data set and encountered below error while using transform function. Any guidance/suggestion would be highly appreciated. I did reinstalled the packages but of no avail.
Mac OS X (Mountain Lion)
R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
library(stringr)
require(stringr)
bnames1 <- transform(bnames1,
first = tolower(str_sub(name,1,1)),
last = tolower(str_sub(name,-1,1)),
vowels = vowels(name),
length= nchar(name),
per1000 = 10000 * prop,
one_par = 1/prop
)
Error in tolower(str_sub(name, 1, 1)) :
lazy-load database '/Library/Frameworks/R.framework/Versions/3.1/Resources/library/stringr/R/stringr.rdb' is corrupt
In addition: Warning messages:
1: In tolower(str_sub(name, 1, 1)) :
restarting interrupted promise evaluation
2: In tolower(str_sub(name, 1, 1)) : internal error -3 in R_decompress1
internal error -3 is often a functioning of installing on top of a loaded package. Restart R and restart your application. There may be other issues, but until you do this you won't be going much further.
Try
remove.packages("stringr")
install.packages("stringr")

Resources