I am running a few linear model fits in python (using R as a backend via RPy) and I would like to export some LaTeX tables with my R "summary" data.
我在python中运行了一些线性模型拟合(通过RPy使用R作为后端),我想用R“摘要”数据导出一些LaTeX表。
This thread explains quite well how to do it in R (with the xtable
function), but I cannot figure out how to implement this in RPy.
这个帖子很好地解释了如何在R中使用xtable函数(但是我没有弄清楚如何在RPy中实现它)。
The only relevant thing searches such as "Chunk RPy" or "xtable RPy" returned was this, which seems to load the package in python but not to use it :-/
返回的唯一相关的事情,如“Chunk RPy”或“xtable RPy”,这似乎是在python中加载包但不使用它: - /
Here's an example of how I use RPy and what happens.
这是我如何使用RPy以及会发生什么的一个例子。
And this would be the error without bothering to load any data:
这将是错误而无需加载任何数据:
from rpy2.robjects.packages import importr
xtable = importr('xtable')
latex = xtable('')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-131-8b38f31b5bb9> in <module>()
----> 1 latex = xtable(res_sum)
2 print latex
TypeError: 'SignatureTranslatedPackage' object is not callable
I have tried using the stargazer
package instead of xtable
and I get the same error.
我尝试过使用stargazer包而不是xtable,我得到了同样的错误。
3 个解决方案
#1
1
Ok, I solved it, and I'm a bit ashamed to say that it was a total no-brainer.
好吧,我解决了它,我有点惭愧地说这完全没问题。
You just have to call the functions as xtable.xtable()
or stargazer.stargazer()
.
您只需将函数称为xtable.xtable()或stargazer.stargazer()。
#2
0
To easily generate TeX data from Python, I wrote the following function;
为了从Python轻松生成TeX数据,我编写了以下函数;
import re
def tformat(txt, v):
"""Replace the variables between [] in raw text with the contents
of the named variables. Between the [] there should be a variable name,
a colon and a formatting specification. E.g. [smin:.2f] would give the
value of the smin variable printed as a float with two decimal digits.
:txt: The text to search for replacements
:v: Dictionary to use for variables.
:returns: The txt string with variables substituted by their formatted
values.
"""
rx = re.compile(r'\[(\w+)(\[\d+\])?:([^\]]+)\]')
matches = rx.finditer(txt)
for m in matches:
nm, idx, fmt = m.groups()
try:
if idx:
idx = int(idx[1:-1])
r = format(v[nm][idx], fmt)
else:
r = format(v[nm], fmt)
txt = txt.replace(m.group(0), r)
except KeyError:
raise ValueError('Variable "{}" not found'.format(nm))
return txt
You can use any variable name from the dictionary in the text that you pass to this function and have it replaced by the formatted value of that variable.
您可以在传递给此函数的文本中使用字典中的任何变量名称,并将其替换为该变量的格式化值。
What I tend to do is to do my calculations in Python, and then pass the output of the globals()
function as the second parameter of tformat
:
我倾向于做的是在Python中进行计算,然后将globals()函数的输出作为tformat的第二个参数传递:
smin = 235.0
smax = 580.0
lst = [0, 1, 2, 3, 4]
t = r'''The strength of the steel lies between SI{[smin:.0f]}{MPa} and \SI{[smax:.0f]}{MPa}. lst[2] = [lst[2]:d].'''
print tformat(t, globals())
Feel free to use this. I put it in the public domain.
随意使用它。我把它放在公共领域。
Edit: I'm not sure what you mean by "linear model fits", but might numpy.polyfit
do what you want in Python?
编辑:我不确定你的意思是“线性模型拟合”,但是可能numpy.polyfit在Python中做你想要的吗?
#3
0
To resolve your problem, please update stargazer
to version 4.5.3, now available on CRAN. Your example should then work perfectly.
要解决您的问题,请将stargazer更新为4.5.3版,现在可在CRAN上使用。那么你的榜样应该完美无缺。
#1
1
Ok, I solved it, and I'm a bit ashamed to say that it was a total no-brainer.
好吧,我解决了它,我有点惭愧地说这完全没问题。
You just have to call the functions as xtable.xtable()
or stargazer.stargazer()
.
您只需将函数称为xtable.xtable()或stargazer.stargazer()。
#2
0
To easily generate TeX data from Python, I wrote the following function;
为了从Python轻松生成TeX数据,我编写了以下函数;
import re
def tformat(txt, v):
"""Replace the variables between [] in raw text with the contents
of the named variables. Between the [] there should be a variable name,
a colon and a formatting specification. E.g. [smin:.2f] would give the
value of the smin variable printed as a float with two decimal digits.
:txt: The text to search for replacements
:v: Dictionary to use for variables.
:returns: The txt string with variables substituted by their formatted
values.
"""
rx = re.compile(r'\[(\w+)(\[\d+\])?:([^\]]+)\]')
matches = rx.finditer(txt)
for m in matches:
nm, idx, fmt = m.groups()
try:
if idx:
idx = int(idx[1:-1])
r = format(v[nm][idx], fmt)
else:
r = format(v[nm], fmt)
txt = txt.replace(m.group(0), r)
except KeyError:
raise ValueError('Variable "{}" not found'.format(nm))
return txt
You can use any variable name from the dictionary in the text that you pass to this function and have it replaced by the formatted value of that variable.
您可以在传递给此函数的文本中使用字典中的任何变量名称,并将其替换为该变量的格式化值。
What I tend to do is to do my calculations in Python, and then pass the output of the globals()
function as the second parameter of tformat
:
我倾向于做的是在Python中进行计算,然后将globals()函数的输出作为tformat的第二个参数传递:
smin = 235.0
smax = 580.0
lst = [0, 1, 2, 3, 4]
t = r'''The strength of the steel lies between SI{[smin:.0f]}{MPa} and \SI{[smax:.0f]}{MPa}. lst[2] = [lst[2]:d].'''
print tformat(t, globals())
Feel free to use this. I put it in the public domain.
随意使用它。我把它放在公共领域。
Edit: I'm not sure what you mean by "linear model fits", but might numpy.polyfit
do what you want in Python?
编辑:我不确定你的意思是“线性模型拟合”,但是可能numpy.polyfit在Python中做你想要的吗?
#3
0
To resolve your problem, please update stargazer
to version 4.5.3, now available on CRAN. Your example should then work perfectly.
要解决您的问题,请将stargazer更新为4.5.3版,现在可在CRAN上使用。那么你的榜样应该完美无缺。