How to import unusual data formats into R? - r

Format consist of lines, every line has set of key="value" elements.
Format example:
X="1" Y="2" Z="who are you?"
Y="4" Z="bla bla..."
X="42"
I would like to import this data into R, table or data.frame, where key defines column.

The following code parses the file you provided in a 'melted' form:
data<-NULL
stream<-file("path");open(stream) #or stream<- textConnection(' X="1" Y="2" Z="who are you?" Y="4" Z="bla bla..." X="42"')
while(length(ele<-c(scan(stream,what="string",n=1,sep="="),scan(stream,what="string",n=1,sep=" ")))>0){
data<-rbind(data,ele);
}
close(stream);
print(data);
Now crystallizing:
sapply(unique(data[,1]),function(key) data[data[,1]==key,2])

Related

Jasper -Java.util.collection paramter from main to Sub report

I've chart report which uses the parameter of Type Java.util.collection. When I try to use this chart at group band as subreport to the Main report with similar parameter of Type Java.util.collection, the chart value keep iterates within the group. For example, if the chart shows the value for L1 & L2 in two separate page. But when added to the Main report for the value L1, the subreport displays both L1 & L2 rather than just L1.
Could I able to display just L1 from subreport when the parameter is passed as L1, not both L& L2 in the group band?
FYI, using Jaspersoft Studio 5.6.1
EDIT:
Suppose I am passing 2 Projects P1 and P2 as parameters from main report, the result now I am getting is:
1st page Main report data P1
Sub report data P1
Sub report data P2
2nd page Main report Data P2
Sub report data P1
Sub report data P2
But, the desired result should be like this:
1st page Main report data P1
Sub report data P1
2nd page Main report Data P2
Sub report data P2.
I've added the subreport in one of the Group header.
Main Report:
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test_subreport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d2616041-39f3-44ce-a1e4-4ce5a6a1593c">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Dev DB Conn" />
<parameter name="param_project" class="java.util.collection" nestedType="java.lang.Integer">
<parameterDescription>
<![CDATA[]]>
</parameterDescription>
</parameter>
<queryString language="SQL">
<![CDATA[SELECT
i.id prid,c.INUSAGE,d.CURRUSAGE
FROM
project table i, custom table c
where i.id = c.id and $X{IN,I.id,param_project}
]]>
</queryString>
<field name="prid" class="java.math.BigDecimal" />
<field name="INUSAGE" class="java.math.BigDecimal" />
<field name="CURRUSAGE" class="java.math.BigDecimal" />
<group name="Group1">
<groupExpression>
<![CDATA[$F{INVID}]]>
</groupExpression>
</group>
<columnHeader>
<band height="210">
<textField>
<reportElement x="0" y="80" width="80" height="30" uuid="263d8d17-1c40-4709-ba8f-76e27a976c75" />
<textFieldExpression>
<![CDATA[$F{prid}]]>
</textFieldExpression>
</textField>
<subreport>
<reportElement isPrintRepeatedValues="false" x="80" y="2" width="200" height="200" uuid="da630714-b937-493c-9db5-a4baa7128f3d" />
<subreportParameter name="param_invID">
<subreportParameterExpression>
<![CDATA[$P{param_project}]]>
</subreportParameterExpression>
</subreportParameter>
<connectionExpression>
<![CDATA[$P{REPORT_CONNECTION}]]>
</connectionExpression>
<subreportExpression>
<![CDATA["Workload1_added_collection_group1.jasper"]]>
</subreportExpression>
</subreport>
</band>
</columnHeader>
Subreport link: subreport
Thanks
Sreeram
Usually the subreport should only get the item it should display, e.g. "P1 ID" for P1 and "P2 ID" for P2. But if you provide a collection it will always cycle through the collection of data.
So you should add either the ID to the subreport or put the chart in the main report.

How to get xml attribute value in R

Hej all,
I have load an XML-File into R and want to extract an attribute value.
<espa_metadata version="2.0" xsi:schemaLocation="http://espa.cr.usgs.gov/v2 http://espa.cr.usgs.gov/schema/espa_internal_metadata_v2_0.xsd">
<global_metadata></global_metadata>
<bands>
<band product="cfmask" source="toa_refl" name="cfmask" category="qa" data_type="UINT8" nlines="7801" nsamps="7651" fill_va.lue="255">
<percent_coverage>
<cover type="clear">40.35</cover>
<cover type="cloud">39.99</cover>
</percent_coverage>
</band>
</bands>
</espa_metadata>
I want to extract the value 39.99 for cover type="cloud".
I used the following approach but I only get "NULL"
library(XML)
data <- xmlParse("LC82030342015346LGN00.xml")
xpathApply(data,"//percent_coverage/cover[#type='cloud']" , xmlValue)
Any ideas? Thank u in advance!

BizTalk mapping records into pairs

