Conducting t-test and effect size with reactive input variable

Hello community,

This is hopefully quite a quick fix, but I have been pulling my hair out on this all day.

I am working to put p-values and effect-sizes (Cohen's d) onto ggplot graphs. In Rmd, I calculate the values separately and then add them with annotating to the plots. However, this doesn't seem to be so easy in Shiny.

  • User-uploaded data frame which folks upload from a csv file: df

  • The dataframe includes a continuous numeric variable: var3

  • Users select categorgical variables from a list of relevant dataset variables to include in the graphs: input$var1 and input$var2

What I think isn't working:

  • I have had good success in using get(input$var1) in all my ggplots, but I can't seem to make a similar option for the other functions. The variables live in the dataframe, but not in the environment, I use get() to locate the name of the variable in the dataframe. Is there something else I can use?

  • I would like to identify the as the first factor. I have tried (head(input$var1),1) and = [1]. Any other suggestions?

  • I don't feel like calling the two calculated variables as functions is a clean fix: C_d() and df_p_val(). Any other suggestions?


options = c("Gender", "Age", "Education", "Income", "MaritalStatus") # these are all variables in the df which will be uploaded by users. 
ui <- fluidPage(
    selectInput("var1", "facet1", choices = options),
    selectInput("var2", "facet2", choices = options), 

... ) # end UI

server <- function(input, output, session) {
... # other code including csv upload of df and some data cleaning

  df_p_val <- reactive({
        #perform a t-test and obtain the p-value 
         rstatix::t_test(df, var3 ~ get(input$var1) , = [1]) %>% 

C_d <- reactive({
        #calculate cohen's d effect size
         cohens_d(var3 ~ get(input$var1), data = df)
# boxplot including p.adj.signif and effsize for var3 ~ var1 
 output$plot1 <- renderPlot( ggplot(df, aes(x = var3, fill = get(input$var1)) ) +  geom_boxplot( ) +
                             facet_grid(get(input$var1) ~ get(input$var2)) +
                              add_pvalue(df_p_val(), label = "p.adj.signif")
                             annotate("text", x = .9, y = 1.1, label = (round(C_d()$effsize, digits = 2)))
} # end server

This topic was automatically closed 54 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.