2010-11-19 27 views
8

我一直在使用Vim在Windows中編寫Stata腳本,現在在大學裏。我現在正在學習R,並且我想完全切換到Linux作爲我的操作系統(我最近在我的筆記本電腦上切換到了Ubuntu)。 R在Windows和Linux中都能很好地與Vim兼容,但是我仍然需要使用Stata。在Windows中,我一直使用由Stata用戶提供的簡單AutoIt腳本將行/整個文件發送給stata進行評估。這個腳本在Linux中不起作用。將代碼從vim發送到stata

這是劇本的樣子

; AutoIt v3 script to run a Stata do-file from an external text editor 
; Version 3.1, Friedrich Huebler, [email protected], www.huebler.info, 30 March 2009 

; Declare variables 
Global $ini, $statapath, $statawin, $dofile, $winpause, $keypause, $clippause 

; File locations 
; Path to INI file 
$ini = @ScriptDir & "\rundo.ini" 
; Path to Stata executable 
$statapath = IniRead($ini, "Stata", "statapath", "C:\Program Files\Stata10\wsestata.exe") 
; Title of Stata window 
$statawin = IniRead($ini, "Stata", "statawin", "Stata/SE 10.1") 

; Path to do-file that is passed to AutoIt 
; Edit line to match editor used, if necessary 
$dofile = $CmdLine[1] 

; Delays 
; Pause after copying of Stata commands to clipboard 
$clippause = IniRead($ini, "Delays", "clippause", "100") 
; Pause between window-related operations 
$winpause = IniRead($ini, "Delays", "winpause", "200") 
; Pause between keystrokes sent to Stata 
$keypause = IniRead($ini, "Delays", "keypause", "1") 

; Set SendKeyDelay and WinWaitDelay to speed up or slow down script 
Opt("WinWaitDelay", $winpause) 
Opt("SendKeyDelay", $keypause) 

; If more than one Stata window is open, the window 
; that was most recently active will be matched 
Opt("WinTitleMatchMode", 2) 

; Check if Stata is already open, start Stata if not 
If WinExists($statawin) Then 
    WinActivate($statawin) 
    WinWaitActive($statawin) 
    ; Activate Stata Command Window and select text (if any) 
    Send("^4") 
    Send("^a") 
    ; Run saved do-file 
    ; Double quotes around $dofile needed in case path contains blanks 
    ClipPut("do " & '"' & $dofile & '"') 
    ; Pause avoids problem with clipboard, may be AutoIt or Windows bug 
    Sleep($clippause) 
    Send("^v" & "{Enter}") 
Else 
    Run($statapath) 
    WinWaitActive($statawin) 
    ; Activate Stata Command Window 
    Send("^4") 
    ; Run saved do-file 
    ; Double quotes around $dofile needed in case path contains blanks 
    ClipPut("do " & '"' & $dofile & '"') 
    ; Pause avoids problem with clipboard, may be AutoIt or Windows bug 
    Sleep($clippause) 
    Send("^v" & "{Enter}") 
EndIf 

; End of script 

具有以下在我的vimrc

" STATA DO-FILE SCRIPTS 

fun! RunIt() 
    w 
    !start "C:\Programme\Stata10\integvim\rundo3\rundo.exe" "%:p" 
endfun 
map <F8> :<C-U>call RunIt()<CR><CR> 
imap <F8> <Esc>:<C-U>call RunIt()<CR><CR> 

fun! RunDoLines() 
    let selectedLines = getbufline('%', line("'<"), line("'>")) 
if col("'>") < strlen(getline(line("'>"))) 
    let selectedLines[-1] = strpart(selectedLines[-1], 0, col("'>")) 
    endif 
if col("'<") != 1 
    let selectedLines[0] = strpart(selectedLines[0], col("'<")-1) 
    endif 
let temp = tempname() . ".do" 
    call writefile(selectedLines, temp) 
    exec "!start C:\\Programme\\Stata10\\integvim\\rundo3\\rundo.exe " . temp 
    au VimLeave * exe "!del -y" temp 
endfun 
map <F9> :<C-U>call RunDoLines()<CR><CR> 
imap <F9> <Esc>:<C-U>call RunDoLines()<CR><CR> 

這是真正實用,實際上我還是堅持到Windows的唯一原因。我將如何去做這樣的Ubuntu的東西?我是linux新手,除了統計數據外,對編程知之甚少。任何幫助是極大的讚賞。 (請不要提示emacs,emacs對stata的支持有問題,雖然它與R的集成好得多,但我仍想繼續使用Vim。)

關於可能相關的主題:我正在考慮學習Python,因爲我可能會使用數據並進行更長時間的實證分析,而且我認爲它可能對某些任務有用,例如解決像這樣的問題或從網站解析數據。這是建議,還是應該看另一種語言(或完全忘記這個想法)?

+1

您可能想在單獨的主題中提出Python問題。但是R也有一整套解析網站數據的工具。如果您打算將R用於後續分析,則最好將R用於數據生成,因爲您可以使用正確的格式更容易地使用R.儘管如此,Python是一種有趣的語言。 – 2010-11-19 15:02:54

回答

0

