Function

コメント・参照等

FUN = f_read_KeikiDoukouShisuu.r
function (download_folder) 
{
    lapply(c("XLConnect", "Nippon"), require, character.only = T)
    path_to_folder <- paste0("C:/Users/", Sys.info()["user"], download_folder)
    setwd(path_to_folder)
    xls_files <- dir()
    xls_files <- sort(xls_files, decreasing = F)
    cnt <- 0
    for (iii in seq(xls_files)) {
        if (grepl("(ci|cont)[0-9]", xls_files[iii])) {
            cnt <- cnt + 1
            sheet_name0 <- XLConnect::getSheets(XLConnect::loadWorkbook(xls_files[iii]))
            sheet_name <- gsub("(^[^(a-z\\s )]+).+", "\\1", sheet_name0, ignore.case = T)
            buf0 <- readWorksheetFromFile(xls_files[iii], sheet = 1, check.names = F, header = F)
            if (grepl("ci[0-9]", xls_files[iii])) {
                colnames(buf0) <- paste0(buf0[2, ], buf0[4, ])
                buf1 <- buf0[-c(1:5), ]
            }
            else {
                colnames(buf0) <- paste0("寄与度:", buf0[2, ])
                buf1 <- buf0[-c(1:3), ]
            }
            buf1[, 1:3] <- apply(buf1[, 1:3], 2, as.numeric)
            buf2 <- buf1[!is.na(buf1[, 1]), ]
            buf3 <- data.frame(Date = as.Date(paste0(buf2[, 2], "-", buf2[, 3], "-1")), buf2[, -c(1:3)], check.names = F, row.names = NULL, stringsAsFactors = F)
            buf3[, -1] <- apply(buf3[, -1], 2, function(x) as.numeric(gsub(",", "", x)))
            colnames(buf3)[-1] <- paste0(sheet_name, ":", sapply(gsub("\\s{2,}|\n|NA", "", colnames(buf3)[-1]), zen2han))
            if (cnt == 1) {
                datadf <- buf3
            }
            else {
                datadf <- merge(datadf, buf3, by = "Date", all = T)
            }
        }
    }
    colnames(datadf)[-1] <- paste0("景気動向指数個別系列:", colnames(datadf)[-1])
    return(datadf)
}