【问题标题】:R: Dplyr 'group_by' does not seem to work in Shiny [duplicate]R:Dplyr 'group_by' 似乎在 Shiny 中不起作用 [重复]
【发布时间】:2017-12-04 09:51:48
【问题描述】:

我刚开始尝试使用 Shiny,但无法让 dplyr 'group_by' 函数正常工作。

我的一般想法是,我有一个包含许多数值变量的数据集和一个可以对这些变量进行分组的变量(在我的例子中是 19 个国家,因此变量范围从 1 到 19)。 现在我想使用 metafor 包对每个国家/地区的两个变量之间的相关性进行元分析。 所以我想分别计算每个国家的两个选定变量之间的相关性,然后将它们放在一个元分析中,最后显示这些影响的森林图。

我以 mtcars 数据集为例说明我已经走了多远:

用户界面

library(shiny)
library(metafor)
library(dplyr)


ui <- fluidPage(
  sidebarPanel(

    selectInput("x", label = "Choose Variable I", 
    choices = c("Displacement (cu.in.)", "Horsepower", "Rear axle ratio",
    "Weight (1000lbs)", "1/4 mile time"), selected = "Displacement (cu.in.)"),

    selectInput("y", label = "Choose Variable II", 
    choices = c("Displacement (cu.in.)", "Horsepower", "Rear axle ratio",
    "Weight (1000lbs)", "1/4 mile time"), selected = "Horsepower")
  ),

  mainPanel(
    plotOutput(outputId = "plot")
    ) 
  )

服务器

 server <- function(input, output) {


  output$plot <-renderPlot({

    data_x <- switch(input$x, 
                     "Displacement (cu.in.)" = mtcars$disp,
                     "Horsepower" = mtcars$hp,
                     "Rear axle ratio" = mtcars$drat,
                     "Weight (1000lbs)" = mtcars$wt,
                     "1/4 mile time" = mtcars$qsec)

    data_y <- switch(input$y, 
                     "Displacement (cu.in.)" = mtcars$disp,
                     "Horsepower" = mtcars$hp,
                     "Rear axle ratio" = mtcars$drat,
                     "Weight (1000lbs)" = mtcars$wt,
                     "1/4 mile time" = mtcars$qsec)

    meta_main <- mtcars %>% 
      group_by(gear) %>% 
      summarise(participantID = n(), 
                correlation = cor(data_x, data_y, use = "complete.obs"))


    meta <- rma(ni=participantID, ri=correlation, 
                method="REML", measure="COR", data=meta_main)


    forest(meta) 

  })

}

shinyApp(ui = ui, server = server)

在示例中,我按“齿轮”(有 3 个级别)进行分组,并选择了变量“排量”、“马力”、“后轴比”、“重量”和“1/4 英里时间”。

最终输出显示选择小部件以及森林图。 但是,所有级别的“齿轮”的相关性都是相同的。因此,我怀疑“group_by”功能无法按预期工作。

我尝试按照网上的建议使用“group_by_”,但变化不大。

关于“group_by”如何与 Shiny 一起工作的任何想法?

【问题讨论】:

  • 当您尝试将group_by_ 用作suggested 时,您是否也使用了summarize_

标签: r group-by shiny dplyr metafor


【解决方案1】:

试试这个服务器功能,首先你应该加载lazyeval包。

server <- function(input, output) {

output$plot <-renderPlot({

data_x <- switch(input$x, 
                 "Displacement (cu.in.)" = "disp",
                 "Horsepower" = "hp",
                 "Rear axle ratio" = "drat",
                 "Weight (1000lbs)" = "wt",
                 "1/4 mile time" = "qsec")

data_y <- switch(input$y, 
                 "Displacement (cu.in.)" = "disp",
                 "Horsepower" = "hp",
                 "Rear axle ratio" = "drat",
                 "Weight (1000lbs)" = "wt",
                 "1/4 mile time" = "qsec")

meta_main <- mtcars %>% 
  group_by(gear) %>% 
  mutate(participantID = n()) %>%
  group_by(gear, participantID) %>% 
  summarise_(correlation = interp(~cor(x, y, use = "complete.obs"), x = as.name(data_x), y = as.name(data_y)))


meta <- rma(ni=participantID, ri=correlation, 
            method="REML", measure="COR", data=meta_main)


forest(meta) 

})

}

【讨论】:

  • 太棒了!这样可行。非常感谢,我已经尝试了很多年了!我需要看看标准的评估方法。
  • 请注意,这些方法现在已经过时了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
  • 2013-06-19
  • 1970-01-01
相关资源
最近更新 更多