第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

檢查目錄是否存在,如果不存在則創(chuàng)建

檢查目錄是否存在,如果不存在則創(chuàng)建

函數(shù)式編程 2020-02-04 15:17:02
我經(jīng)常發(fā)現(xiàn)自己寫的R腳本會(huì)產(chǎn)生大量輸出。我發(fā)現(xiàn)將其輸出放入其自己的目錄更加干凈。我在下面編寫的內(nèi)容將檢查目錄是否存在并移入該目錄,或者創(chuàng)建目錄然后移入該目錄。有沒(méi)有更好的方法來(lái)解決這個(gè)問(wèn)題?mainDir <- "c:/path/to/main/dir"subDir <- "outputDirectory"if (file.exists(subDir)){    setwd(file.path(mainDir, subDir))} else {    dir.create(file.path(mainDir, subDir))    setwd(file.path(mainDir, subDir))}
查看完整描述

3 回答

?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

用途showWarnings = FALSE:


dir.create(file.path(mainDir, subDir), showWarnings = FALSE)

setwd(file.path(mainDir, subDir))

dir.create()如果該目錄已經(jīng)存在,則不會(huì)崩潰,它只會(huì)打印出警告。因此,如果您可以看到警告,那么這樣做就沒(méi)有問(wèn)題:


dir.create(file.path(mainDir, subDir))

setwd(file.path(mainDir, subDir))


查看完整回答
反對(duì) 回復(fù) 2020-02-04
?
幕布斯6054654

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊

自2015年4月16日起,隨著的發(fā)布,R 3.2.0有一個(gè)名為的新功能dir.exists()。要使用此功能并創(chuàng)建目錄(如果目錄不存在),可以使用:


ifelse(!dir.exists(file.path(mainDir, subDir)), dir.create(file.path(mainDir, subDir)), FALSE)

FALSE如果目錄已經(jīng)存在或TRUE無(wú)法創(chuàng)建,并且目錄不存在但創(chuàng)建成功,則將返回該目錄。


請(qǐng)注意,只需檢查目錄是否存在,即可使用


dir.exists(file.path(mainDir, subDir))


查看完整回答
反對(duì) 回復(fù) 2020-02-04
?
RISEBY

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊

就一般體系結(jié)構(gòu)而言,我建議在目錄創(chuàng)建方面采用以下結(jié)構(gòu)。這將涵蓋大多數(shù)潛在的問(wèn)題,并且dir.create呼叫將檢測(cè)到與目錄創(chuàng)建有關(guān)的任何其他問(wèn)題。


mainDir <- "~"

subDir <- "outputDirectory"


if (file.exists(paste(mainDir, subDir, "/", sep = "/", collapse = "/"))) {

    cat("subDir exists in mainDir and is a directory")

} else if (file.exists(paste(mainDir, subDir, sep = "/", collapse = "/"))) {

    cat("subDir exists in mainDir but is a file")

    # you will probably want to handle this separately

} else {

    cat("subDir does not exist in mainDir - creating")

    dir.create(file.path(mainDir, subDir))

}


if (file.exists(paste(mainDir, subDir, "/", sep = "/", collapse = "/"))) {

    # By this point, the directory either existed or has been successfully created

    setwd(file.path(mainDir, subDir))

} else {

    cat("subDir does not exist")

    # Handle this error as appropriate

}

另請(qǐng)注意,如果~/foo不存在,則dir.create('~/foo/bar')除非您指定,否則對(duì)的調(diào)用將失敗recursive = TRUE。


查看完整回答
反對(duì) 回復(fù) 2020-02-04
  • 3 回答
  • 0 關(guān)注
  • 875 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)