Set up different fonts for fragments of string in R - r

I have a long string txt that I want to display as margin text in a plot using mtext(). The txt string is composed of another string txt.sub, as well as of a date string, which applies a specific format to a date command argument. However, I want to display the "date" part of that string only in bold.
The string is:
date.in = as.Date( commandArgs( trailingOnly=TRUE )[1], format="%m/%d/%Y" )
date = format(date.in, "%b %d, %Y")
txt.sub = "Today's date is: "
txt = paste(txt.sub, date, sep = "")
I tried the following
## Plot is called first here.
mtext(expression(paste(txt.sub, bold(date), sep = "")), line = 0, adj = 0, cex = 0.8)
but the problem with this is that it doesn't paste the values of txt.sub and date, but rather displays literally the words "txt.sub" and "date".
Is there any way to get to the result I am looking for? Thank you!

Adjusting one of the examples from the help page on mathematical annotation (see example 'How to combine "math" and numeric variables'):
mtext(bquote(.(txt.sub) ~ bold(.(date))), line=0, adj=0, cex=0.8)

Related

How present currency value in French format in R

I have part of code that should show currency value in a French format
This code
library(formattable)
currency(x = 123456, symbol = "€", digits = 0)
gives me "€123,456".
I need code that gives me "123 456€" in a French format for one single value.
Thanks!
I'm not sure how to do it with currency function. It seems to not take in consideration putting the symbol after.
You can maybe use prettyNum function from base R in combination with paste to add the symbol at the end:
paste(prettyNum(x, big.mark = " ",big.interval = 3), "€")
[1] "123 456 €"
Alternatively, in DT, you can use formatCurrency function:
library(DT)
x = 123456
datatable(as.matrix(x)) %>% formatCurrency(1, '\U20AC', digits = 0, before = FALSE, mark = "")
Does it answer your question ?

escape quote with paste in r

I've tried to follow other related question here, and the answer is not working for me, so I apologize for what is likely a duplicate question. I'm not finding other answers work for me.
I have 2 strings:
numbers = 1:12
month = month.name
I want to paste together to get the following, having quotes around the number, and quotes around the month, with an equal sign between:
""1" = "January"", ""2" = "February"", etc.
But
paste(numbers, month, sep = '" = "')
and
paste(numbers, month, sep = '\" = \"')
both give result:
"1\" = \"January" "2\" = \"February"...etc.
How do I get rid of the \?

as.POSIXlt vs as.date and strptime

Basically, I have this date set for electric consumption per min in a household and I have a data with 9 columns, my data is:
https://archive.ics.uci.edu/ml/datasets/Individual+household+electric+power+consumption
so I tried two things and got two somewhat different output and I cant seem to figure out why is that:
first input:
hpc$Datetime<-as.POSIXlt(hpc$Datetime, format = "%d/%m/%Y %H:%M:%S")
with(hpc,plot(Datetime,Global.active.power, ylab = "Global.active.power(Killowatts)",
xlab = "",type = "l"))
second input:
hpc<-read.table("hpc.txt", skip = 66637, nrow = 2879, sep =";")
hpc$Time<-strptime(hpc$Time, format = "%H:%M:%S")
hpc$Date<-as.Date(hpc$Date, format = "%d/%m/%Y")
with(hpc,plot(Time,Global.active.power, ylab = "Global.active.power(Killowatts)",
xlab = "",type = "l"))
Why is there a line appearing in the second image
It will be a great if someone can be kind enough to help me out!!
Thankyou in advance

R string vector seq

