简体   繁体   中英

How do I use an r for-loop to repeatedly fill out template with each loop using data from a list

I want to write a script that will take a list of daily incomes and enter these incomes into a standard XML template. which currently has a placeholder where the income value should be. The aim is to generate n, in this case 3, api ready XML products with unique date and income fields. I cannot post actual data as it is confidential, so I've generated a simplified version.

 Date Income
       <chr>  <dbl>
1 20/06/2017   2000
2 22/06/2017   3023
3 23/06/2017   4021

XML 
#
<Date>holding_date</Date>
<LineAmountTypes>Inclusive</LineAmountTypes>
<Description>total daily income</Description>
<LineAmount>holding_lineamount_value</LineAmount>
</LineItem></LineItem>
<BankAccount>
    <Code>value</Code>
</BankAccount>

I can't figure out a way, using for-loops or LApply functions, to say, generate a series of XML templates, similar to the one above, except that each contains a unique daily income where "holding_lineamount_value" is now.

I can do this manually obviously, but I need this to be reproducible for hundreds of daily incomes and dates.

Can anyone help me construct a for loop or alternative to complete this task as I'm lost?

It isn't very clear from your description what you would like the final XML structure to look like. But this might get you started on creating a new XML document for each row of data in your dataframe.

library(XML)

# Create function to generate an XML file
createXML <- function(x){
  # Get data from current column being processed
  holding_date <- x[1]
  holding_lineamount_value <- x[2]

  # Create main node
  xmlfile <- newXMLNode("MainXML")

  # Add nodes to main node
  xmlfile <- addChildren(xmlfile, newXMLNode("Date", holding_date))
  xmlfile <- addChildren(xmlfile, newXMLNode("LineAmountTypes", "Inclusive"))
  xmlfile <- addChildren(xmlfile, newXMLNode("Description", "total daily income"))
  xmlfile <- addChildren(xmlfile, newXMLNode("LineAmount", holding_lineamount_value))
  xmlfile <- addChildren(xmlfile, newXMLNode("LineItem"))
  xmlfile <- addChildren(xmlfile, newXMLNode("LineItems"))

  # Create BankAccount node
  ba <- newXMLNode("BankAccount")

  # Add Code node to BankAccount node
  ba <- addChildren(ba, newXMLNode("Code","value"))

  # Add BankAccount node to main node
  xmlfile <- addChildren(xmlfile, ba)

  # Return the xml file
  return(xmlfile)
}

# Create dataframe
df <- data.frame(Date = c("20/06/2017", "22/06/2017","23/06/2017"),
                 Income = c(2000,3023,4021),
                 stringsAsFactors = FALSE)

# Transpose dataframe to be processed with lapply
tdf <- as.data.frame(t(df))

# Create a list of XML files for each column of transposed dataframe
xml.list <- lapply(tdf, createXML)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM