Customize correlation output #286
Replies: 12 comments 11 replies
-
That's amazing @JanMarvin! Great work (on this and on I would like to update my package's I will of course make minor modifications as I familiarize myself with the new |
Beta Was this translation helpful? Give feedback.
-
Ha, I have nothing against that. Glad that you like it. But for the time being, I suggest that you try to get it working with We are on the right track and are closing in, but we are not there yet. This was one of the first real world examples I messed around with to see how we behave and it has already triggered two pull request (#288 and another one following soon). But if you want to be an early adapter and help us with testing things, you're more than welcome to try and use |
Beta Was this translation helpful? Give feedback.
-
Just to clarify, you are saying that it would be possible to achieve the same result with |
Beta Was this translation helpful? Give feedback.
-
So I was able to create a new sheet with the p-values and format those according to desired criteria using However, one aspect I am not able to replicate is your style including the stars. It seems the style with stars works with data <- mtcars
my.cor.matrix <- stats::cor(data, use = "pairwise.complete.obs")
my.cor.matrix <- as.data.frame(my.cor.matrix)
wb <- openxlsx::createWorkbook()
openxlsx::addWorksheet(wb, "r-values")
openxlsx::writeData(wb, sheet = "r-values", my.cor.matrix, rowNames = TRUE)
all.columns <- 1:(ncol(my.cor.matrix) + 1)
r.style <- openxlsx::createStyle(numFmt = "#.#0 \\*")
star_style <- openxlsx::createStyle(numFmt = "#.#0 \\*\\*\\*")
# Note: Commenting the code below does not make the following one work
openxlsx::addStyle(wb,
sheet = "r-values",
style = r.style,
rows = all.columns,
cols = all.columns,
gridExpand = TRUE)
openxlsx::conditionalFormatting(wb,
"r-values",
cols = all.columns,
rows = all.columns,
rule = ">0",
style = star_style)
openxlsx::saveWorkbook(wb,
file = "test.xlsx",
overwrite = TRUE)
openxlsx::openXL("test.xlsx")
If I use the AND or OR operators in the rule (like in your example), I get the following message:
Any idea? |
Beta Was this translation helpful? Give feedback.
-
Hi @rempsyc , thanks for trying. Unfortunately with With your style in > wb$styles$dxfs
[1] "<dxf><font><sz val=\"11\"/><color rgb=\"FF000000\"/><name val=\"Calibri\"/></font> </dxf>" And it should look like this (from > wb$styles_mgr$styles$dxfs
[1] "<dxf><font><color rgb=\"FF000000\"/><name val=\"Calibri\"/><sz val=\"11\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.##0 \\*\\*\\*\"/><fill><patternFill patternType=\"solid\"><bgColor rgb=\"FFFFC7CE\"/></patternFill></fill></dxf>" The |
Beta Was this translation helpful? Give feedback.
-
Oh you could add the numfmt XML node to the |
Beta Was this translation helpful? Give feedback.
-
Thanks! How exactly would I go about adding "the numfmt XML node to the openxlsx workbook"? Is that a PR I need to submit with |
Beta Was this translation helpful? Give feedback.
-
You could simply assign to |
Beta Was this translation helpful? Give feedback.
-
Cool stuff! I'm able to make it work with one conditional formatting, but not with several ones. From the Because when I use more than one, only the first one is used, not the second one. When I look at wb$styles_mgr$styles$dxfs
[1] "<dxf><font><color rgb=\"FF9C0006\"/><name val=\"Calibri\"/><sz val=\"11\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.##0 _*_*_*\"/><fill><patternFill patternType=\"solid\"><bgColor rgb=\"FFFFC7CE\"/></patternFill></fill></dxf>"
[2] "<dxf><font><color rgb=\"FF9C0006\"/><name val=\"Calibri\"/><sz val=\"11\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.##0 _*_*_*\"/><fill><patternFill patternType=\"solid\"><bgColor rgb=\"FFFFC7CE\"/></patternFill></fill></dxf>"
[3] "<dxf><font><color rgb=\"FF9C0006\"/><name val=\"Calibri\"/><sz val=\"11\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.##0 \\*_*_*\"/><fill><patternFill patternType=\"solid\"><bgColor rgb=\"FFFFC7CE\"/></patternFill></fill></dxf>"
[4] "<dxf><font><color rgb=\"FF9C0006\"/><name val=\"Calibri\"/><sz val=\"11\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.##0 \\*_*_*\"/><fill><patternFill patternType=\"solid\"><bgColor rgb=\"FFFFC7CE\"/></patternFill></fill></dxf>"
[5] "<dxf><font><color rgb=\"FF9C0006\"/><name val=\"Calibri\"/><sz val=\"11\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.##0 \\*\\*_*\"/><fill><patternFill patternType=\"solid\"><bgColor rgb=\"FFFFC7CE\"/></patternFill></fill></dxf>"
[6] "<dxf><font><color rgb=\"FF9C0006\"/><name val=\"Calibri\"/><sz val=\"11\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.##0 \\*\\*_*\"/><fill><patternFill patternType=\"solid\"><bgColor rgb=\"FFFFC7CE\"/></patternFill></fill></dxf>"
[7] "<dxf><font><color rgb=\"FF9C0006\"/><name val=\"Calibri\"/><sz val=\"11\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.##0 \\*\\*\\*\"/><fill><patternFill patternType=\"solid\"><bgColor rgb=\"FFFFC7CE\"/></patternFill></fill></dxf>"
[8] "<dxf><font><color rgb=\"FF9C0006\"/><name val=\"Calibri\"/><sz val=\"11\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.##0 \\*\\*\\*\"/><fill><patternFill patternType=\"solid\"><bgColor rgb=\"FFFFC7CE\"/></patternFill></fill></dxf>" I realized the description of those styles are available in wb$styles_mgr$dxf
typ id name
1 dxf 0 gre_style
2 dxf 1 red_style
3 dxf 2 gre_style_p1
4 dxf 3 red_style_p1
5 dxf 4 gre_style_p2
6 dxf 5 red_style_p2
7 dxf 6 gre_style_p3
8 dxf 7 red_style_p3 Seems like I can't access that info in Here is a reprex showing that the first style (no star) gets applied correctly, but not the second style: wb <- openxlsx::createWorkbook()
r.style <- openxlsx::createStyle(numFmt = "#.#0 _*_*_*")
starstyle <- openxlsx::createStyle(numFmt = "#.#0 \\*_*_*")
cm <- mtcars |> correlation::correlation() |> summary(redundant = TRUE)
all.columns <- 1:(ncol(cm))
pf <- attr(cm, "p")
openxlsx::addWorksheet(wb, "p-values")
openxlsx::writeData(wb, sheet = "p-values", pf, rowNames = FALSE)
openxlsx::conditionalFormatting(wb,
"p-values",
cols = all.columns,
rows = all.columns,
rule = "> 0.05",
style = r.style)
openxlsx::conditionalFormatting(wb,
"p-values",
cols = all.columns,
rows = all.columns,
rule = "<= 0.05",
style = starstyle)
wb$styles$dxfs
#> [1] "<dxf><font><sz val=\"11\"/><color rgb=\"FF000000\"/><name val=\"Calibri\"/></font> </dxf>"
wb$styles$dxfs <- c("<dxf><font><sz val=\"11\"/><color rgb=\"FF000000\"/><name val=\"Calibri\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.#0 _*_*_*\"/></dxf>",
"<dxf><font><sz val=\"11\"/><color rgb=\"FF000000\"/><name val=\"Calibri\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.#0 \\*_*_*\"/></dxf>")
wb$styles$dxfs
#> [1] "<dxf><font><sz val=\"11\"/><color rgb=\"FF000000\"/><name val=\"Calibri\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.#0 _*_*_*\"/></dxf>"
#> [2] "<dxf><font><sz val=\"11\"/><color rgb=\"FF000000\"/><name val=\"Calibri\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.#0 \\*_*_*\"/></dxf>"
openxlsx::saveWorkbook(wb,
file = "test.xlsx",
overwrite = TRUE)
openxlsx::openXL("test.xlsx")
But keeping only the second style/dfx of the list does add stars as expected since it becomes the first one. But then if there's a second style with two stars it doesn't get applied, etc. Maybe there is something else breaking that I am missing. Perhaps I should open a new PR in |
Beta Was this translation helpful? Give feedback.
-
You only see a single style, because wb <- openxlsx::createWorkbook()
r.style <- openxlsx::createStyle(numFmt = "#.#0 _*_*_*", bgFill = "blue")
starstyle <- openxlsx::createStyle(numFmt = "#.#0 \\*_*_*", bgFill = "green")
cm <- mtcars |> correlation::correlation() |> summary(redundant = TRUE)
all.columns <- 1:(ncol(cm))
pf <- attr(cm, "p")
openxlsx::addWorksheet(wb, "p-values")
openxlsx::writeData(wb, sheet = "p-values", pf, rowNames = FALSE)
openxlsx::conditionalFormatting(wb,
"p-values",
cols = all.columns,
rows = all.columns,
rule = "> 0.05",
style = r.style)
openxlsx::conditionalFormatting(wb,
"p-values",
cols = all.columns,
rows = all.columns,
rule = "<= 0.05",
style = starstyle)
wb$styles$dxfs <- c("<dxf><font><sz val=\"11\"/><color rgb=\"FF000000\"/><name val=\"Calibri\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.#0 _*_*_*\"/></dxf>",
"<dxf><font><sz val=\"11\"/><color rgb=\"FF000000\"/><name val=\"Calibri\"/></font><numFmt numFmtId=\"3\" formatCode=\"#.#0 \\*_*_*\"/></dxf>")
openxlsx::openXL(wb) Ofc you can open an issue in |
Beta Was this translation helpful? Give feedback.
-
Ok so in the meanwhile I have decided to try to make library(rempsyc)
#> Tutorials: https://remi-theriault.com/tutorials
#> Bug report, support, special request: https://github.com/rempsyc/rempsyc/issues
#>
#> Suggested APA citation: Thériault, R. (2022). rempsyc: Convenience functions for psychology (R package version 0.0.5.5) [Computer software]. https://rempsyc.remi-theriault.com
cormatrix_excel2(mtcars)
#> # Correlation Matrix (pearson-method)
#>
#> Parameter | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb
#> --------------------------------------------------------------------------------------------------------------------------------
#> mpg | 1.00*** | -0.85*** | -0.85*** | -0.78*** | 0.68*** | -0.87*** | 0.42 | 0.66** | 0.60** | 0.48 | -0.55*
#> cyl | -0.85*** | 1.00*** | 0.90*** | 0.83*** | -0.70*** | 0.78*** | -0.59* | -0.81*** | -0.52* | -0.49 | 0.53*
#> disp | -0.85*** | 0.90*** | 1.00*** | 0.79*** | -0.71*** | 0.89*** | -0.43 | -0.71*** | -0.59* | -0.56* | 0.39
#> hp | -0.78*** | 0.83*** | 0.79*** | 1.00*** | -0.45 | 0.66** | -0.71*** | -0.72*** | -0.24 | -0.13 | 0.75***
#> drat | 0.68*** | -0.70*** | -0.71*** | -0.45 | 1.00*** | -0.71*** | 0.09 | 0.44 | 0.71*** | 0.70*** | -0.09
#> wt | -0.87*** | 0.78*** | 0.89*** | 0.66** | -0.71*** | 1.00*** | -0.17 | -0.55* | -0.69*** | -0.58* | 0.43
#> qsec | 0.42 | -0.59* | -0.43 | -0.71*** | 0.09 | -0.17 | 1.00*** | 0.74*** | -0.23 | -0.21 | -0.66**
#> vs | 0.66** | -0.81*** | -0.71*** | -0.72*** | 0.44 | -0.55* | 0.74*** | 1.00*** | 0.17 | 0.21 | -0.57*
#> am | 0.60** | -0.52* | -0.59* | -0.24 | 0.71*** | -0.69*** | -0.23 | 0.17 | 1.00*** | 0.79*** | 0.06
#> gear | 0.48 | -0.49 | -0.56* | -0.13 | 0.70*** | -0.58* | -0.21 | 0.21 | 0.79*** | 1.00*** | 0.27
#> carb | -0.55* | 0.53* | 0.39 | 0.75*** | -0.09 | 0.43 | -0.66** | -0.57* | 0.06 | 0.27 | 1.00***
#>
#> p-value adjustment method: Holm (1979) cormatrix_excel2(iris)
#> # Correlation Matrix (pearson-method)
#>
#> Parameter | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width
#> ----------------------------------------------------------------------
#> Sepal.Length | 1.00*** | -0.12 | 0.87*** | 0.82***
#> Sepal.Width | -0.12 | 1.00*** | -0.43*** | -0.37***
#> Petal.Length | 0.87*** | -0.43*** | 1.00*** | 0.96***
#> Petal.Width | 0.82*** | -0.37*** | 0.96*** | 1.00***
#>
#> p-value adjustment method: Holm (1979) cormatrix_excel2(airquality)
#> # Correlation Matrix (pearson-method)
#>
#> Parameter | Ozone | Solar.R | Wind | Temp | Month | Day
#> ----------------------------------------------------------------------------
#> Ozone | 1.00*** | 0.35** | -0.60*** | 0.70*** | 0.16 | -0.01
#> Solar.R | 0.35** | 1.00*** | -0.06 | 0.28** | -0.08 | -0.15
#> Wind | -0.60*** | -0.06 | 1.00*** | -0.46*** | -0.18 | 0.03
#> Temp | 0.70*** | 0.28** | -0.46*** | 1.00*** | 0.42*** | -0.13
#> Month | 0.16 | -0.08 | -0.18 | 0.42*** | 1.00*** | -7.96e-03
#> Day | -0.01 | -0.15 | 0.03 | -0.13 | -7.96e-03 | 1.00***
#>
#> p-value adjustment method: Holm (1979)
It seems that styles overwrite each other (instead of being additive). Which means that to make it work I had do it through the So overall, I am pretty satisfied (though I'll probably need to tweak the colours eventually) and will probably use One aspect I have noticed is that the default font and background are coloured, whereas I think they should not be. This forces me to specify font as black and background as white, but in doing so, the natural borders are lost (and I don't feel like specifying them manually and try to match the default colour, etc.). It would be nice if defaults would be actual defaults, if possible. Thanks for all your help! |
Beta Was this translation helpful? Give feedback.
-
I suppose it is best to continue the discussion here, so just an update. I have been having trouble with the if (isFALSE(requireNamespace(openxlsx2, quietly = TRUE))) {
rlang::check_installed(
"remotes",
reason = "to install the required dependency for this function (openxlsx2)."
)
cat("The package `openxlsx2` is required for this function\n",
"Would you like to install it?")
if (utils::menu(c("Yes", "No")) == 1) {
remotes::install_github('JanMarvin/openxlsx2')
} else (stop(
"The cormatrix_excel2 function relies on the `openxlsx2` package.
You can install it manually with:
remotes::install_github('JanMarvin/openxlsx2')"))
} Not best practice, but at least it works! That said, I am also getting no errors or warnings when running R CMD check locally, but I am getting one on GitHub tests for Windows only: C:\rtools40\usr\bin\tar.exe --use-compress-program "zstd -d" -xf D:/a/_temp/a1c2b5ca-dcde-4da8-9a76-7dcea8572bd6/cache.tzst -P -C D:/a/rempsyc/rempsyc --force-local
! rlang, fansi, utf8, and vctrs are loaded in other R sessions and have locked
files in your library. The installation will probably fail, unless you quit
from this R session:
• Rterm.exe (5876) in cmd.exe (6908)
ℹ Building openxlsx2 0.2.1.9000
✖ Failed to build openxlsx2 0.2.1.9000
Error: Error: <callr_remote_error: Failed to build source package 'openxlsx2'>
in process 5876
-->
Failed to build source package 'openxlsx2', stdout + stderr:
OE> * installing *source* package 'openxlsx2' ...
OE> Error: package or namespace load failed for 'openxlsx2' in inDL(x, as.logical(local), as.logical(now), ...):
OE> unable to load shared object 'C:/Users/runneradmin/AppData/Local/Temp/RtmpIn5JEg/pkg-lib16f464242299/openxlsx2/libs/x64/openxlsx2.dll':
OE> LoadLibrary failure: The specified procedure could not be found.
OE>
OE> Error: loading failed
OE> Execution halted
OE> ERROR: loading failed
OE> * removing 'C:/Users/RUNNER~1/AppData/Local/Temp/RtmpIn5JEg/pkg-lib16f464242299/openxlsx2' Note: I know you already warned me that the package is at one's "own risk and perils" until it reaches CRAN but I am just sharing here FYI. |
Beta Was this translation helpful? Give feedback.
-
Create significance stars to correlation output (asked for in ycphs/openxlsx#374 by @rempsyc).
This is what it looks like:
For comparison in R it looks like this:
There is no real reason why this should not be backward compatible. Styles in
openxlsx
usestylesObject()
, otherwise it should work too.Beta Was this translation helpful? Give feedback.
All reactions