Git Product home page Git Product logo

Comments (11)

emmanuels777 avatar emmanuels777 commented on July 29, 2024 1

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.

testgit-healthyr avatar testgit-healthyr commented on July 29, 2024

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.

 avatar commented on July 29, 2024

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.

 avatar commented on July 29, 2024

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.

 avatar commented on July 29, 2024

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.

testgit-healthyr avatar testgit-healthyr commented on July 29, 2024

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.

 avatar commented on July 29, 2024

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.

ewenharrison avatar ewenharrison commented on July 29, 2024

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.

 avatar commented on July 29, 2024

Hi

The last solution gives this error

Error in $<-.data.frame(tmp, "all", value = 1L) : replacement has 1 row, data has 0

from finalfit.

testgit-healthyr avatar testgit-healthyr commented on July 29, 2024

Sorry, I can't replicate that error.

from finalfit.

emmanuels777 avatar emmanuels777 commented on July 29, 2024

Hi Ewen.
Problem solved with your last solution.
Thanks a lot!!

from finalfit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.