Python and Rpy2: Calling plot function with options that have “.” in them

StackOverflow https://stackoverflow.com/questions/2125218

  •  22-09-2019
  •  | 
  •  

문제

I'm just starting to learn how to use rpy2 with python. I'm able to make simple plots and such, but I've run into the problem that many options in R use ".". For example, here's an R call that works:

barplot(t, col=heat.colors(2), names.arg=c("pwn", "pwn2"))

where t is a matrix.

I want to use the same call in python, but it rejects the "." part of names.arg. My understanding was that in python you replace the "." with "_", so names_arg for example, but that is not working either. I know this is a basic problem so I hope someone has seen this and knows the fix. Thanks!

도움이 되었습니까?

해결책

콧수염 와 같은 microTemplating 라이브러리를 사용하여 들어오는 JSON 객체를 {{ key }} 템플릿 구문을 사용하여 HTML 스 니펫으로 구문 분석 할 수 있습니다.객체가 같은 것처럼 보이는 경우 :

var myObj = {
    name: 'Joe Smith',
    age: 25,
    features: {
        hairColor: 'red',
        eyeColor: 'blue'
    }
};
.

콧수염을 사용하면 {{#}} 및 {{/}}를 사용하여 쉽게 중첩 된 객체를 이송하여 쉽게 렌더링 할 수 있습니다.

Mustache.render('Hello, my name is {{name}} and I am {{age}} years old. {{#features}} I have {{hairColor}} hair and {{eyeColor}} eyes.{{/features}}', myObj);
.

출력 :

안녕하세요, 제 이름은 Joe Smith이고 나는 25 세입니다.나는 빨간 머리카락과 파란 눈을 가지고있다.

편집 : 더 많은 Germane 응용 프로그램 - 객체 목록이있는 템플릿을 사용하여 제어판을 동적으로 생성합니다.여기에 내 템플릿과 객체 (HTML가 목록에 고장 났고 명확성을 위해 가입) :

var panel = [
  '<div id="cpanel">',
    '{{#cpanel}}',
      '<div class="panel_section">',
        '<h1 class="cpanel">{{name}}</h1>',
        '<p>',
          '{{#content}}',
            '<button id="{{id}}">{{id}}</button>',
          '{{/content}}',
        '</p>',
      '</div>',
    '{{/cpanel}}',
  '</div>',
].join('\n');

var panelObj = {
  cpanel: [
  {
    name: 'playback',
    content: [
      {id: 'play'},
      {id: 'pause'},
      {id: 'stop'}
    ]
  }, {
    name: 'zoom',
    content: [
      {id: 'zoomIn'},
      {id: 'zoomOut'}
    ]
  }]
};
.

다음 콧수염으로 렌더링합니다.

Mustache.render(panel, panelObj);
.

출력 :

<div id="cpanel">
  <div class="panel_section">
    <h1 class="cpanel">playback</h1>
    <p>
      <button id="play">play</button>
      <button id="pause">pause</button>
      <button id="stop">stop</button>
    </p>
  </div>
  <div class="panel_section">
    <h1 class="cpanel">zoom</h1>
    <p>
      <button id="zoomIn">zoomIn</button>
      <button id="zoomOut">zoomOut</button>
    </p>
  </div>
</div>
.

다른 팁

I don't know whether Rpy will accept this, but you can have keyword parameters with periods in them. You have to pass them through a dictionary though. Like this:

>>> def f(**kwds): print kwds
... 
>>> f(a=5, b_c=6)
{'a': 5, 'b_c': 6}
>>> f(a=5, b.c=6)
Traceback (  File "<interactive input>", line 1
SyntaxError: keyword cant be an expression (<interactive input>, line 1)
>>> f(**{'a': 5, 'b.c': 6})
{'a': 5, 'b.c': 6}

With rpy2-2.1.0, one way to write it would be:

from rpy2.robjects.packages import importr
graphics = importr("graphics")
grdevices = importr("grDevices")

graphics.barplot_default(t, 
                         col = grdevices.heat_colors(2),
                         names_arg = StrVector(("pwn", "pwn2")))

Having to use barplot_default (rather that barplot) is due to the extensive use of the ellipsis '...' in R'sfunction signatures and to the fact that save parameter name translation would require analysis of the R code a function contains.

More, and an example of a function to perform systematic translation of '.' to '_' is at: http://rpy.sourceforge.net/rpy2/doc-2.1/html/robjects.html#functions

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top