R - “文件错误(文件,ifelse(追加,”a“,”w“)):无法打开连接”

时间:2016-07-08 12:32:43

标签: r dataframe

我有这种形式的数据框:

JdbcRowSet jdbcRowSet = new com.sun.rowset.JdbcRowSetImpl(jdbcTemplate.getDataSource().getConnection());
jdbcRowSet.setConcurrency(ResultSet.CONCUR_READ_ONLY);
jdbcRowSet.setReadOnly(true);
jdbcRowSet.setCommand(sql);
jdbcRowSet.setString(1, value);
jdbcRowSet.execute();
jdbcRowSet.setFetchDirection(ResultSet.FETCH_FORWARD);
jdbcRowSet.setFetchSize(100);

我想循环遍历各行,每行检查第一列(X1)的名称&amp;在我的计算机中创建一个具有相同名称的文件夹,并在此文件夹中创建与其各自列(X2),(X3),(X4)具有相同名称的子文件夹。当我运行脚本时,我只能看到文件夹 R290601 WOVEN TWILL ,子文件夹 001 6 231 < / strong>,但不是其余的,我收到此错误:

  

文件错误(文件,ifelse(追加,“a”,“w”)):     无法打开连接

此外,我收到第二行的警告:

  

在dir.create中(paste0(pth,df $ X1 [i])):     无法使用穿孔L3-0,3创建目录'C:\ Users \ Dev \ Desktop \ Joe \ 009-1373 *,原因'无效参数'

我的代码是:

X1                                      X2 X3                       X4
R290601 WOVEN TWILL                    001  6                      231
009-1373  *with perforated L3-0,3      152 NA                     <NA>
R481400 THREAD                       A1282 12                    A0399
0091375 PURE SOCK                      001  6                      072
R282380 SOFTLIN W/FELT                 007  6                      072
R282480 MICROFIBRE                     001  6                      F72
R281200 ARTIFICIAL                   A0638  6                      072

为什么我会收到此错误,如何查看所有文件夹及其相应的子文件夹?

1 个答案:

答案 0 :(得分:0)

library(readxl)
library(tidyverse)
## First read the dataframe taht contains Folder & Sub-folder names
df <- read_excel("C:/Users/Dev/Desktop/Joe/df.xlsx") 


## Special characters like "\ / : * ? " < > |" cann't be present in filename,
## So first remove it from the df for every column.
df <- df %>% 
  mutate(across(everything(), .fns = function(x) gsub('[[:punct:]]','', x))) ## '[[:punct:]]' for the special character


setwd('C:/Users/Dev/Desktop/Joe') ## Replace with your working directory



for(i in 1:nrow(df)){
  
  df_name <- df[i, ] %>% 
    select_if(~ !is.na(.x)) %>% 
    select_if(~ .x != 'NA') ## if NA is character & you don't need it
  
  for(j in 1:ncol(df_name)){
    
    if(!is.na(df_name[,j])){
      if(!dir.exists(as.character(df_name[,j]))){
        dir.create(as.character(df_name[,j]), recursive = TRUE)
        setwd(as.character(df_name[,j])) ## Set wd to the newly created dir
        ## X1 --> X2 --> X3 --> X4
      }
    }
    
  }
  
  setwd('C:/Users/Dev/Desktop/Joe') ## Again go back to the main wd

}

## IF you want the folder like this : X1 --> X2, X3, X4 then 

for(i in 1:nrow(df)){
  
  df_name <- df[i, ] %>% 
    select_if(~ !is.na(.x)) %>% 
    select_if(~ .x != 'NA') ## if NA is character & you don't need it
  
  if(!is.na(df_name[,1])){
    if(!dir.exists(as.character(df_name[,1]))){
      dir.create(as.character(df_name[,1]), recursive = TRUE)
      setwd(as.character(df_name[,1])) ## Set wd to the newly created dir
    }
  }
  
  for(j in 2:ncol(df_name)){
    
    if(!is.na(df_name[,j])){
      if(!dir.exists(as.character(df_name[,j]))){
        dir.create(as.character(df_name[,j]), recursive = TRUE)
      }
    }
    
  }
  
  setwd('C:/Users/Dev/Desktop/Joe') ## Again go back to the main wd
  
}