Using a BizTalk Map and wondering if there's a good way to do the following.
I need to map a received Invoice to a list of Orders. However each Order must contain only two line items (let's assume the Invoice has an even number).
This means that Invoice(1, 2, 3, 4) should map to Orders(Order (1, 2), Order (3, 4)).
Essentially, I need to create new parent elements when (index % 2 == 0).
Is there a functoid-only way of doing this?
XML example below:
<Invoice>
<Client>Client1</Client>
<Line>
<Code>1</Code>
<Price>$1.00</Price>
<Qty>1</Qty>
</Line>
<Line>
<Code>2</Code>
<Price>$2.00</Price>
<Qty>2</Qty>
</Line>
<Line>
<Code>3</Code>
<Price>$3.00</Price>
<Qty>3</Qty>
</Line>
<Line>
<Code>4</Code>
<Price>$4.00</Price>
<Qty>4</Qty>
</Line>
</Invoice>
to
<Orders>
<Order>
<Client>Client1</Client>
<OrderItem>
<Code>1</Code>
<Qty>1</Qty>
</OrderItem>
<OrderItem>
<Code>2</Code>
<Qty>2</Qty>
</OrderItem>
</Order>
<Order>
<Client>Client1</Client>
<OrderItem>
<Code>3</Code>
<Qty>3</Qty>
</OrderItem>
<OrderItem>
<Code>4</Code>
<Qty>4</Qty>
</OrderItem>
</Order>
</Orders>
What I've tried:
Standard mapping: A single Order with all lines, as expected
Value Mapping to Client based on index % 2: Order per line (half have Client)
Table Looping (Gated to index % 2) + Extractor to Client: Order per line, only half lines
What I'm likely to end up doing:
XSLT
Or, C# manipulation after mapping
In the advanced functoids section of the toolbox look for the 'index' functoid.
Add a scripting functoid on the output.
Add C# code to check for the indexes you want to match.
You can switch output on and off by returning a boolean from the scripting functoid.

Multiply with XML in FLEX

I write a code which is return the following XML which contain in a string type variable.
<imageedit>
<matrix a="0.5213903738845257" b="0" c="0" d="0.5213903738845257" tx="559.6" ty="1.0784769629138395"/>
<cutout x="0" y="0" width="400" height="568"/>
</imageedit>
Now i want to multiply all the nodes with 3 and again store in that string type variable. how can i do this?
You can use E4X to access attributes like this:
var xml : XML = <imageedit>
<matrix a="0.5213903738845257" b="0" c="0" d="0.5213903738845257" tx="559.6" ty="1.0784769629138395"/>
<cutout x="0" y="0" width="400" height="568"/>
</imageedit>
xml.matrix.#a = Number(xml.matrix.#a)*3
and the same for other attributes. You can also iterate through them with 'for each'.

flex3 Format date without timezone

I'm receiving a date from a server in milliseconds since 1-1-1970. I then use the DateFormatter to print the date to the screen. However, Flex adds timedifference and thus it displays a different time than what I got from the server. I've fixed this by changing the date before printing to screen. But I think that's a bad solution because the date object doesn't hold the correct date.
Does anyone know how to use the dateFormatter to print the date, ignoring the timezone?
this is how I did it:
function getDateString(value:Date):String
{
var millisecondsPerMinute:int = 1000*60;
var newDate:Date = new Date(value.time - (millisecondsPerMinute*value.timezoneOffset));
var dateFormatter:DateFormatter = new DateFormatter();
dateFormatter.formatString = "EEEE DD-MM-YYYY LL:MM AA";
return dateFormatter.format(newDate);
}
Maybe there is something I'm missing but this seems to work for me.
<?xml version="1.0"?>
<!-- formatters\FormatterDateField.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<!-- Declare a DateFormatter and define formatting parameters.-->
<mx:DateFormatter id="dateFormatter"
formatString="EEEE DD-MM-YYYY LL:NN:SS AA"/>
<mx:Label text="Millis (1220836618601 == Monday 08-09-2008 01:16:58 AM):"/>
<mx:TextInput id="dob" text="1220836618601"/>
<mx:Label text="Formatted date UTC: "/>
<mx:TextInput id="formattedDate"
text=""
editable="false"/>
<mx:Label text="Formatted date local: "/>
<mx:TextInput id="formattedDateLoc"
text=""
editable="false"/>
<!-- Format and update the date.-->
<mx:Button label="Format Input"
click="
var d :Date = new Date(parseInt(dob.text));
formattedDate.text=dateFormatter.format(d.toUTCString());
formattedDateLoc.text=dateFormatter.format(d);
"/>
</mx:Application>
Suggesting that instead of passing the date object (which is timezone dependant) into the dateFormatter, pass in the date object's UTC String instead. I didn't find anything that would suggest that the DateFormatter does anything to the timezone, so there shouldn't be any need to try to compensate for the timezone, especially when the date object already provides a method for getting the UTC.
function getDateString(value:Date):String
{
var dateFormatter:DateFormatter = new DateFormatter();
dateFormatter.formatString = "EEEE DD-MM-YYYY LL:MM AA";
return dateFormatter.format(value.toUTCString());
}
In Flex Hero 4.5 you can use the new Spark DateTimeFormatter:
<s:DateTimeFormatter dateTimePattern="HH':'mm':'ss" id="dateFormatterUTC" useUTC="true" />
<s:Label text="{dateFormatterUTC.format(new Date())}" />
The most simple of fixes is to have as many objects as you can (and properties of objects) be strings. The timezoneOffset solution works fine, but the timezoneOffset for many US cities changes twice during the year. The best rule -- everything is a string.

Resources