Shiny 中 emmeans 函数输出的错误消息:"object 'factor' not found"

Error message in emmeans function output in Shiny : "object 'factor' not found"

我是一个非常新的闪亮用户。我想使用 emmeans 函数显示 post-hoc 测试的结果。这是我闪亮的应用程序的代码。

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)

闪亮的应用程序:

第一个选项卡一切正常,但我使用 emmeans 函数时出现此错误:

为什么函数 emmeans 无法识别我的“向量”因子?

尝试更改 output$Tukey1 中的 if 语句。基本上,我建议建立在 lm()emmeans() 调用中使用的公式

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))
}