首先,不确定这是否至关重要,但是,following the documentation,我们需要先添加这个块:
```{r setup, include=FALSE}
library(knitr)
library(reticulate)
knitr::knit_engines$set(python = reticulate::eng_python)
```
您想要获得的是特定于 Jupyter 的功能。我只能通过_repr_svg_() 函数将点转换为纯 SVG 来重现它:
```{python}
from shapely.geometry import Point, LineString, Polygon, MultiPoint
point1 = Point(2.2, 4.2)
point2 = Point(7.2, -25.1)
point3 = Point(9.26, -2.456)
point3D = Point(9.26, -2.456, 0.57)
multipoints = MultiPoint([point1, point2, point3, point3D])
svg = multipoints._repr_svg_()
# or, in your case
svg = point1._repr_svg_()
```
然后用 R 块显示它:
```{r}
htmltools::HTML(py$svg)
```
我尝试仅在 Python 端执行此操作(调用 r.HTML()),这只会导致文本输出。
请注意,这将导致以下警告:
sys:1: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the `geoms` property instead to get the number of parts of a multi-part geometry.
但你可以忽略它,它仍然会画点:
完整代码:
---
title: "Reprex"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
library(reticulate)
knitr::knit_engines$set(python = reticulate::eng_python)
```
```{python}
from shapely.geometry import Point, LineString, Polygon, MultiPoint
point1 = Point(2.2, 4.2)
point2 = Point(7.2, -25.1)
point3 = Point(9.26, -2.456)
point3D = Point(9.26, -2.456, 0.57)
multipoints = MultiPoint([point1, point2, point3, point3D])
svg = multipoints._repr_svg_()
# or, in your case
svg = point1._repr_svg_()
```
```{r}
htmltools::HTML(py$svg)
```