How to send dates as characters from JMP to RAll,
I am trying to leverage some finance functions that exist in R . The functions require dates to be passed as character . Can anybody guide me on how to pass the dates in a format acceptable to R from JMP .
@ih ( Special Mention)
# Working R Code
library(tvm)
CF1 <- c(-100,20,40,25,8,15)
d1 <- as.Date(c("2016-01-01","2016-04-01","2016-10-01","2017-02-01","2017-03-01","2017-06-01"))
Res <- vector()
Res <- sapply(1:length(d1), function(i) {xnpv(i,CF1[i:length(CF1)],d1[i:length(d1)])})I would like to do the exact same thing - except the inputs will need be passed from JMP to R .
The cash flow (CF1) is a straight vector and have no problem in passing, however the dates are the tricky part.
XNPV function in R if I am not requires dates to be formatted as character shown above.
Even though, I format the dates as character in JMP , but when I pass them to R I dont think they are being passed correctly.
R Send(DatesInJMP)
d1 <- as.Date(DatesInJMP)
R Get(d1)
- This throws an error - "as.Date.default(DatesInJMP)" - do not know how to convert to class "Date"
Re: How to send dates as characters from JMP to RFrom ?as.Date in R:
The <FONT face="Courier New">as.Date</FONT> methods accept character strings, factors, logical <FONT face="Courier New">NA</FONT> and objects of classes <FONT face="Courier New">"</FONT><A href="http://127.0.0.1:46214/help/library/base/help/POSIXlt" target="_blank" rel="nofollow noopener noreferrer"><FONT face="Courier New">POSIXlt</FONT></A><FONT face="Courier New">"</FONT> and<FONT face="Courier New">"</FONT><A href="http://127.0.0.1:46214/help/library/base/help/POSIXct" target="_blank" rel="nofollow noopener noreferrer"><FONT face="Courier New">POSIXct</FONT></A><FONT face="Courier New">"</FONT>
DatesInJMP shows up as a list of strings instead of a string vector. The code below verifies this, then corrects the problem by casting to a character vector first.
Note that you can also send dates and times as numbers, you just need to account for the lack of a time zone in JMP by importing the value as UTC. You could then offset it to local time if necessary; I do that sort of thing with lubridate. Or you could do that offset in JMP before sending it to R.
Names default to here( 1 );
DatesInJMP = { "2017-01-01", "2017-01-02", "2017-01-03" };
R Init();
R Send( DatesInJMP );
R Submit( "str(DatesInJMP)");
R Submit( "dt <- as.Date(as.character(DatesInJMP))" );
Write("\!N"); //New line so log is easier to read
R Submit( "str(dt)");
Write("\!N");
//Send a number instead
JMPTime = num( Today() );
R Send( JMPTime );
R Submit( "Date_POSIX <- as.POSIXct( JMPTime, origin = '1904-01-01', tz = 'GMT')");
R Submit( "Date_ <- as.Date( Date_POSIX )");
R Submit( "str( Date_POSIX )");
R Submit( "str( Date_ )");
R Term();
Edited of fix the screwed up formatting from trying to change code in this window..
Re: How to send dates as characters from JMP to R@txnelson,
The second piece of code that I have shared i.e. is in JSL and not R .
R Send(DatesInJMP)
d1 <- as.Date(DatesInJMP)
R Get(d1)
This is my attempt to display the dates that are passed from JMP to R . When I invoke R from JMP , an instance of R is not opened, so I am not sure how to test what R is seeing other than retrieving it into JMP as I try above. Each argument is passed separately from JMP to R as shown below :
# JSL CODE
R Send(DatesInJMP);
R Send(CashFlow);
R Submit("
d1 <- as.Date(DatesInJMP)
"
);
R Get(d1);
Re: How to send dates as characters from JMP to RHave you displayed in R the list you passed so you can see what R received? Can you modify you original R code to a separate list, and then pass it as you are attemping to do in your modified version of the R code, for JMP?
Re: How to send dates as characters from JMP to RHey Jim,
I did format the dates as characters before passing to R. They are formatted as a list of character dates.
The formula I used for formatting them are :
(Char( Format( :PeriodEnding, "y/m/d" ) ))Re: How to send dates as characters from JMP to RUday,
Dates in JMP are numeric. The list that you need in your R code is a list of strings. You need to use the Format() function to build a new list in JMP where you have translated the numeric values in the list, to character values in the string, before you pass them to R