i just want to give a parameter like by which it can trim table like below, for example if i give new paramter n = 10 then it should show first 10 records and bottom 10 records and trim the rest of records without changing the original percentage values
This is easiest to do with the slice function from dplyr I think. I used the iris dataset and the variable x to make the difference clearer with the n() function from dplyr that's counting the rows. I'm also showing the original rowId just to verify the cuts are correct. This is not needed.
What is the purpose of this? If you just want to display the top to look at it, the function I provided (and yours it seems as well) will work as long as you don't save the output and thus overwrite the original dataframe.
so for example if a particular variable have many outliers (may be more than 100-500) the the requirement is to shrink the table with top 10 and bottom 10 and having "...." between table which indicates that there is more data between tables, this is already implemented in vba output file , now i want to implement it into R.
Note: there is one more thing like the table gets displayed on the basis of banner which i didn't included in my reproducible example. i am rely on expss package because the requirement is to display tables in expss like output.
It seems that what you want is not possible with the expss functions, but I did manage to find a way to manipulate the output by capturing the output to the console and changing it. It works nicely, but of course you'll need to tweak some values if the format of the table changes, because this only works for the given example for now.
library(tidyverse)
library(expss)
endRows = 5 #Number of rows to keep start / end
#Data
df <- nycflights13::flights
var_lab(df[["distance"]])<-"Table 1"
#Build table
t1<- cro_cpct(df[["distance"]])
#Capture the table output on the console as stings
newOutput = capture.output(t1)
#Cut out the lines not needed and add "..."
newOutput = c(
newOutput[1:(endRows+3)], #Header + first rows
"", #optional blank line
str_pad("...", nchar(newOutput[1]), "both"), #Add the ...
"", #optional blank line
newOutput[(length(newOutput)-endRows):length(newOutput)] #last rows + footer
)
#Paste all back together and print
newOutput %>% paste(collapse = "\n") %>% cat()
#>
#> | | | #Total |
#> | ------- | ------------ | -------- |
#> | Table 1 | 17 | 0.0 |
#> | | 80 | 0.0 |
#> | | 94 | 0.3 |
#> | | 96 | 0.2 |
#> | | 116 | 0.1 |
#>
#> ...
#>
#> | | 2576 | 0.1 |
#> | | 2586 | 2.4 |
#> | | 3370 | 0.0 |
#> | | 4963 | 0.1 |
#> | | 4983 | 0.1 |
#> | | #Total cases | 336776.0 |