R 有光泽的ezANOVA输入

R 有光泽的ezANOVA输入,r,shiny,R,Shiny,因此,我在列出函数中的输入时遇到了一些问题,尤其是ezANOVA。以下是迄今为止我对代码的了解: ui.R: 服务器.R: library(shiny) library(ez) shinyServer(function(input, output) { csvfile <- reactive({ csvfile <- input$file1 if (is.null(csvfile)){return(NULL)} dt <- read.csv(cs

因此,我在列出函数中的输入时遇到了一些问题,尤其是ezANOVA。以下是迄今为止我对代码的了解:

ui.R:

服务器.R:

library(shiny)
library(ez)

shinyServer(function(input, output) {

  csvfile <- reactive({
    csvfile <- input$file1
    if (is.null(csvfile)){return(NULL)}
    dt <- read.csv(csvfile$datapath, header=input$header, sep=input$sep)
    dt
  })

  output$var <- renderUI({
    if(is.null(input$file1$datapath)){return()}

    else{
      return(list(radioButtons("estimate", "Please Pick The Dependent Variable",  choices = names(csvfile())),
              radioButtons("between1", "Please Pick The Between Subjects Factor", choices = names(csvfile())),
              radioButtons("within1", "Please Pick The Within Subjects Factor", choices = names(csvfile())),
              radioButtons("sid", "Please Pick The Subject Id Variable", choices = names(csvfile())),
              actionButton("submit", "Submit")))
  }

})


output$aovSummary = renderTable({

  if(is.null(input$file1$datapath)){return()}

  if(input$submit > 0){

    aov.out <- ezANOVA(data = csvfile(), dv = .(input$estimate), wid = .(input$sid), between = .(input$between1), 
                   within = .(input$within1), detailed = TRUE, type = "III")
    return(aov.out)

  }
})
})
我在浏览器中收到的错误是:

输入$estimate不是提供的数据框中的变量

因此,函数ezANOVA没有使用实际的变量名,而是使用字符串input$estimate,这不是我想要它做的

我该如何着手解决这个问题,还是无能为力?
提前感谢您的帮助

您需要动态构造对ezANOVA的调用,即使用输入变量中字符串的值来定义函数调用。由于LISP的传统,通过eval在R中实现这一点相对容易。相对容易,因为在R中字符串仍然很痛苦,您需要操纵字符串来实现这一点。下面是代码的最低工作版本

服务器.R

我已经更改/修复了一些较小的问题,但与eval无关的两个最重要的更改是:

包括一个选项,让R用空白作为字段分隔符来完成它通常的事情。 更改了渲染函数以包含实际的方差分析表。ezANOVA返回一个列表,该列表的第一个条目始终是ANOVA并包含ANOVA表。然而,有时会有更多的假设测试和事后修正条目,例如Mauchly的球形度测试和Huynh-Feldt修正。当它们出现时,你真的需要添加逻辑来处理它们。 代码风格也是一个问题——最好先去掉空的if块,然后是一个完整的else,而只测试实际运行代码的条件。让R从函数的末尾掉下来,模拟一个不存在的返回值

我假设UI改进正在等待一个工作示例,但您需要考虑:

对于不同的参数和/或不响应单选按钮,而只是响应操作按钮,有意义的默认值(可能在变量类型上)。否则,在设置值时,ezANOVA会出现令人困惑的错误。 如果您有纯中间或纯内部设计,会发生什么?
您可能还想查看隐藏更多选项的方法,直到以有意义的方式设置初始选项数据文件。

Awesome!谢谢你的帮助!你说得对,我把代码控制在最低限度以免混淆。由于我正在处理的数据使用了混合过程方差分析,我现在只坚持使用它,但我计划将此代码用于许多不同的设计。谢谢你为我澄清了评估方法。我还将确保进行其他建议的更改:。如果您愿意回答,我还有一个问题,我将如何计算say csvfile[,input$between1]的向量?否则方差分析测试将不起作用。evalparsetext=paste0d$,输入$between1,
library(shiny)
library(ez)

shinyServer(function(input, output) {

  csvfile <- reactive({
    csvfile <- input$file1
    if (is.null(csvfile)){return(NULL)}
    dt <- read.csv(csvfile$datapath, header=input$header, sep=input$sep)
    dt
  })

  output$var <- renderUI({
    if(is.null(input$file1$datapath)){return()}

    else{
      return(list(radioButtons("estimate", "Please Pick The Dependent Variable",  choices = names(csvfile())),
              radioButtons("between1", "Please Pick The Between Subjects Factor", choices = names(csvfile())),
              radioButtons("within1", "Please Pick The Within Subjects Factor", choices = names(csvfile())),
              radioButtons("sid", "Please Pick The Subject Id Variable", choices = names(csvfile())),
              actionButton("submit", "Submit")))
  }

})


output$aovSummary = renderTable({

  if(is.null(input$file1$datapath)){return()}

  if(input$submit > 0){

    aov.out <- ezANOVA(data = csvfile(), dv = .(input$estimate), wid = .(input$sid), between = .(input$between1), 
                   within = .(input$within1), detailed = TRUE, type = "III")
    return(aov.out)

  }
})
})
    Animal  Visit   Dose    Estimate
    2556    0   3   1.813206946
    2557    0   3   1.933397744
    2558    0   3   1.689893603
    2559    0   3   1.780301984
2560    0   3   1.654374476
2566    0   10  3.401283412
2567    0   10  3.015958525
2568    0   10  2.808705611
2569    0   10  3.185718418
2570    0   10  2.767128836
2576    0   30  3.941412617
2577    0   30  3.793328436
2578    0   30  4.240736154
2579    0   30  3.859611218
2580    0   30  4.049743097
2586    0   100 5.600261483
2587    0   100 5.588115651
2588    0   100 5.089081008
2589    0   100 5.108262681
2590    0   100 5.343876403
2556    27  3   1.453587471
2557    27  3   1.994413484
2558    27  3   1.638132168
2559    27  3   2.138289747
2560    27  3   1.799769874
2566    27  10  3.302851871
2567    27  10  3.014199997
2568    27  10  3.190990162
2569    27  10  3.577924375
2570    27  10  3.537461068
2576    27  30  4.470837132
2577    27  30  4.081833308
2578    27  30  4.497192825
2579    27  30  4.205494309
2580    27  30  4.234496088
2586    27  100 6.054284369
2587    27  100 5.436697078
2588    27  100 5.398721492
2589    27  100 4.990794986
2590    27  100 5.573305744
2551    0   3   1.838550166
2552    0   3   1.847992942
2553    0   3   1.349892703
2554    0   3   1.725937126
2555    0   3   1.534652719
2561    0   10  2.931535704
2562    0   10  2.947599556
2563    0   10  3.092658629
2564    0   10  2.837625632
2565    0   10  2.970227467
2571    0   30  4.00746885
2572    0   30  3.921844968
2573    0   30  3.575724773
2574    0   30  4.17137839
2575    0   30  4.25251528
2581    0   100 4.785295667
2582    0   100 5.610955803
2583    0   100 5.497109771
2584    0   100 5.262724458
2585    0   100 5.430003698
2551    27  3   1.9326519
2552    27  3   2.313193186
2553    27  3   1.815261865
2554    27  3   1.345218914
2555    27  3   1.339432001
2561    27  10  3.305894401
2562    27  10  3.192621055
2563    27  10  3.76947789
2564    27  10  3.127887366
2565    27  10  3.231750087
2571    27  30  4.306556353
2572    27  30  4.232038905
2573    27  30  4.042378186
2574    27  30  4.784843929
2575    27  30  4.723665015
2581    27  100 5.601181262
2582    27  100 5.828647795
2583    27  100 5.652171222
2584    27  100 5.326512658
2585    27  100 6.009774247
library(shiny)
library(ez)

shinyServer(function(input, output) {

    csvfile <- reactive({
        csvfile <- input$file1
        if (is.null(csvfile)){return(NULL)}
        dt <- read.csv(csvfile$datapath, header=input$header, sep=input$sep)
        dt
    })

    output$var <- renderUI({
        if(!is.null(input$file1$datapath)){
            d <- csvfile()
            anova.opts <- list(
                            radioButtons("estimate", "Please Pick The Dependent Variable",  choices = names(d)),
                            radioButtons("between1", "Please Pick The Between Subjects Factor", choices = names(d)),
                            radioButtons("within1", "Please Pick The Within Subjects Factor", choices = names(d)),
                            radioButtons("sid", "Please Pick The Subject Id Variable", choices = names(d)),
                            actionButton("submit", "Submit")
                        )
            anova.opts
        }

    })


    output$aovSummary = renderTable({

        if(!is.null(input$submit)){

            aov.out <- eval(parse(text=paste(
                                "ezANOVA(data = csvfile()
                               , dv = .(", input$estimate, ") 
                               , wid = .(", input$sid, ")
                               , between = .(", input$between1, ")
                               , within = .(", input$within1, ")
                               , detailed = TRUE, type = \"III\")")))
        aov.out$ANOVA
        }
    })
})
library(shiny)

shinyUI(pageWithSidebar(

    headerPanel('Analysis of Variance'),

    sidebarPanel(

        fileInput("file1", "CSV File", accept=c("text/csv", "text/comma-separated-values,text/plain", ".csv")),

        checkboxInput("header", "Header", TRUE),

        radioButtons('sep', 'Separator',c(Comma=',',Semicolon=';',Tab='\t', `White Space`=''),''),

        uiOutput('var') 
    ),

    mainPanel( 

        tableOutput('aovSummary')

    )
)
)