使用预先编写的脚本读取R中的bin - UseMethod中的ERROR



我觉得这里的解决方案可能很容易,所以我很抱歉提前,如果我的问题是基本的,但我是新的R,我真的需要这个脚本的工作。

我正在尝试使用依赖于软件特定R包的预写脚本提取一些数据(bin在程序中作为报告生成,我需要这些报告以表格格式)。

这是我使用的代码:

*setwd("C:/Users/Oo/OneDrive/Documents/Evil_code_testing/")

minDateFilea <- '1991-01-01'
minDateFileb <- '2021-01-01'
firstYearAssessment <- '1991'
lastYearAssessment <- '2050'
RAC <- 0.0196
print('_____________________________________')
print('Defining function taking file paths and returning data')

getBinData <- function(patha, pathb) {
filea<-macroReadBin(patha)
fileb<-macroReadBin(patha)
nameRun<- substring(patha, (stri_locate_last_fixed(patha, "par")[,2] +1), (stri_locate_last_fixed(patha, ".bin")[,1]-2))
#Extract results for a and b
selectedfilea <- filea[c('Date','Drainage_mmh_Rate_total','Solute_concentration_Drain_flow')]
selectedfileb <- fileb[c('Date','Drainage_mmh_Rate_total','Solute_concentration_Drain_flow')]
#Reject the first 2 years
selectedfilea<-selectedfilea %>% filter(Date>minDateFilea)
selectedfileb<-selectedfileb %>% filter(Date>minDateFileb)
#Combine
selectedfile <-rbind(selectedfilea,selectedfileb)
#Summarise max PEC per year
selectedfile$PECsw <-((selectedfile$Drainage_mmh_Rate_total*selectedfile$Solute_concentration_Drain_flow*24)/(selectedfile$Drainage_mmh_Rate_total*24+3))
selectedfile$YEAR<-paste0(selectedfile$Date)
selectedfile$YEAR<-as.POSIXct(strptime(selectedfile$YEAR, format ="%Y-%m-%d %H:%M:%S", tz = "GMT"))
selectedfile$YEAR<-format(selectedfile$YEAR, format = "%Y")
selectedfile_stat<-selectedfile %>% group_by(YEAR) %>% summarise_at(vars(PECsw), list(max=max))
names(selectedfile_stat)[names(selectedfile_stat) == 'max'] <- nameRun
selectedfile_stat
}
print('_____________________________________')
print('Grab all bin files')
#Get a list of the files
files <- list.files(getwd(), ".bin")
soil<-substring(files, (stri_locate_last_fixed(files, "par")[,2] +1), (stri_locate_last_fixed(files, "par")[,2] +1))
weather<-substring(files, (stri_locate_last_fixed(files, "par")[,2] +2), (stri_locate_last_fixed(files, ".bin")[,1]-2))
part<-substring(files, (stri_locate_last_fixed(files, ".bin")[,1]-1), (stri_locate_last_fixed(files, ".bin")[,1]-1))
filesToTreat <- data.frame("Path" = files,"Soil" = soil, "Weather" = weather, "Part" = part)
print('_____________________________________')
print('match a/b together to determine selected scenarios')
filesToTreat$Matching_b<-paste(filesToTreat$Part)
for(i in 1:length(filesToTreat$Matching_b)){
if((filesToTreat$Part[i] =="a")&(filesToTreat$Part[i+1] =="b")&(filesToTreat$Soil[i+1] ==filesToTreat$Soil[i])&(filesToTreat$Weather[i+1] ==filesToTreat$Weather[i])){
filesToTreat$Matching_b[i] = filesToTreat$Path[i+1]
}else{filesToTreat$Matching_b[i] = "FALSE"}
}
print(filesToTreat)
print('_____________________________________')
print('Extract and merge selected scenarios together')
#create an empty dataframe
OriginalDataframe<- data.frame("YEAR" = numeric(0))
#for each scenario...
for(i in 1:length(filesToTreat$Matching_b)){
if(filesToTreat$Matching_b[i] != "FALSE"){
#extract drainflow rate and concentration data from bin files a&b and determine maxPEC per year
xpatha<- filesToTreat$Path[i]
xpathb<- filesToTreat$Matching_b[i]
xselectedfile_stat <- getBinData(xpatha, xpathb)
#Merge maxPEC data together
OriginalDataframe <-merge(xselectedfile_stat,OriginalDataframe,by="YEAR", all = TRUE)
print(paste('added data for soil', filesToTreat$Soil[i], 'and weather', filesToTreat$Weather[i]))
}
}*

这是导致错误的结果和错误之后的所有结果