This is the output I am aiming for where z will provide a plot of mine with an x-axis.
z<-c("2014-01", "", "", "2014-04", "", "", "2014-07", "", "","2014-10", "", "", "2015-01")
The plot will be produced once a month and I aim on automating the axis creation. 2015-01 is my last available data point and the plot will display a span of 1 year back to 2014-01. Next update will make me want to set the plot to 2015-02 to 2014-02.
As can be seen, the labelling of the axis goes back in 3 month steps, leaving the ticks in between empty.
Can I automate this process by providing just the latest label 2015-02 and the rest gets deducted by R somehow?
I was thinking maybe to convert my starting point 2015-01 to a date Format and sequence it back to 2014-01. Then making it a character again and using it for the axis...
myz <- as.Date(c("2014-01", "2015-01"), "%Y - %m")
But myz is empty. And ofc the problem with the empty tick is far froms olved.
Any advice?
This might be done more efficiently but here is a solution:
version <- "201701"
endDate = as.Date(paste0(version,"01"), "%Y%m%d")
startDateString = paste0(as.integer(substr(version, 1, 4)) - 3, substr(version, 5, 6))
startDate = as.Date(paste0(startDateString,"01"), "%Y%m%d")
rngx <- format(seq(startDate, endDate, "3 months"), "%Y-%m")
output = c()
for(i in 1:(length(rngx)-1)) {
output = c(output, rngx[i], "", "")
}
output = c(output, rngx[length(rngx)])
version <- "201701" marks the start date (and is used for some other parts in the script as well, for example the setwd()

Does setDataFormatForType() work correctly for Dates in XLConnect?

I recently tried all sorts of formatting arguments on the function
setDataFormatForType(wb, type=XLC$DATA_TYPE.DATETIME, format="d/m/yy")
for example format="d/m/yy" as shown above, besides numerous others.
This then is followed up by
setStyleAction(wb, XLC$"STYLE_ACTION.DATA_FORMAT_ONLY")
and then I write a worksheet and save the workook.
No form of format tweaking seems to work.
As soon as I mess with any format in the setDataFormatForType command the result is that the numeric time value shows up in the date columns in Excel workbook that I save later on
i.e. for Nov. 6th, 2013 = 41584.
If I do not interfere with any DataFormats then Standard (POSIX) format gets saved but when you look at that in the resulting Excel it has some Custom "XLConnect format" assigned to it so it is displayed "wrong" :-(
- which means American notation (leading month followed by day) but what I want is Eurepean (leading day followed by the month).
If anyone has some experience with setting these DataFormats (especially 'dates') in XLConnect, then sharing some thoughts or wisdom would be highly appreciated.
Thanks, Walter
There's a new style action XLC$"STYLE_ACTION.DATATYPE" in the XLConnect version available from github at https://github.com/miraisolutions/xlconnect. The "datatype" style action can be used to style cells of a specific type using a specific cell style which can be set using setCellStyleForType. See the following example:
require(XLConnect)
wb = loadWorkbook("test.xlsx", create = TRUE)
setStyleAction(wb, XLC$"STYLE_ACTION.DATATYPE")
cs = createCellStyle(wb, name = "mystyle")
setDataFormat(cs, format = "d/m/yy")
setCellStyleForType(wb, style = cs, type = XLC$"DATA_TYPE.DATETIME")
data = data.frame(A = 1:10, B = Sys.time() + 1:10)
createSheet(wb, "data")
writeWorksheet(wb, data = data, sheet = "data")
saveWorkbook(wb)
You do need to have a named region called "Dates". I saved a copy of the template2.xslx file with such a region. The only think that worked for me was to write it out with the format.Date function:
Dates=seq(from=as.Date("2001-01-01"), to=as.Date("2013-01-01"), by=365)
file.copy(system.file("demoFiles/template2.xlsx",
package = "XLConnect"),
"dataformat.xlsx", overwrite = TRUE)
wb <- loadWorkbook("dataformat.xlsx")
setDataFormatForType(wb, type = XLC$"DATA_TYPE.DATETIME",
format = "dd/mm/yyyy")
setStyleAction(wb, XLC$"STYLE_ACTION.DATA_FORMAT_ONLY")
createName(wb, name = "Dates", formula = "mtcars!$A$1")
writeNamedRegion(wb, format(Dates, "%d.%m.%Y"), name = "Dates")
saveWorkbook(wb)

Resources