【问题标题】:Using tapply in Shiny to find mean of a column在 Shiny 中使用 tapply 查找列的平均值
【发布时间】:2017-10-17 05:43:19
【问题描述】:

我在使用 tapply 功能时遇到了麻烦。我从从反应变量创建的同一个数据帧中提取两个向量。第一个是我从用户输入的选择中调用的,第二个是我创建的,以保持我的代码通用性并在我的排序函数中使用。下面使用 r-bloggers 示例显示了我的示例代码。数据在这里。 https://redirect.viglink.com/?format=go&jsonp=vglnk_150821851345614&key=949efb41171ac6ec1bf7f206d57e90b8&libId=j8v6cnh201021u9s000DAhzunvtas&loc=https%3A%2F%2Fwww.r-bloggers.com%2Fbuilding-shiny-apps-an-interactive-tutorial%2F&v=1&out=http%3A%2F%2Fdeanattali.com%2Ffiles%2Fbcl-data.csv&ref=https%3A%2F%2Fduckduckgo.com%2F&title=Building%20Shiny%20apps%20%E2%80%93%20an%20interactive%20tutorial%20%7C%20R-bloggers&txt=here

它抛出的错误是它们的长度不同,即使它们的属性和类打印输出完全相同。 我知道这不是世界上最好的代码,但我只是整理了一个简单的例子。

    library(shiny)
library(tidyverse)
bcl <- read.csv("bcl-data.csv", stringsAsFactors = FALSE)

ui <- fluidPage(titlePanel("Sampling Strategies"),
                sidebarLayout(
                  sidebarPanel(
                    selectInput("XDATA","xdata",
                                choices = c(names(bcl))),
                    selectInput("YDATA","ydata",
                                choices = c(names(bcl)))
                  ),

                  mainPanel(
                    tabsetPanel(
                      tabPanel("The table",tableOutput("mytable"))
                    ))
                ))

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

  filtered <- reactive({
    bcl <- bcl %>% mutate(ID = 1:nrow(bcl))
  })

  output$mytable <- renderTable({
    dataset <- filtered() %>% mutate(sampled = "white")
    sample.rows <- sample(dataset$ID, 5, replace = FALSE)
    dataset$sampled[sample.rows] <- "black"
    final <- tapply(dataset[input$XDATA], list(dataset$sampled),mean)[["black"]]

    return(final)
  })
}

shinyApp(ui = ui, server = server)

干杯 编辑*对不起,我的错,忘记更改下拉列表代码。我感兴趣的是一个可以从加载的数据集中选择的通用 xdata 向量。然后我对其进行采样,并希望从采样索引中找到平均值。

【问题讨论】:

  • 我认为应该是 data.frame(dataset$sampled) 而不是 list(dataset$sampled) 另外,您的第一个参数似乎是 data.frame。请通过可重复的示例阐明您想要的内容
  • 那些xdata、ydata是什么?
  • 已编辑。我想要做的就是从指定的列中返回采样索引的平均值 input$XDATA

标签: r shiny tapply


【解决方案1】:

其中一个问题在于子集。 [ 仍然返回 data.frame。所以,我们需要[[。如果我们看?tapply

tapply(X, INDEX, FUN = NULL, ..., 默认 = NA, 简化 = TRUE)

在哪里

X 是一个原子对象,通常是一个向量


ui <- fluidPage(titlePanel("Sampling Strategies"),
                sidebarLayout(
                  sidebarPanel(
                    selectInput("XDATA","xdata",
                                choices = c(names(bcl)[5:7])),
                    selectInput("YDATA","ydata",
                                choices = c(names(bcl)))
                  ),

                  mainPanel(
                    tabsetPanel(
                      tabPanel("The table",tableOutput("mytable"))
                    ))
                ))


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

  filtered <- reactive({
    bcl <- bcl %>% mutate(ID = row_number())
  })



  output$mytable <- renderTable({

    dataset <- filtered() %>% mutate(sampled = "white")
    sample.rows <- sample(dataset$ID, 20, replace = FALSE)
    dataset$sampled[sample.rows] <- "black"
    final <- tapply(dataset[[input$XDATA]], list(dataset$sampled),mean, na.rm = TRUE, simplify = TRUE)

    return(final)


  })

}

shinyApp(ui = ui, server = server)

-输出

【讨论】:

  • 非常感谢。我试过双括号,但使用 dataset$sampled 作为数据框。正在运行检查,它们在属性方面完全相同。为帮助干杯!赞成,但我是SO的新手,所以它并不重要。对不起。
猜你喜欢
  • 2017-12-23
  • 2018-07-12
  • 2020-08-10
  • 1970-01-01
  • 2012-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多