简体   繁体   English

尝试将多个 jpeg 写入 r 中的文件时出错

[英]error trying to write multiple jpegs to file in r

I have over 600 WAV files I am trying to plot and write to a file.我有超过 600 个 WAV 文件,我正在尝试 plot 并写入文件。 I made a loop to do this, but it gives an error that says "Error in audiomoth13_allfiles@left: trying to get slot "left" from an object of a basic class ("list") with no slots"我做了一个循环来执行此操作,但它给出了一个错误,显示“audiomoth13_allfiles@left 中的错误:试图从没有插槽的基本 class(“列表”)的 object 中获取插槽“左””

Below is my code that produces the error.下面是我产生错误的代码。

for (audiomoth13wavfiles in seq_along(audiomoth13_allfiles)) {
audiomoth13_allfiles<-lapply(audiomoth13wavfiles,readWave)
snd<-audiomoth13_allfiles@left 
fs<-audiomoth13_allfiles@samp.rate
dur<-length(snd)/audiomoth13_allfiles@samp.rate
snd<-snd-mean(snd)
timearray<-(0:(5760000-1))/audiomoth13_allfiles@samp.rate
jpeg(file= paste0("C:/ACLF_Audiomoth_13/ACLF_Audiomoth_13/plots",names(audiomoth13wavfiles) 
[audiomoth13wavfiles],".jpeg"))
plot(timearray,snd,type='l',xlab='Time',ylab='Amplitude')
dev.off()} 

The error occurs whether or not I put audiomoth13_allfiles<-lapply(audiomoth13wavfiles,readWave) in the loop.无论我是否将 audiomoth13_allfiles<-lapply(audiomoth13wavfiles,readWave) 放入循环中,都会发生错误。 audiomoth13_allfiles contains all the files as an S4 object with slots audiomoth13_allfiles 包含所有文件作为 S4 object 带插槽

Is there another way to do this?还有另一种方法可以做到这一点吗? Thanks.谢谢。

You are conflating a for loop with lapply , which is sort of redundant.您将for循环与lapply ,这有点多余。 Try something akin to what is shown below (or use a for loop instead of lapply ).尝试类似于下面显示的内容(或使用for循环而不是lapply )。 The idea is to loop through the indices of all.wav files.这个想法是遍历 all.wav 文件的索引。 This way you can use the same index for the file name and the file read in using readWave .这样,您可以对文件名和使用readWave读取的文件使用相同的索引。

library(tuneR)
wavfiles <- list.files(pattern="*.wav")
lapply(seq_along(wavfiles), function(x){
    wav <- readWave(wavfiles[x])
    snd <- wav@left
    fs <- wav@samp.rate
    dur <- length(snd)/fs
    snd <- snd - mean(snd)
    timearray <- seq(0, dur, length.out=length(snd))
    jpeg(file=paste0(tools::file_path_sans_ext(wavfiles[x]), "_plot.jpg"))
        plot(snd~timearray, type='l', xlab='Time', ylab='Amplitude')
    dev.off()}
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM