【问题标题】:Grouped table of means by variable in shiny按闪亮变量分组的均值表
【发布时间】:2019-08-02 01:15:00
【问题描述】:

我正在尝试使用fileInput 获取用户输入的 CSV,并通过用户选择的项目输出分组表,即 x 轴和 y 轴包含下拉菜单,然后从 x 轴输出分组表并显示分组的方法y 轴。现在,当我运行我的代码时,它将对 x 轴进行分组,但在平均值列下它显示所有值的平均值。

server <- shinyServer(function(input, output, session) {

data1 <- reactive({
req(input$file1)
inFile <- input$file1
df <- read.csv(inFile$datapath, header = input$header, sep = input$sep,
               quote = input$quote)

updateSelectInput(session, inputId = 'xcol', label = 'X Axis',
                  choices = names(df), selected = names(df)[1])
updateSelectInput(session, inputId = 'ycol', label = 'Y Axis',
                  choices = names(df), selected = names(df)[2])
return(df)
  })
output$tabset1Selected <- renderTable({
mean.table<-group_by(data1(), as.factor(data1()[,input$xcol])) %>%
  summarise(
    count = n(),
    mean = mean(data1()[,input$ycol], na.rm = TRUE),
    se = std.error(data1()[,input$ycol], na.rm = TRUE))
  })

我也无法运行HSD.test

res.anova <- aov(data1()[,input$ycol]~data1()[,input$xcol], data=dataset)     
HSDtest.measurement <- HSD.test(res.anova,"treatment", group = TRUE)

res.anova 函数有效,但是当我尝试运行 HSD.test 时,我不确定在“治疗”部分放置什么以使其运行。

【问题讨论】:

    标签: r shiny dplyr


    【解决方案1】:

    我们可以将group_by 中的代码更改为group_by_at,因为它是一个字符串输入,然后将input$ycol 更改为symbol 并评估(!!)以提取值

    output$tabset1Selected <- renderTable({
          data1() %>%
                group_by_at(vars(input$xcol)) %>%
                summarise(
                    count = n(),
                    mean = mean(!! rlang::sym(input$ycol), na.rm = TRUE),
                    se = std.error(!! rlang::sym(input$ycol), na.rm = TRUE))
               })
    

    它显示所有值的平均值,因为我们正在提取整列。例如

    iris %>%
            group_by(Species) %>%
            summarise(Mean = mean(iris$Sepal.Length)) # breaks the grouping info
    

    相反,我们只需要列名

    iris %>%
            group_by(Species) %>%
            summarise(Mean = mean(Sepal.Length)) 
    

    当我们得到一个字符串列名作为输入时,它。被转换为symbol 并被评估(!!

    iris %>%
            group_by(Species) %>%
            summarise(Mean = mean(!! rlang::sym("Sepal.Length"))) 
    

    【讨论】:

    • 非常感谢!另外,如果我要运行HSD.test(y, txt ...),我如何将xcol 输入用于trt 部分?我一直遇到错误,因为它通常有引号。
    • @Seezium。您能否在您的帖子中更新该部分。可以类似!! rlang::sym(input$xcol)
    • 我更新了。我很困惑,因为格式需要引号,这是一个反应元素。
    • @Seezium。它是一个公式,因此您可以使用paste 创建公式。而不是aov(data1()[,input$ycol]~data1()[,input$xcol], data=dataset) 将是aov(as.formula(paste0(input$ycol, " ~ ", input$xcol)), data = dataset) 不清楚是data = dataset 还是data = data1()
    • aov 公式有效,但我在使用 HSD.test 时遇到问题,因为如果我输入“input$xcol”之类的内容,处理部分会用引号引起来,它会返回 NULL
    【解决方案2】:

    我认为您在将输入转换为可在 dplyr group_by 中使用并汇总调用的表单时遇到问题。你可以试试这样:

        output$tabset1Selected <- renderTable({
            data1() %>%
                group_by(!! rlang::sym(input$xcol)) %>%
                summarise(count = n(),
                          mean = mean(!! rlang::sym(input$ycol)))
        })
    

    如果您希望您的输出列被称为更具描述性的名称,您可以这样做:

        output$tabset1Selected <- renderTable({
            col_name <- paste0("mean_", input$ycol)
            data1() %>%
                group_by(!! rlang::sym(input$xcol)) %>%
                summarise(count = n(),
                          !!col_name := mean(!! rlang::sym(input$ycol)))
        })
    

    【讨论】:

      猜你喜欢
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 2023-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多