闪亮地计算数据框中列的平均值 [英] Shiny calculate the mean of columns in dataframe

查看:131
本文介绍了闪亮地计算数据框中列的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算数据框中所有列的平均值,但出现错误:参数不是数字或逻辑:返回NA. 这是我正在使用的代码:

I'm trying to calculate the mean of all columns in a data frame but I get the error : argument is not numeric or logical: returning NA. Here's the code I'm using:

UI.R

library(shiny)
library(shinydashboard)
library(plyr)
library(reshape2)

#library(data.table)

shinyUI(pageWithSidebar(
  headerPanel("CSV Viewer"),
  sidebarPanel(
    fileInput('file1', 'Choose CSV File',
              accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv'))


  ),
  mainPanel(

    tableOutput('data'),
    verbatimTextOutput('mean')

  )
))

server.UI

shinyServer(function(input, output,session) {

  output$data <- renderTable({
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    data<-data.frame(read.csv(inFile$datapath, header=FALSE, sep=";"))

  })
  output$mean<-renderPrint({

   mean(data)

  })
})

我完全迷路了,无法弄清错误.请帮我.谢谢.

I'm completely lost and I can't figure out the error. please help me. thanks.

推荐答案

好吧,所以您的主要问题是因为您试图获取数据帧的均值,而使用mean()函数是不可能的.该代码应该可以工作(请注意,因为您没有提供我编写的任何示例数据-我在其中添加了一个date列,并且该函数没有取其平均值).

Ok so your main problem si that you're trying to take the mean of a data frame, which is not possible using the mean() function. This code should work (note since you didn't give any example data I made some up - I threw in a date column in there and the function doesn't take the mean of it).

shinyServer(function(input, output,session) {

  dataset <- reactive({
    #Change the below line
    data.frame(a=c(1,2,3),b=c(2,4,5),c=as.Date(c("12/31/09","12/31/10","12/31/11"),"%m/%d/%y"))

  })

  output$mean<-renderPrint({

   colMeans(dataset()[,which(sapply(dataset(), class) != "Date")])

  })
})

请注意,我调用了dataset(),这使我可以引用数据.这应该给您预期的输出.请注意,我还在数据集中所有非Date类型的列上调用了colMeans函数.您还应该能够用您的数据框定义代替我的.也就是说,在我的代码中,您只需在renderTable()调用中将data.frame(a=c(1,2,3),b=c(2,4,5),c=as.Date(c("12/31/09","12/31/10","12/31/11"),"%m/%d/%y"))替换为您的代码即可.

Note that I call dataset() which allows me to reference the data. This should give you your expected output. Note that I also call the colMeans function on all the columns in the dataset that are not of the type Date. You should also be able to substitute your dataframe definition for mine. That is, in my code you would just replace data.frame(a=c(1,2,3),b=c(2,4,5),c=as.Date(c("12/31/09","12/31/10","12/31/11"),"%m/%d/%y")) with your code in your renderTable() call.

希望这会有所帮助.

根据下面的注释,您可以在renderTable调用中定义数据并在renderPrint中进行引用.两个主要问题是read.csv()中的数据定义和在数据帧上调用mean的尝试.后一个问题由colMeans()解决,前一个问题由@Mouna中的代码解决:dataset<-data.frame(read.csv(inFile$datapath, header = FALSE, sep = ";", dec = ","))

Per comments below, it turns out you can can define your data in the renderTable call and reference it in the renderPrint. The two main problems were the definition of the data in the read.csv() and the attempt to call mean on dataframe. The latter problem is fixed by colMeans() and the former was fixed using code from @Mouna : dataset<-data.frame(read.csv(inFile$datapath, header = FALSE, sep = ";", dec = ","))

这篇关于闪亮地计算数据框中列的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