> setwd("C:/Users/Oo/OneDrive/Documents/Evil_code_testing/")
> RAC <- 0.0196
> 
> print('_____________________________________')
[1] "_____________________________________"
> print('Defining function taking file paths and returning data')
[1] "Defining function taking file paths and returning data"
> 
> 
> getBinData <- function(patha, pathb) {
**+     filea<-macroReadBin(patha)
+     fileb<-macroReadBin(patha)**
+     nameRun<- substring(patha, (stri_locate_last_fixed(patha, "par")[,2] +1), (stri_locate_last_fixed(patha, ".bin")[,1]-2))
+     #Extract results for a and b
+     selectedfilea <- filea[c('Date','Drainage_mmh_Rate_total','Solute_concentration_Drain_flow')]
+     selectedfileb <- fileb[c('Date','Drainage_mmh_Rate_total','Solute_concentration_Drain_flow')]
+     #Reject the first 2 years
+     selectedfilea<-selectedfilea %>% filter(Date>minDateFilea)
+     selectedfileb<-selectedfileb %>% filter(Date>minDateFileb)
+     #Combine
+     selectedfile <-rbind(selectedfilea,selectedfileb)
+     #Summarise max PEC per year
+     selectedfile$PECsw <-((selectedfile$Drainage_mmh_Rate_total*selectedfile$Solute_concentration_Drain_flow*24)/(selectedfile$Drainage_mmh_Rate_total*24+3))
+     selectedfile$YEAR<-paste0(selectedfile$Date)
+     selectedfile$YEAR<-as.POSIXct(strptime(selectedfile$YEAR, format ="%Y-%m-%d %H:%M:%S", tz = "GMT"))
+     selectedfile$YEAR<-format(selectedfile$YEAR, format = "%Y")
+     selectedfile_stat<-selectedfile %>% group_by(YEAR) %>% summarise_at(vars(PECsw), list(max=max))
+     names(selectedfile_stat)[names(selectedfile_stat) == 'max'] <- nameRun
+     selectedfile_stat
+ }
> print('_____________________________________')
[1] "_____________________________________"
> print('Grab all bin files')
[1] "Grab all bin files"
> #Get a list of the files
> files <- list.files(getwd(), ".bin")
> soil<-substring(files, (stri_locate_last_fixed(files, "par")[,2] +1), (stri_locate_last_fixed(files, "par")[,2] +1))
> weather<-substring(files, (stri_locate_last_fixed(files, "par")[,2] +2), (stri_locate_last_fixed(files, ".bin")[,1]-2))
> part<-substring(files, (stri_locate_last_fixed(files, ".bin")[,1]-1), (stri_locate_last_fixed(files, ".bin")[,1]-1))
> filesToTreat <- data.frame("Path" = files,"Soil" = soil, "Weather" = weather, "Part" = part)
> 
> print('_____________________________________')
[1] "_____________________________________"
> print('match a/b together to determine selected scenarios')
[1] "match a/b together to determine selected scenarios"
> filesToTreat$Matching_b<-paste(filesToTreat$Part)
> for(i in 1:length(filesToTreat$Matching_b)){
+   if((filesToTreat$Part[i] =="a")&(filesToTreat$Part[i+1] =="b")&(filesToTreat$Soil[i+1] ==filesToTreat$Soil[i])&(filesToTreat$Weather[i+1] ==filesToTreat$Weather[i])){
+     filesToTreat$Matching_b[i] = filesToTreat$Path[i+1]
+   }else{filesToTreat$Matching_b[i] = "FALSE"}
+ }
> print(filesToTreat)
Path Soil  Weather Part Matching_b
1 parL109082WCa.bin    L 109082WC    a          2
2 parL109082WCb.bin    L 109082WC    b      FALSE
3 parL111074WCa.bin    L 111074WC    a          4
4 parL111074WCb.bin    L 111074WC    b      FALSE
5 parL129077WCa.bin    L 129077WC    a          6
6 parL129077WCb.bin    L 129077WC    b      FALSE
> 
> print('_____________________________________')
[1] "_____________________________________"
> print('Extract and merge selected scenarios together')
[1] "Extract and merge selected scenarios together"
> #create an empty dataframe
> OriginalDataframe<- data.frame("YEAR" = numeric(0))
> #for each scenario...
> for(i in 1:length(filesToTreat$Matching_b)){
+   if(filesToTreat$Matching_b[i] != "FALSE"){
+     #extract drainflow rate and concentration data from bin files a&b and determine maxPEC per year
+     xpatha<- filesToTreat$Path[i]
+     xpathb<- filesToTreat$Matching_b[i]
+     xselectedfile_stat <- getBinData(xpatha, xpathb)
+     #Merge maxPEC data together
+     OriginalDataframe <-merge(xselectedfile_stat,OriginalDataframe,by="YEAR", all = TRUE)
+     print(paste('added data for soil', filesToTreat$Soil[i], 'and weather', filesToTreat$Weather[i]))
+   }
+ }
Error in UseMethod("macroReadBin") : 
no applicable method for 'macroReadBin' applied to an object of class "factor"

我把粗体行,我认为造成的问题,但我不知道如何解决它。在WD中,我调用了许多文件,命名为000a, 000b, 999a, 999b等。所以我尝试将readbinmacro应用于特定的文件名,但它没有改变任何东西。

我真的需要理解这段代码,所以我真的很感激,如果有人能告诉不只是如何修复它,但我从根本上是错误的,所以我可以避免这种情况在未来。这段代码是由某人写的,我没有办法联系,所以不幸的是不能检查作者

解决方案非常简单。如果有人遇到类似的问题,这里有帮助

# Convert the class of file paths to character
filesToTreat$Path <- as.character(filesToTreat$Path)
# Call the function
result <- getBinData(filesToTreat$Path[1], filesToTreat$Path[2])