I have been using add_header_lines() to add a header to my flextable. I would like this header to have a different text format than the table as a whole. Unfortunately, when I use fontsize() it formats text for both the header and the column names. Is there a way to only change the text format for the header produced with add_header_lines()?
Here is some sample code:
df <- data.frame(crime = c("assault", "homicide", "burglary"),
Jan = c(5, 2, 7),
Feb = c(2, 4, 0),
Mar = c(1, 2, 1))
flex <- flextable(df)
flex <- add_header_lines(flex, values = "This is a header")
flex <- fontsize(flex, size = 15, part = "header")
As you can see, the fontsize function affects both the added header and the column values. Any idea of how to change just the headers text format?
You have to use the selector i.
library(flextable)
df <- data.frame(crime = c("assault", "homicide", "burglary"),
Jan = c(5, 2, 7),
Feb = c(2, 4, 0),
Mar = c(1, 2, 1))
flex <- flextable(df)
flex <- add_header_lines(flex, values = "This is a header")
flex <- fontsize(flex, size = 15, part = "header", i = 1)
flex <- color(flex, color = "red", part = "header", i = 1)
flex
I'm using heatmap.2 to create a plot, however, the initial plot that is saved to my source folder is missing a key and title.
When I then run the dev.off() command, the Key and the Title are then used to overwrite the original graph?
For instance, I will produce a plot like this:
Which is far from perfect. But then when I run the dev.off() to close the device (otherwise a host of other errors ensue):
What you are looking at above is a very distorted Key and my 'XYZ' title.
Why on earth is it creating two files, firstly the one with my matrix, and then overwriting this with a second file containing my flipping key and my title? I cannot follow the logic.
I've updated my OS, my version of R, RStudio, all my packages and unistalled RStudio. Nothing seems to help.
If you'd like to try and replicate my error here is the example matrix:
structure(c(1, 4, 5, 3, 3, 4, 6, 1, 7, 5, 5, 4, 4, 8, 1, 3, 9,
2, 2, 9, 3, 1, 3, 4, 4, 5, 5, 5, 1, 4, 4, 3, 3, 3, 9, 1), .Dim = c(6L,
6L))
And this is the script I'm using to plot my example data. You'll need to provide a SourceDir and make sure you assign the matrix to the name "Matrix".
if (!require("gplots")) {
install.packages("gplots", dependencies = TRUE)
library(gplots)
}
if (!require("RColorBrewer")) {
install.packages("RColorBrewer", dependencies = TRUE)
library(RColorBrewer)
}
my_palette <- colorRampPalette(c("snow", "yellow", "darkorange", "red"))(n = 399)
transition
col_breaks = c(seq(0,1,length=100), #white 'snow'
seq(2,4,length=100), # for yellow
seq(5,7,length=100), # for orange 'darkorange'
seq(8,9,length=100)) # for red
png(paste(SourceDir, "Heatmap_Test.png"),
width = 5*1000,
height = 5*1000,
res = 300,
pointsize =15)
heatmap.2(Matrix,
main = paste("XYZ"),
notecol="black",
key = "true" ,
colsep = c(3, 6, 9),
rowsep = c(3, 6, 9),
labCol = NULL,
labRow = NULL,
sepcolor="white",
sepwidth=c(0.08,0.08),
density.info="none",
trace="none",
margins=c(1,1),
col=my_palette,
breaks=col_breaks,
dendrogram="none",
RowSideColors = c(rep("blue", 3), rep("orange", 3)),
ColSideColors = c(rep("blue", 3), rep("orange", 3)),
srtCol = 0 ,
asp = 1 ,
adjCol = c(NA, 0) ,
adjRow = c(0, NA) ,
#keysize = 2 ,
Colv = FALSE ,
Rowv = FALSE ,
key.xlab = paste("Correlation") ,
cexRow = (1.8) ,
cexCol = (1.8) ,
notecex = (1.5) ,
lmat = rbind(c(0,0,0,0), c(0,0,2,0),c(0,1,3,0),c(0,0,0,0)) ,
#par(ColSideColors = c(2,2)),
lhei = c(1, 1, 3, 1) ,
lwid = c(1, 1, 3, 1))
dev.off()
I'd really appreciate any insight into this problem.
I believe this resulted from the fact that I had more than just elements 1 to four, as the coloured rows I had added counted as additional elements that had to be arranged in the display matrix.
As such:
mat = rbind(c(0,0,0,0), c(0,0,2,0),c(0,1,3,0),c(0,0,0,0)) ,
lhei = c(1, 1, 3, 1) ,
lwid = c(1, 1, 3, 1))
No longer cut the butter. After much ado, I finally managed to get the following layout to work (on my actual data, not my example data).
lmat = rbind(c(0,4,5,0), c(0,0,2,0),c(0,1,3,0),c(0,0,6,0)) ,
lhei = c(0.4, 0.16, 3, 0.4) , # Alter dimensions of display array cell heighs
lwid = c(0.4, 0.16, 3, 0.4),
Notice the inclusion of elements 5 and 6.
So my final command looks like this (note that there will be many other changes but the real progress happened once I added in 5 and 6):
png(paste(SourceDir, "XYZ.png"),
width = 5*1500,
height = 5*1500,
res = 300, # 300 pixels per inch
pointsize =30)
heatmap.2(CombinedMtx,
main = paste("XYZ"), # heat map title
notecol="black",
key = "true" ,# change font color of cell labels to black
colsep = c(6, 12, 18),
labCol = c(" "," "," ", "XX"," "," "," "," "," ", "YY"," "," "," "," "," ", "ZZ"," "," "," "," "," ", "QQ"),
rowsep = c(6, 12, 18),
labRow = c(" "," "," ", "XX"," "," "," "," "," ", "YY"," "," "," "," "," ", "ZZ"," "," "," "," "," ", "QQ"),
sepcolor="white",
sepwidth=c(0.08,0.08),
density.info="none",
trace="none",
margins=c(1,1),
col=my_palette,
breaks=col_breaks,
dendrogram="none",
RowSideColors = c(rep("#deebf7", 6), rep("#1c9099", 6), rep("#addd8e", 6), rep("#fee391", 6)),
ColSideColors = c(rep("#deebf7", 6), rep("#1c9099", 6), rep("#addd8e", 6), rep("#fee391", 6)),
srtCol = 0 ,
asp = 1 ,
adjCol = c(1.5, -61.5) ,
adjRow = c(0, -1.38),
offsetRow = (-59.5),
keysize = 2 ,
Colv = FALSE ,
Rowv = FALSE ,
key.xlab = NA ,
key.ylab = NULL ,
key.title = NA ,
cexRow = (1.6) ,
cexCol = (1.6) ,
notecex = (1.5) ,
cex.main = (20),
lmat = rbind(c(0,4,5,0), c(0,0,2,0),c(0,1,3,0),c(0,0,6,0)) ,
#par(ColSideColors = c(2,2)),
lhei = c(0.4, 0.16, 3, 0.4) , # Alter dimensions of display array cell heighs
lwid = c(0.4, 0.16, 3, 0.4),
symkey = any(0.5 < 0, na.rm=FALSE) || col_breaks,
key.par=list(mar=c(3.5,0, 1.8,0) ) #tweak specific key paramters
)
dev.off()
Also, if you don't start each time by creating the PNG and enf each time by using dev.off() it won't work. I believe this might also have been contribution to my confusion, and potentially after drawing the heatmap, some elements were being drawn once the dev.off() command was run, causing the heatmap to be overwritten.
This (with my matrix) creates this image.
What I have done is a really gammy way of labelling my blocks but until I can work out how to get ComplexHeatmap working properly I'll be stuck using hacks like this with Heatmap.2.
I found out that you can execute R within U-SQL. So i took a R-script from one of our data-scientists and build a U-SQL script based on this sample script.
The adapted script:
DECLARE #INPUT_DAT string =
#"/Samples/Data/dat2json/validationData.dat.201805271617";
DECLARE #OUTPUT string = #"/Samples/Output/validationdata.out";
REFERENCE ASSEMBLY [ExtR];
DECLARE #myRScript = #"
datavector <- as.vector(readBin(#INPUT_DAT, "double", size = 4, n = 99000))
Size <- length(datavector)
numberOfPixels <- Size / 84
MaterialBase <- factor(rep(c("Plastic", "Aluminum"), each = (Size / 2)))
ThicknessBase <- factor(rep(c(rep(c(0, 10, 20, 30, 40, 50), times = 7),
rep(c(0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0), each = 6)), each = numberOfPixels))
ThicknessIterated <- factor(rep(c(rep(c(0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0),
each = 6), rep(c(0, 10, 20, 30, 40, 50), times = 7)), each = numberOfPixels))
Pixel <- rep(1:numberOfPixels, times = 84)
dflabel <- data.frame(MaterialBase, ThicknessBase, ThicknessIterated, Pixel,
Value = datavector)
";
#RScriptOutput = REDUCE #myRScript USING new
Extension.R.Reducer(command:#myRScript, rReturnType:"dataframe");
OUTPUT #ScriptOutput
TO #OUTPUT
USING Outputters.Tsv();
The problem is that when I build the code, Visual Studio stops on line 6, after #". Intellisense also show a red ~ sign indicating that something is wrong. The error it generates is: Expected one od: OPTION ';'
The R-script works perfectly in R-studio.
Update 2018-07-19:
I have narrowed it a bit down. The problem is the double quotes in the #myRScript variable. So I changed the code to the following:
DECLARE #INPUT_DAT string =
#"/dat2json/data/validationData.dat.201805271617";
DECLARE #OUTPUT string = #"/dat2json/data/validationdata.out";
DECLARE #vartype string = "double";
DECLARE #var1 string = "Plastic";
DECLARE #var2 string = "Aluminum";
REFERENCE ASSEMBLY [ExtR];
DECLARE #myRScript string = #"
datavector <- as.vector(readBin(#INPUT_DAT, #vartype, size = 4, n = 99000))
Size <- length(datavector)
numberOfPixels <- Size / 84
MaterialBase <- factor(rep(c(#var1, #var2), each = (Size / 2)))
ThicknessBase <- factor(rep(c(rep(c(0, 10, 20, 30, 40, 50), times = 7),
rep(c(0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0), each = 6)), each = numberOfPixels))
ThicknessIterated <- factor(rep(c(rep(c(0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0),
each = 6), rep(c(0, 10, 20, 30, 40, 50), times = 7)), each = numberOfPixels))
Pixel <- rep(1:numberOfPixels, times = 84)
dflabel <- data.frame(MaterialBase, ThicknessBase, ThicknessIterated, Pixel,
Value = datavector)
";
#RScriptOutput = REDUCE #myRScript ON MaterialBase USING new
Extension.R.Reducer(command:#myRScript, rReturnType:"dataframe");
OUTPUT #ScriptOutput
TO #OUTPUT
USING Outputters.Tsv();
But now I get an other error:
E_CSC_USER_ROWSETVARIABLENOTFOUND: Rowset variable #myRScript was not found.
Description:
Rowset variables must be assigned to before they can be referenced.
Resolution:
Assign a rowset to the rowset variable or remove the reference.
Looks like I have to put the rsult of the R-script into a variable an use that one in the REDUCE statement. But how to do that?
Deploy R script as resource. The script file is deployed into the vertex workspace and is accessible from any custom code.
DECLARE #rScriptFile string = #"MyR2.R";
DECLARE #rScriptDeploy string = #"/rscripts/" + #rScriptFile;
DEPLOY RESOURCE #rScriptDeploy;
#inputQuery2 =
REDUCE #inputQuery1
ON Par
PRODUCE Par,
...
READONLY Par
USING new Extension.R.Reducer(scriptFile : #rScriptFile, rReturnType : "dataframe");
I want to write a set of randomly generated numbers to a text file with fixed format. But for some reasons, write.fwf only wrote the 1st column right, all other columns got one extra digit. How can I fix it? Thanks!
set.seed(1899)
library(sensitivity)
library(randtoolbox)
par_lower <- c( 0.12, 0.13, 0.038, 0.017)
par_upper <- c(12.00, 13.00, 3.800, 1.700)
sample_size <- 5
lim_para8 <- c(par_lower[1], par_upper[1])
lim_para9 <- c(par_lower[2], par_upper[2])
lim_parb8 <- c(par_lower[3], par_upper[3])
lim_parb9 <- c(par_lower[4], par_upper[4])
par_rand <- parameterSets(par.ranges = list(lim_para8, lim_para9,
lim_parb8, lim_parb9),
samples = sample_size, method = "sobol")
par_rand
# write to file
library(gdata)
file2write <- paste("par.txt", sep = "")
write.fwf(par_rand, file = file2write, width = c(10, 10, 10, 10), colnames = FALSE)
The results:
6.060 6.56500 1.91900 0.858500
9.030 3.34750 2.85950 0.437750
3.090 9.78250 0.97850 1.279250
4.575 4.95625 2.38925 0.227375
10.515 11.39125 0.50825 1.068875
If I changed to
write.fwf(par_rand, file = file2write, width = c(10, 9, 9, 9),
colnames = FALSE, quote = FALSE, rownames = FALSE)
I got this error
Error in write.fwf(par_rand, file = file2write, width = c(10, 9, 9, 9), :
'width' (9) was too small for columns: V4
'width' should be at least (10)
Please try the code below, it works for me. I tested with several formats and all worked. Both code segments return a fixed format file with width 4 x 10.
This of course implies that setting sep in the definition of file2write does not work for getting the desired output with write.fwf
write.fwf(par_rand, file = "par2.txt", width = c(10, 10, 10, 10), colnames = FALSE, sep = "")
write.fwf(par_rand, file = file2write, width = c(10, 10, 10, 10), colnames = FALSE, sep = "")
The following generates the same but with 1x10 and 3x9, as I think you wanted
write.fwf(par_rand, file = "par3.txt", width = c(10, 9, 9, 9), colnames = FALSE, sep = "")
Please let me know whether this is what you wanted.