我正在嘗試從該路徑加載和繪制圖片:
C:\Users\Rayane_2\Desktop\Data\PCB-DATASET-master\PCB-DATASET-master\01_missing_hole_01.jpeg
我試過 :
library(imager)
file <- system.file('C:\Users\Rayane_2\Desktop\Data\PCB-DATASET-master\PCB-DATASET-master\01_missing_hole_01.jpeg',package='imager')
im <- load.image(file)
im # file not found
包提供的正確運行示例:
library(imager)
file <- system.file('extdata/parrots.png',package='imager')
#system.file gives the full path for a file that ships with a R package
#if you already have the full path to the file you want to load just run:
#im <- load.image("/somedirectory/myfile.png")
im <- load.image(file)
plot(im) #Parrots!
謝謝您的幫助 !
uj5u.com熱心網友回復:
必須轉義作為目錄分隔符的反斜杠,您應該已經看到了錯誤
Error: '\U' used without hex digits in character string starting ""C:\U"用另一個反斜杠轉義它,如
'C:\\Users\\Rayane_2\\Desktop\\Data\\PCB-DATASET-master\\PCB-DATASET-master\\01_missing_hole_01.jpeg'但是,即使在 Windows 上,也可以使用正斜杠,因此這也適用:
'C:/Users/Rayane_2/Desktop/Data/PCB-DATASET-master/PCB-DATASET-master/01_missing_hole_01.jpeg'system.file只在包中查找檔案。來自?system.file:Description Finds the full file names of files in packages etc. Arguments ...: character vectors, specifying subdirectory and file(s) within some package. The default, none, returns the root of the package. Wildcards are not supported.這意味著
...引數中提供的所有路徑都需要是相對的。一個這樣的例子是你在問題中提出的問題,system.file('extdata/parrots.png',package='imager')如果你看看已安裝的軟體包(可能的檔案結構
C:/Users/Rayane_2/R/win_library/4.1/imager),你會看到一個名為目錄Meta,R,data,doc,help,html,和(而不是在每個包中找到)extdata。在那個目錄中必須是parrots.png. 如果在指定包的安裝目錄中找到檔案,則回傳您查找的檔案的完整(絕對)路徑。該值的
system.file是,你可能不知道的完整路徑。當 (1) 以編程方式在其他用戶將使用您的代碼的情況下執行某些操作時,這是一種很好的方法;(2) 你有多個庫路徑,.libPaths()不知道哪個包含包,又不想自己全部檢查;或者 (3) 你想要更短、更多的自檔案化代碼。如果您已經知道檔案的完整路徑,則
system.file無濟于事。最重要的
system.file是,這是錯誤的功能。直接加載檔案即可。
library(imager) im <- load.image('C:/Users/Rayane_2/Desktop/Data/PCB-DATASET-master/PCB-DATASET-master/01_missing_hole_01.jpeg')
uj5u.com熱心網友回復:
使用其中之一。
欲了解更多資訊見?Quotes,?file.path,?Sys.getenv,?path.expand。該path.expand示例將取決于您的主目錄的設定方式,但通常已將其設定為C:\Users\yourname\Documents.
file.path("C:", "Users", "Rayane_2", "Desktop", "Data", "PCB-DATASET-master",
"PCB-DATASET-master", "01_missing_hole_01.jpeg")
file.path(Sys.getenv("USERPROFILE"), "Desktop", "Data", "PCB-DATASET-master",
"PCB-DATASET-master", "01_missing_hole_01.jpeg")
r"{C:\Users\Rayane_2\Desktop\Data\PCB-DATASET-master\PCB-DATASET-master\01_missing_hole_01.jpeg}"
"C:\\Users\\Rayane_2\\Desktop\\Data\\PCB-DATASET-master\\PCB-DATASET-master\\01_missing_hole_01.jpeg"
# this depends on how your home variable has been set but the
# setting is often such that this works
path.expand("~\\..\\Desktop\\Data\\PCB-DATASET-master\\PCB-DATASET-master\\01_missing_hole_01.jpeg")
"C:/Users/Rayane_2/Desktop/Data/PCB-DATASET-master/PCB-DATASET-master/01_missing_hole_01.jpeg"
# after entering this navigate to file. This will display the path
# to the file and you can then copy and paste it from the
# R console into your code.
file.choose()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394823.html