我很少再使用stata了,但是在某個時候在bash中找到了一個快速解決方案。這個腳本,在我的.vimrc中用幾行執行,效果很好。您可能需要根據您的系統調整延遲時間。

#!/bin/bash 

# needs wmctrl, xte and xsel 
# to get them run 
# sudo apt-get install wmctrl xautomation xsel 
# in debian/ubuntu linux 

#copy to clipboard "do filename" 
echo 'do ' '"'$1'"' | xsel -b 

# get current window id 
winid = $(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}') 

# check for stata window, if found activate else execute 
# use correct version here 
if [ "$(pgrep stata)" ] 
then 
    wmctrl -a 'Stata/MP 11.2' 
else 
    xstata & 
    sleep .1 
fi 

# delay depends on window manager etc 
# .1 ok with xmonad in 10.04 
sleep .1 

# switch to command line, ctrl-4 in stata 10, ctrl-1 in 11/12 
# and select existing text via ctrl-a 
xte 'keydown Control_L' 'key 1' 'key A' 'usleep 100' \ 
    'key V' 'keyup Control_L' 
sleep .1 
xte 'key Return' 
sleep .3 


# go back to editor window 
wmctrl -i -a $winid 

調整並將其放入您的vimrc中。

"" STATA DO-FILE SCRIPTS 
fun! RunIt() 
    w 
    "adjust this path to the bash script 
    !sh "/home/username/.rundo.sh" "%:p" 
endfun 
au FileType stata noremap <F8> :<C-U>call RunIt()<CR><CR> 
fun! RunDoLines() 
    let selectedLines = getbufline('%', line("'<"), line("'>")) 
if col("'>") < strlen(getline(line("'>"))) 
    let selectedLines[-1] = strpart(selectedLines[-1], 0, col("'>")) 
    endif 
if col("'<") != 1 
    let selectedLines[0] = strpart(selectedLines[0], col("'<")-1) 
    endif 
let temp = tempname() . ".do" 
    call writefile(selectedLines, temp) 
      "adjust this path to the bash script 
      exec "!sh /home/username/.rundo.sh " . temp 
    "au VimLeave * exe "!del -y" temp 
    au VimLeave * silent exe '!del /Q "'.$TEMP.'\*.tmp.do"' 
endfun 
au FileType stata noremap <F9> :<C-U>call RunDoLines()<CR><CR> 
1

如果你要切換到Linux(好),你應該 1.呼叫stata並將你的許可證切換到一個Linux許可證(他們將免費),然後從本地安裝它,然後啓動stata vim只需要一個bash腳本(我不是vim的專家) 2.或者,你可以購買stata 11,它擁有所有支持平臺的一個許可證 3.你可以用wine安裝stata(在Linux上玩更容易對我來說),但我不能讓葡萄酒給予超過一半ram的stata,所以我安裝了本機stata10我使用gedit for python,stata,R,latex等等,它處理任何事情,並且gtksourceview非常容易添加語法高亮和樣式等等..

Python是偉大的數據分析,請參閱

http://www.scipy.org/ http://openopt.org/Welcome http://www.macworld.com/appguide/app.html?id=63924&expand=false

刮網站,那裏有

http://scrapy.org/ 的http:// wwwsearch。 sourceforge.net/mechanize/

以及用於解析數據的文本通常我們有, http://pyparsing.wikispaces.com/

我有一大堆的文件,你可能會發現有用的(對於STATA和其他人,(一些書PDF拷貝一些的copyleft)gtksoureview語言定義),但我不知道如何安裝這個網站上的文件

+1

我有stata 11,它在linux中工作正常。即時通訊只是尋找修復從VIM執行stata。 – ilprincipe 2010-11-21 13:53:59

+0

我希望有人知道你的問題的答案,對不起,我不使用vim。我可以問一下你對vim的吸引力嗎? – 2010-11-22 14:25:51

+0

最初它或多或少是一種隨機選擇,但我很快就喜歡它,並且從未想過改用其他東西。模式編輯比emacs使用的修飾鍵更方便,我不需要一個完整的ide。我要去看看bash。 – ilprincipe 2010-11-23 15:35:51

1

[編輯]:哎呀,我注意到你的主要問題涉及到AutoIt腳本,對不起,我不知道如何將它翻譯成Linux。我不認爲有任何簡單的方法。你可能想看看xautomation或其他類似的工具,但這是一個完全不同的球類遊戲。

在你.vimrc取代:

!start "C:\Programme\Stata10\integvim\rundo3\rundo.exe" "%:p" 

有了,做在Linux同樣的事情的命令。 應該類似於此:

!/usr/share/... "%:p" 

因爲我沒有一個關於Stata的頭緒,你必須找出從別人的確切命令。

此外,我不確定"%:p":請在vim幫助中查看並比較Windows和Linux之間的差異。

1

在linux中,你可以使用一個名爲slime.vim的vim插件,它允許你在不離開vim的情況下將代碼從vim傳遞到正在運行的R進程。我在Mac,php,python和ocaml上使用它,它工作的很好。唯一需要注意的是,它使用屏幕來實現它的魔力本身並不是一件壞事。

如果我正確理解你的問題,我相信這會對你有極大的幫助。祝你好運,希望能幫助你。