2012-12-03 74 views
3

我在Refer to/select a drive based only on its label? (i.e., not the drive letter)找到了一個有用的腳本,可以在cmd.exe窗口或cygwin下運行它,在我的電腦上查找驅動器號。如何將vbs腳本的輸出返回到R控制檯?

我想不出的是如何讓返回的字符串(例如E:)顯示在我的R控制檯中。如果我運行system('cscript /nologo DriveFromLabel.vbs label',intern=TRUE),那麼我會得到character(0)

有一些開關,以便在cmd.exe呼叫至R可見此評論的結果,或者是有一些方法來創建調用cygwin並返回cscript結果與R腳本?

+0

是否打印的東西,但它不會在變量結束了? –

回答

1

這對我的作品

system('Cscript /nologo your_path/DriveFromLabel.vbs DRIVE_LABEL',intern=TRUE)[1] 
+0

好當然!在我的Windows7系統上,它是'... [1]',但是解決了這個問題。它必須是某種「返回(不可見())」事情。它總是很簡單:-)。 –

+0

@Carl沒有,因爲你使用/ nologo選項。我更新我的答案 – agstudy

2

可能是cscript寫入stderr而不是stdout。 (test.sh)一個小例子使用以下bash腳本:

echo spam 1>&2 

也產生中沒有捕獲的結果:

> spam = system("./test.sh", intern = TRUE) 
spam 
> spam 
character(0) 

Linux下將溶液現在是重定向stderrstdout

> spam = system("./test.sh 2>&1", intern = TRUE) 
> spam 
[1] "spam" 

你可以看看this link在Windows下重定向stderr。也感謝Brian Ripley回答this R-help post。的system文檔證實了我的故事:

For command-line R, error messages written to ‘stderr’ will be 
sent to the terminal unless ‘ignore.stderr = TRUE’. They can be 
captured (in the most likely shells) by 

    system("some command 2>&1", intern=TRUE) 

Stdout and stderr:標題下。

+0

'system'的R文檔說雖然收集了stderr和stdout。 –

+0

但我的小例子顯示相反。只有stdout被'system'捕獲。也許這意味着顯示'stderr'和'stdout',但'intern = TRUE'時只捕獲'stdout'。 –

+0

這在一般情況下非常有用,即使對於我簡單的調用,檢查的答案是最簡單的。我將保存您的筆記,以便將來用於更復雜的系統調用。 –

2

我想我找到了一個方法: system("C:/cygwin/bin/bash.exe ./doletter.sh",intern=TRUE),其中doletter包含 cscript /nologo DriveFromLabel.vbs label'成功返回的驅動器盤符。
現在,我只需要做一些技巧來將所需的「標籤」字符串加載到shell腳本中,通過R函數從頭開始創建doletter.sh文件即可「輕鬆」完成。

相關問題