在R Jupyter笔记本中显示来自文件的图像

12
我在磁盘上有一张图片,并希望在R Jupyter笔记本的markdown单元格中显示它。我该怎么做?
我知道用Python很简单,只需要从display导入Image类即可。
3个回答

11

IRdisplay具有用于丰富显示“内容”的功能,其中包括图像:

library("IRdisplay")
display_png(file="plot.png)  

谢谢。它起作用了。但是,我无法弄清楚如何将图像居中。 - Hamideh

3
在 Jupyter Python 笔记本的 markdown 单元格中,按照平常的方式进行操作:
<img src="../relative/path/to/img.png">

或者
![image](../relative/path/to/img.png)

1
这似乎只适用于来自网络的图像,而不适用于本地托管的图像。 - user3617525
2
@user3617525,如果您提供一个有效的相对路径到图像,这应该可以工作。我更喜欢语法![image](../relative/path/to/img.png),但这只是个人口味问题。 - cel

0
以下是Jupyter R Kernel Notebook用户选择文件系统中的图像文件(.PNG),剥离完整路径名,然后将图像插入到此代码下面的单元格中的代码。
image_chosen = choose.files(
               default = "", 
               caption = "Select The Image File (in PNG format) to Insert",
               multi = TRUE, filters = Filters,
               index = nrow(Filters)
               )

chosen_image_name = basename(image_chosen)
#chosen_image_name   # uncomment this line to show the location of the image.

IRdisplay::display_png(file = chosen_image_name)    # This line inserts the image.

然后使用标准的Jupyter Notebook扩展程序“Hide Input”来隐藏此代码单元格,使用“Freeze”NBextension来冻结代码单元格,以便图像保持静止,笔记本不会尝试在每次笔记本代码运行时选择和插入新图像。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接