I am very new shiny user. I wanted to show the results of post-hoc test using emmeans function. Here's the code of my shiny app.
ui <- shinyUI(fluidPage(
# Application title
titlePanel("Statistical analyses "),
sidebarLayout(
sidebarPanel(
# select variable/factors
selectInput("Depedent_Var", "Select your y : the dependent variable",
choices = c("length_ch2","total_count_ch2")),
selectInput("factor1", "Select the first factor",selected = "Vector",
choices = c("Vector","Time")),
selectInput("factor2", "Select the second factor",selected = "Time",
choices = c("Vector","Time")),
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Statistical test : Anova", tabsetPanel(
tabPanel("Significance of the main effects",DT::dataTableOutput("ANOVA")),
tabPanel("Tukey's post hoc tests 1",DT::dataTableOutput("Tukey1"))))
)
)
)
))
server.r
server <- shinyServer(function(input, output) {
data <- reactive({Neurite_all_no})
output$ANOVA<-DT::renderDataTable({
if (input$Depedent_Var=="length_ch2") {
LM.fit<-lm(sqrt(get(input$Depedent_Var))~get(input$factor1)*get(input$factor2),data=data())
Anovaa=(Anova(LM.fit))
rownames(Anovaa)<-c(input$factor1,input$factor2,paste0(input$factor1,":",input$factor2),"inter")
DT::datatable(data.frame(Anovaa)[-4,])%>%
formatRound(columns =1:length(Anovaa[1,]),digits = 2)
}
})
output$Tukey1<-DT::renderDataTable({
if (input$Depedent_Var=="length_ch2") {
LM.fit<-lm(sqrt(get(input$Depedent_Var))~get(input$factor1)*get(input$factor2),data=data())
em<-emmeans(LM.fit,~get(input$factor2)|get(input$factor1),type="response")
aa=pairs(regrid(em),adjust="bonferroni")
DT::datatable(data.frame(aa))
}
})
})
################################################################################
# Run the application
shinyApp(ui = ui, server = server)
The shiny app:
all good for the first tab, but I get this error using emmeans function:
Why does the function emmeans is not recognizing my "Vector" factor?
CodePudding user response:
Try this change to the if
statement inside your output$Tukey1
. Basically, I suggest building up the formulae, used in the lm()
and the emmeans()
calls
if (input$Depedent_Var=="length_ch2") {
lm_formula = as.formula(paste0("sqrt(",input$Depedent_Var,")~",input$factor1,"*", input$factor2))
LM.fit<-lm(lm_formula,data=data())
em_formula = as.formula(paste0("~",input$factor2,"|", input$factor1))
em<-emmeans(LM.fit,em_formula,type="response")
aa=pairs(regrid(em),adjust="bonferroni")
DT::datatable(data.frame(aa))
}