使用函数中的参数创建新类

时间:2019-01-16 13:57:04

标签: r class methods

目标::我想在我创建的函数的summary输出中添加“分布”和“参数”。

我创建的函数采用n和分布的选择,并从该分布返回随机数。该函数的输出是类MyClass的对象。

下面我为summary的对象创建了MyClass函数,该函数当前返回输出的长度,均值和标准差。

问题::我希望summary函数也能打印出random_number_generator函数的原始参数,请参见下面的期望的输出。

random_number_generator <- function(n,distribution,mean=NULL,sd=NULL,lambda=NULL,size=NULL,prob=NULL){
  if (distribution=="Normal"){
   x <- rnorm(n=n,mean=mean,sd=sd)
  }
  
  if (distribution=="Poisson"){
  x <- rpois(n=n, lambda=lambda)
  }
  
 if (distribution=="Binomial") {
  x <- rbinom(n=n,size=size,prob=prob)
 }
  
  class(x) <- "MyClass"
  return (x)
}

answer <- random_number_generator(n=10, distribution = "Binomial",size=15,prob=0.4)


summary.MyClass <- function(x) {
    stopifnot(inherits(x, "MyClass"))
    cat("\t\n", 
        sprintf("Sample size: %s\n", length(x)),
        sprintf("Mean: %s\n", mean(x)),
        sprintf("Standard Deviation: %s\n", sd(x))
        )
}

当前输出:

summary(answer)
	
 Sample size: 10
 Mean: 4.9
 Standard Deviation: 1.59513148186739

期望的输出:

class(answer)
[1] "MyClass"

summary(answer)

    Sample size: 10
    Mean: 4.9
    Standard Deviation: 1.59513148186739
    Distribution: "Binomial"
    Size: 15
    Prob: 0.4

2 个答案:

答案 0 :(得分:2)

我们可以执行以下操作:

random_number_generator <- function(n, distribution, mean = NULL, sd = NULL, lambda = NULL, size = NULL, prob = NULL) {
  if (distribution == "Normal")
    x <- rnorm(n = n, mean = mean, sd = sd)
  else if (distribution == "Poisson")
    x <- rpois(n = n, lambda = lambda)
  else if (distribution == "Binomial")
    x <- rbinom(n = n, size = size, prob = prob)
  out <- list(x = x, distribution = distribution, size = size, prob = prob)
  class(out) <- "MyClass"
  out
}

summary.MyClass <- function(obj) {
  stopifnot(inherits(obj, "MyClass"))
  cat("\t\n", 
      sprintf("Sample size: %s\n", length(obj$x)),
      sprintf("Mean: %s\n", mean(obj$x)),
      sprintf("Standard Deviation: %s\n", sd(obj$x)),
      sprintf("Distribution: %s\n", obj$distribution),
      sprintf("Size: %d\n", obj$size),
      sprintf("Probability: %s\n", obj$prob)
  )
}

answer <- random_number_generator(n = 10, distribution = "Binomial", size = 15, prob = 0.4)
summary(answer)
#   
#  Sample size: 10
#  Mean: 5.5
#  Standard Deviation: 1.64991582276861
#  Distribution: Binomial
#  Size: 15
#  Probability: 0.4

因此,要返回额外的信息,我们需要将此额外的信息保留为random_number_generator的输出,这与我所做的

out <- list(x = x, distribution = distribution, size = size, prob = prob)
class(out) <- "MyClass"

答案 1 :(得分:1)

您还可以将参数存储为返回值的属性。这样一来,如果您不想要,就不会得到列表:

 private void audio_recorder_init() throws IOException {

        String ses_datam = "/mnt/sdcard2/rota/ses.aac";

        // Byte array for audio record
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        ParcelFileDescriptor[] descriptors = ParcelFileDescriptor.createPipe();
        ParcelFileDescriptor parcelRead = new ParcelFileDescriptor(descriptors[0]);
        ParcelFileDescriptor parcelWrite = new ParcelFileDescriptor(descriptors[1]);

        InputStream inputStream = new ParcelFileDescriptor.AutoCloseInputStream(parcelRead);

        //Audio Recorder initialize
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); //for mp3= TREE_GPP
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);    //for mp3= AMR_NB
        recorder.setAudioEncodingBitRate(48000);//48000
        recorder.setAudioSamplingRate(16000);//16000

        //recorder.setOutputFile(path2);
        recorder.setOutputFile(parcelWrite.getFileDescriptor());

        try {
            recorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        recorder.start();



        int read;
        byte[] data = new byte[16384];

        while ((read = inputStream.read(data, 0, data.length)) != -1) {
            byteArrayOutputStream.write(data, 0, read);
            byte[] ses_data = byteArrayOutputStream.toByteArray();

            writeToFile(ses_data,ses_datam);
        }

        byteArrayOutputStream.flush();



        }