tbl_summary function

Hello everyone, I am currently working on the package "gtsummary" and use its function tbl_summary to create descriptive statistics table. I want to add a 95% CI of mean to the table and according to this article FAQ + Gallery • gtsummary I should add the function "ll" and "ul"

This is my code:

ll <- function(x) t.test(x)$conf.int[[1]] # Lower 95% CI of mean
ul <- function(x) t.test(x)$conf.int[[2]] # Upper 95% CI of mean
      
# create table 1
table <- df() %>% select(input$indStat,input$depStat) %>% 
        tbl_summary(by=input$indStat,type=all_continuous()~"continuous2",statistic = list(all_continuous()~c("{mean}","{sd}","{ll}","{ul}","{median}", "{p25} , {p75}","{min} , {max}")),missing="no",digits = all_continuous() ~ 3) 

however, it shows an error as follows:

Warning: Error in : Problem with mutate() input df_stats.
x Problem with mutate() input value.
x could not find function "ll"
i Input value is purrr::map2_dbl(...).
i Input df_stats is pmap(...).
129:

I am creating a shiny web app and anyone has idea how to solve this one? I dont understand why it has something to do with mutate() function and why it could not find function "ll". Thank you so much!

Can you post an example data set illustrating the problem? When I run your code on the trial data included with gtsummary, I don't see any error.

ll <- function(x) t.test(x)$conf.int[[1]] # Lower 95% CI of mean
ul <- function(x) t.test(x)$conf.int[[2]] # Upper 95% CI of mean

# create table 1
table <-
  trial %>%
  select(trt, age) %>%
  tbl_summary(
    by = trt,
    type = all_continuous() ~ "continuous2",
    statistic = all_continuous() ~ c("{mean}", "{sd}", "{ll}", "{ul}", "{median}", "{p25} , {p75}", "{min} , {max}"),
    missing = "no",
    digits = all_continuous() ~ 3
  )

That works when I run the code in R console, but unfortunately when I try to put it in the R Shiny Script it shows the error. So the problem apparently located on the function "ll" and "ul", somehow if I put it in the R Shiny script within the server, it can not read the function and gives the error that "x could not find function ll". But when I remove the ll and ul from gtsummary table, it works fine. Here is example in shiny code:

library(gtsummary)
library(gt)
library(dplyr)

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      gt_output("tblsummary"),
    ) )
)

# Define server logic required to draw a histogram ----
server <- function(input, output) {
  
  output$tblsummary <- render_gt({
    ll <- function(x) t.test(x)$conf.int[[1]] # Lower 95% CI of mean
    ul <- function(x) t.test(x)$conf.int[[2]] # Upper 95% CI of mean
    
    # create table 1
    table <-
      trial %>%
      select(trt, age) %>%
      tbl_summary(
        by = trt,
        type = all_continuous() ~ "continuous2",
        statistic = all_continuous() ~ c("{mean}", "{sd}", "{ll}", "{ul}", "{median}", "{p25} , {p75}", "{min} , {max}")
      ) %>% as_gt()
    
  })
  
}
shinyApp(ui, server)

Hmmm, I am not sure! I don't use Shiny much. I'll ask around and report back!

Thank you so much! that would be great! :smiley:

It's a scoping issue. That code belongs outside of the server step. Confirmed following code works.

Nate

library(gtsummary)
library(gt)
library(dplyr)
library(shiny)

ll <- function(x) t.test(x)$conf.int[[1]] # Lower 95% CI of mean
ul <- function(x) t.test(x)$conf.int[[2]] # Upper 95% CI of mean

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      gt_output("tblsummary"),
    ) )
)

# Define server logic required to draw a histogram ----
server <- function(input, output) {

  output$tblsummary <- render_gt({
   
    
    # create table 1
    table <-
      trial %>%
      select(trt, age) %>%
      tbl_summary(
        by = trt,
        type = all_continuous() ~ "continuous2",
        statistic = all_continuous() ~ c("{mean}", "{sd}", "{ll}", "{ul}", "{median}", "{p25} , {p75}", "{min} , {max}")
      ) %>% as_gt()
    
  })
  
}
shinyApp(ui, server)
1 Like

I have tried that one as well and it does not work, still the same error above appears. Does it work when you run it?

Ok, well let's use the global assignment and see if that works.

library(gtsummary)
library(gt)
library(dplyr)
library(shiny)


ll <<- function(x) t.test(x)$conf.int[[1]] # Lower 95% CI of mean
ul <<- function(x) t.test(x)$conf.int[[2]] # Upper 95% CI of mean
ui <- fluidPage(
    titlePanel("Hello Shiny!"),
    sidebarLayout(
        sidebarPanel(),
        mainPanel(
            gt_output("tblsummary"),
        ) )
)

# Define server logic required to draw a histogram ----
server <- function(input, output) {
    
    output$tblsummary <- render_gt({
        
        
        # create table 1
        table <-
            trial %>%
            select(trt, age) %>%
            tbl_summary(
                by = trt,
                type = all_continuous() ~ "continuous2",
                statistic = all_continuous() ~ c("{mean}", "{sd}", "{ll}", "{ul}", "{median}", "{p25} , {p75}", "{min} , {max}")
            ) %>% as_gt()
        
    })
    
}

# Run the application 
shinyApp(ui = ui, server = server)
3 Likes

That worked for me! Thank you @Nate884 !

That worked for me too! Thank you so much @Nate884 for your help! :smile: :smile:

It works pretty well

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.