Comments (11)
Hi Ewen. A Clean shorter solution for html bold formatting of significant p-values in kable tables.
threshold = 0.05 # Threshold for P-value formatting
r %>%
summary_factorlist(dependent, explanatory, cont = "median",
column = TRUE,
fit_id = TRUE)%>%
ff_merge(
coxphuni(r, dependent, explanatory) %>%
fit2df(estimate_suffix=" Univariate"),
last_merge = TRUE
) %>%
# Tidy-up
# Bold formatting of p-values
mutate_all(~cell_spec(.x, "html",bold = (as.numeric(stri_sub(.x, from=-6, to=-2)) < threshold )))%>%
# Table Labelling
rename(" R " = label) %>%
rename(" " = levels) -> t
Regards,
from finalfit.
Hi, thanks for this.
I find kableExtra
quite difficult to get to work with Word. I'd be interested in any working code you have.
Yes the default formatting removes a column heading which mutate()
has started complaining about.
This can avoided by including add_dependent_label = FALSE
. If a dependent label is desired it can be added in as per below (the reason this is here is if you are doing multiple tables and lose track of the outcome measure between tables).
Remember, the finalfit columns are characters, so any conditional logic will have to be string-based. Again see below.
You can force numeric outputs, but this is really for passing to plots rather than for a final formatted tables, finalfit(..., condense = FALSE)
.
I'm interested in how you get on, so please post back any results.
explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor", "nodes")
dependent = 'mort_5yr'
colon_s %>%
finalfit(dependent, explanatory, add_dependent_label = FALSE) %>%
mutate(
`OR (multivariable)` = cell_spec(`OR (multivariable)`, "latex",
bold = if_else(stringr::str_detect(`OR (multivariable)`, "<0.0"), TRUE, FALSE))
) %>%
dependent_label(colon_s, dependent)
from finalfit.
Hi Ewen
Thanks for your response. Really helpful.
This worked for me:
summary_factorlist(dependent, explanatory, p=1, add_dependent_label=FALSE, cont = "median", total_col = TRUE, condense=TRUE) %>% mutate(
p = cell_spec(
p, "html", bold = if_else(
p<0.05, TRUE, FALSE))) %>% kable(format = "html", escape = F, row.names = FALSE, linesep = "")%>% kable_styling(full_width = F, font_size = 12, bootstrap_options = c("condensed")) -> table1
For Word I solved it with copy and paste.
from finalfit.
I realize that if you include "metrics", the string solution is impossible.
Sot this is how I partially solved it.
%>%
finalfit(dependent, explanatory, explanatory_multi, digits = c(2,2,2), add_dependent_label = FALSE) %>%
mutate(
HR (univariable)
= cell_spec(HR (univariable)
, "html", bold = if_else(stringr::str_detect(HR (univariable)
, "<0.0"), TRUE, FALSE)),
HR (multivariable)
= cell_spec(HR (multivariable)
, "html", bold = if_else(stringr::str_detect(HR (multivariable)
, "<0.0"), TRUE, FALSE))
) %>%
kable(format = "html", escape = F, row.names = FALSE, linesep = "")%>%
kable_styling(full_width = F, font_size = 12, bootstrap_options = c("condensed")) -> table4
totest %>%
finalfit(dependent, explanatory, explanatory_multi, digits = c(2,2,2), add_dependent_label = FALSE, metrics = TRUE) -> table4.1
table4
kable(table4.1[[2]], row.names = FALSE, linesep = "") %>%
kable_styling(full_width = F, font_size = 12, bootstrap_options = c("condensed"))
from finalfit.
This is not elegant but works:
%>%
finalfit(dependent, explanatory, explanatory_multi, digits = c(2,2,2), add_dependent_label = FALSE) %>%
mutate(
`HR (univariable)` = cell_spec(`HR (univariable)`, "html", bold = if_else(stringr::str_detect(`HR (univariable)`, "<0.0")|stringr::str_detect(`HR (univariable)`, "0.01") |
stringr::str_detect(`HR (univariable)`, "0.02")|stringr::str_detect(`HR (univariable)`, "0.02")|
stringr::str_detect(`HR (univariable)`, "0.03")|stringr::str_detect(`HR (univariable)`, "0.04")
, TRUE, FALSE)),
`HR (multivariable)` = cell_spec(`HR (multivariable)`, "html", bold = if_else(stringr::str_detect(`HR (multivariable)`, "<0.0")|stringr::str_detect(`HR (multivariable)`, "0.01") |
stringr::str_detect(`HR (multivariable)`, "0.02")|stringr::str_detect(`HR (multivariable)`, "0.02")|
stringr::str_detect(`HR (multivariable)`, "0.03")|stringr::str_detect(`HR (multivariable)`, "0.04")
, TRUE, FALSE))
) %>%
kable(format = "html", escape = F, row.names = FALSE, linesep = "")%>%
kable_styling(full_width = F, font_size = 12, bootstrap_options = c("condensed")) ->
from finalfit.
What about this?
(I should say that I don't really agree with highlighting on the basis of a p-value :) )
colon_s %>%
finalfit(dependent, explanatory, digits = c(2,2,2), add_dependent_label = FALSE) %>%
bind_cols(colon_s %>%
finalfit(dependent, explanatory, digits = c(2,2,2),
add_dependent_label = FALSE, condense = FALSE) %>%
select(p.x, p.y)
) %>%
mutate(
`HR (univariable)` = cell_spec(`HR (univariable)`, "html", bold = if_else(p.x < 0.05, TRUE, FALSE)),
`HR (multivariable)` = cell_spec(`HR (multivariable)`, "html", bold = if_else(p.y < 0.05, TRUE, FALSE)),
) %>%
select(-c(p.x, p.y)) %>%
kable(format = "html", escape = F, row.names = FALSE, linesep = "")%>%
kable_styling(full_width = F, font_size = 12, bootstrap_options = c("condensed"))
from finalfit.
Nice solution, based on numeric outputs.
I agree with you in the non - highlighting of the p-value, but sometimes is very useful when you have a large number of variables.
Regards,
from finalfit.
I wonder if this generic approach deals with all/most circumstances?
explanatory = c("age", "nodes", "sex.factor", "obstruct.factor", "perfor.factor")
dependent = "Surv(time, status)"
dependent = "mort_5yr"
threshold = 0.05
table = colon_s %>%
finalfit(dependent, explanatory, digits = c(2,2,3), add_dependent_label = FALSE)
table %>%
select(matches("OR|HR|Coefficient"))%>%
purrr::map2_df(colon_s %>%
finalfit(dependent, explanatory, condense = FALSE) %>%
select(starts_with("p")),
~ if_else(.y < threshold, cell_spec(.x, "html", bold = TRUE), .x, .x)
) %>%
bind_cols(table %>% select(-matches("OR|HR|Coefficient")), .) %>%
kable(format = "html", escape = F, row.names = FALSE, linesep = "")%>%
kable_styling(full_width = F, font_size = 12, bootstrap_options = c("condensed"))
from finalfit.
Hi
The last solution gives this error
Error in
$<-.data.frame(
tmp, "all", value = 1L) : replacement has 1 row, data has 0
from finalfit.
Sorry, I can't replicate that error.
from finalfit.
Hi Ewen.
Problem solved with your last solution.
Thanks a lot!!
from finalfit.
Related Issues (20)
- finalfit does not show median, only mean HOT 1
- inverse OR in finalfit in R continuation closed event
- Fine-gray model visualization HOT 2
- Extending cont_cut to or_plot HOT 1
- Adjust axes for coef plots? HOT 1
- Multinomial Logistic Regression HOT 4
- About modifying the hr_plot function HOT 3
- Error with or_plot HOT 2
- `fct_explicit_na()` was deprecated in forcats 1.0.0 HOT 1
- missing_compare - discrepancy in the manual (Kruskal-Wallis) HOT 1
- `Namespace in Imports field not imported from: ‘tidyselect’` HOT 1
- The output from the stratified models example does not align with the results of the stratified Cox model. HOT 4
- Table format of the finalfit output only gives HTML format script HOT 4
- Potential bugs finalfit command HOT 1
- Finalfit package: Descriptive statistics query for p values and getting Median as statistics HOT 1
- p>0.05, but 95% confidence interval for the odds ratio DOES NOT include 1. How can I check and standardise the methods used to calculate the p-values and confidence intervals? HOT 2
- ff_remove_ref for summaryfactorlist HOT 4
- finalfit results in table with "Coefficient" instead of "OR" HOT 3
- ff_plot with interaction HOT 2
- Feature Request: Random effect in univariable models HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from finalfit.