Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Wednesday, January 31, 2018

Spotfire - TERR dealing with date

 It is really hard for TERR to deal with date type in Spotfire. You cannot use as.Date() in TERR which works perfectly fine in R Studio.

Reference:
https://datashoptalk.com/convert-strings-date-time-terr/#comment-3303


Spotfire data functions recognize TERR objects of class “POSIXct” as date/time information. As designed, the Spotfire/TERR data function interface for date/time information does the following:
– Converts a Spotfire value or column whose DataType is “Date”, “Time” or “DateTime” into a TERR object of class “POSIXct”.
– Converts a TERR object of class “POSIXct” into a Spotfire value or column with a DataType of “DateTime”, which can then be formatted in Spotfire to display only the date (or to display only the time) if needed.
This interface does not use any other TERR object classes (such as the “Date” class in TERR) to transfer date/time information between Spotfire and TERR.

You can do this in R Studio, but this doesn't work in TERR:

A way around it is to use strptime()


R - scraping url and download csv from website


Tuesday, January 2, 2018

R - Fill in data gaps function

fillNAgaps <- function(x, firstBack=FALSE) {
  ## NA's in a vector or factor are replaced with last non-NA values
  ## If firstBack is TRUE, it will fill in leading NA's with the first
  ## non-NA value. If FALSE, it will not change leading NA's.
 
  # If it's a factor, store the level labels and convert to integer
  lvls <- NULL
  if (is.factor(x)) {
    lvls <- levels(x)
    x    <- as.integer(x)
  }
 
  goodIdx <- !is.na(x)
 
  # These are the non-NA values from x only
  # Add a leading NA or take the first good value, depending on firstBack 
  if (firstBack)   goodVals <- c(x[goodIdx][1], x[goodIdx])
  else             goodVals <- c(NA,            x[goodIdx])
 
  # Fill the indices of the output vector with the indices pulled from
  # these offsets of goodVals. Add 1 to avoid indexing to zero.
  fillIdx <- cumsum(goodIdx)+1
 
  x <- goodVals[fillIdx]
 
  # If it was originally a factor, convert it back
  if (!is.null(lvls)) {
    x <- factor(x, levels=seq_along(lvls), labels=lvls)
  }
 
  x
}

colInput <-c(2,4,5,6,1,8,9,3,10,13,11,12)
bins <-5
colInput<-sort(colInput)

colOutput <- c()
n<- seq(1,length(colInput),by = length(colInput)/bins)

for (i in 1:length(n)){
  if(i <= n[i]){
    colOutput[ceiling(n[i]):ceiling(n[i]+1)] <-  paste("group " ,toString(i))
    if(is.na(colOutput[floor(n[i])])){colOutput[floor(n[i])]<-paste("group " ,toString(i))}
  }
}

colInput<-sort(colInput)

colOutput<-colOutput[1:length(colInput)]

colOutput<-fillNAgaps(colOutput)

tblNewGroup <- cbind(colInput ,colOutput)

Thursday, October 12, 2017

Spotfire - Filter data by user input (R script)

1. Create Input Field (multiple select) in the Text Area










2. Create Data Function


3. Add Input/Output Parameters

4. Edit Parameters for both Input/Output











5. Add table use the following steps:

6. To apply the function in the list box. Add "Apply" and "Clear" button and add script
7. Make sure to add relationship between the user input data table and the data Table