8

我在大学里使用 Vim 在 Windows 中编写 Stata 脚本已有一段时间了。我现在正在学习 R,我想完全切换到 Linux 作为我的操作系统(我最近在笔记本电脑上切换到了 Ubuntu)。R 在 Windows 和 Linux 中都能很好地与 Vim 配合使用,但有时我仍然需要使用 Stata。在 Windows 中,我一直在使用 Stata 用户提供的简单 AutoIt 脚本将行/整个文件发送到 stata 进行评估。此脚本在 Linux 中不起作用。

这是脚本的样子

; AutoIt v3 script to run a Stata do-file from an external text editor
; Version 3.1, Friedrich Huebler, fhuebler@gmail.com, 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,因为我可能会在更长的时间内处理数据并进行经验分析,我认为它可能对某些任务有用,例如解决此类问题或解析来自网站的数据。这是推荐的,还是我应该看另一种语言(或完全忘记这个想法)?

4

4 回答 4

1

如果您要切换到 linux(永远),您应该 1. 调用 stata 并将您的许可证切换到 linux 许可证(他们将免费提供)并本地安装,然后从 vim 启动 stata 只需要一个 bash脚本(我不是 vim 专家) 2. 或者,你可以购买 stata 11,它对所有支持的平台都有一个许可证 3. 你可以用 wine 安装 stata(在 linux 上玩对我来说更容易)但我不能t get wine 给 stata 超过半个 gig 的 ram,所以我安装了本地 stata10

我将 gedit 用于 python、stata、R、latex 等,它可以处理任何事情,而 gtksourceview 很容易添加语法高亮和样式等。

Python 非常适合数据分析,请参阅

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

用于抓取网站

http://scrapy.org/ http://www.sourceforge.net/mechanize/

为了从文本中解析数据,我们通常有 http://pyparsing.wikispaces.com/

我有一堆你可能会觉得有用的文件(stata 和其他人的 gtksoureview 语言定义,一些书籍的 pdf 副本(一些 copyleft))但我不知道如何在这个网站上附加文件

于 2010-11-21T10:13:13.550 回答
1

我很少再使用 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> 
于 2012-09-22T13:47:42.883 回答
1

在 linux 中,你可以使用一个名为 slime.vim 的 vim 插件,它允许你在不离开 vim 的情况下将代码从 vim 传送到正在运行的 R 进程。我在 Mac 上将它用于 mysql、php、python 和 ocaml,效果很好。唯一需要注意的是,它使用屏幕来发挥它的魔力,这本身并不是一件坏事。

如果我正确理解您的问题,我相信这将对您有很大帮助。祝你好运,希望对你有所帮助。

于 2010-12-14T14:28:06.700 回答
1

[编辑]:糟糕,我注意到您的主要问题与 AutoIt 脚本有关,抱歉,我不知道如何将其翻译到 Linux。我不认为有任何简单的方法。您可能想研究 xautomation 或其他类似工具,但那是完全不同的球赛。

在你的.vimrc替换:

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

使用在 linux 中执行相同操作的命令。它应该类似于:

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

由于我对Stata一无所知,因此您必须从其他人那里找出确切的命令。

另外我不确定"%:p":请在 vim 帮助中查找并比较 Windows 和 Linux 之间的差异。

于 2010-12-09T19:41:45.650 回答