replacing texts from global option in table

I have created a function which is creating a table like below, now i want to change the names of labels (25th percentile.25%,75th percentile.75%,Mean,Median...) from global option.

i was trying to convert it into dataframe then replace but structure is being changing.

df <- structure(list(row_labels = c("TT[summary]: context of table|25th percentile.25%", 
                              "TT[summary]: context of table|Mean", "TT[summary]: context of table|Median", 
                              "TT[summary]: context of table|75th percentile.75%", "TT[summary]: context of table|Max", 
                              "TT[summary]: context of table|Min", "TT[summary]: context of table|valid_nn"
), Total = c(62.5, 913.75, 933, 1745.75, 1749, 40, 4), Col1 = c(62.5, 
                                                                913.75, 933, 1745.75, 1749, 40, 4), COl2 = c(1736, 1736, 1736, 
                                                                                                             1736, 1736, 1736, 1)), row.names = c(NA, -7L), class = c("etable", 
                                                                                                                                                                      "data.frame"))
      

global options in rmarkdown are below

options(percentile25 = "25th Percentil")
options(percentile75 = "75th Percentil")
options(Mean_n = "Meana")
options(Med_n = "Mediana")
options(Min_n = "Minn")
options(Max_n = "Maxx")

is there any way where i can change the labels dynamically if the present in the table df for example if Mean present in df the replace it Mean only with Meana and median with mediana ....and so on

you seem to be asking about string replacement in your dataframe. So I will perform that.
You also want to get replacement names from options, so theres a step to collect those

df <- structure(list(row_labels = c("TT[summary]: context of table|25th percentile.25%", 
                                    "TT[summary]: context of table|Mean", "TT[summary]: context of table|Median", 
                                    "TT[summary]: context of table|75th percentile.75%", "TT[summary]: context of table|Max", 
                                    "TT[summary]: context of table|Min", "TT[summary]: context of table|valid_nn"
), Total = c(62.5, 913.75, 933, 1745.75, 1749, 40, 4), Col1 = c(62.5, 
                                                                913.75, 933, 1745.75, 1749, 40, 4), COl2 = c(1736, 1736, 1736, 
                                                                                                             1736, 1736, 1736, 1)), row.names = c(NA, -7L), class = c("etable", 
                                                                                                                                                                      "data.frame"))
df1 <- df # so you can see what we had before we changed it

options(percentile25 = "25th Percentil")
options(percentile75 = "75th Percentil")
options(Mean_n = "Meana")
options(Med_n = "Mediana")
options(Min_n = "Minn")
options(Max_n = "Maxx")

library(tidyverse)

 replacements_list <- list(
  `25th percentile.25%` = getOption("percentile25"),
  `75th percentile.75%` = getOption("percentile75"),
  Mean=getOption("Mean_n"),
  Median=getOption("Med_n"),
  Min = getOption("Min_n"),
  Max = getOption("Max_n")
     )

for(n in names(replacements_list)){
  i <- replacements_list[[n]]
  
  df <-  mutate(df,
              across(where(is.character),
                     \(x)str_replace_all(x,
                                         pattern=fixed(n),
                                         replacement=i)))
}

df

i am getting the error below

Error in mutate() :
i In argument: across(...) .
Caused by error in across() :
! Can't compute column row_labels .
Caused by error in str_replace_all() :
! pattern must be a vector, not a <criterion/function> object.

in my code as shared here ?
For me it reproduces when I restart clean.
my system is like so :

 sessionInfo()
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.utf8  LC_CTYPE=English_United Kingdom.utf8    LC_MONETARY=English_United Kingdom.utf8
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.2     purrr_1.0.2     readr_2.1.5     tidyr_1.3.0     tibble_3.2.1   
 [9] ggplot2_3.4.4   tidyverse_2.0.0

loaded via a namespace (and not attached):
 [1] rstudioapi_0.15.0 magrittr_2.0.3    hms_1.1.3         tidyselect_1.2.0  munsell_0.5.0     timechange_0.2.0  colorspace_2.1-0 
 [8] R6_2.5.1          rlang_1.1.3       fansi_1.0.4       tools_4.2.2       grid_4.2.2        gtable_0.3.4      utf8_1.2.4       
[15] cli_3.6.2         withr_2.5.0       lifecycle_1.0.3   tzdb_0.4.0        vctrs_0.6.5       glue_1.6.2        stringi_1.7.8    
[22] compiler_4.2.2    pillar_1.9.0      generics_0.1.3    scales_1.2.1      pkgconfig_2.0.3

FOR ME IT ISN'T WORKING, SHOULD I UPDATE R ..??

well, you can try to remove fixed() if it causes you issues; though it shouldn't (meaning it doesnt for me :slight_smile: )

you mean pattern = n ..???

yes, that's what I mean.

its not replacing sir after removing fixed

maybe you have problems from conflicts with other packages you have open that I am not using.
Perhaps you can try a clean restart of your R session, and then run the code I provided. I expect it would work.

please explain a lillte bit how you found the solution, to understand what i was not thinking about ..

the solution of text replacement ? or restarting your r-session ?

text replacement yes

I guess I just worked backwards. you said you wanted to replace text;
stringr is a good package for that, it has str_replace_all.
you need to know what to replace, where, and what with.
I made replacements_list out of what you indicated you wanted to replace;
then i loop around that, and pass it to str_replace_all

photo1707759262

one more thing sir, if i will have more stats other tan stats list like in the pic . i mean i want to make dynamic like if he find that string in the table then replace .

some times table will have all stats like above or sometimes i will have on max , min, Total cases, missing values etc . then waht should i update ..??

the str_replace_all function, is dynamic you can replace any string with any thing...
If your process isnt standardardised and must be fully dynamic, you cant beat the primitives, i.e. str_replace_all

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.