将fig.width传递到标签列表 [英] Passing fig.width into a taglist

查看:89
本文介绍了将fig.width传递到标签列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rgl小部件会响应编织代码块选项中指定的图形宽度和高度,例如

The rgl widget responds to the figure width and height specified in knitr code chunk options, e.g.

```{r fig.width=2, fig.height=2}
library(rgl); example(plot3d);
rglwidget()
```

给出一个小图(在我的屏幕上为192 x 192).但是,如果将rglwidget()放入可浏览的tagList中,则不会:

gives a small plot (192 x 192 on my screen). However, if I put rglwidget() in a browsable tagList, it doesn't:

```{r fig.width=2, fig.height=2}
library(rgl); example(plot3d)
library(htmltools)
browsable(tagList(rglwidget(), rglwidget()))
```

这提供了两个完整尺寸的小部件.调试Java脚本显示,每个脚本都被初始化为960 x 500,而不是第一个示例中的192 x 192.

This gives two full size widgets. Debugging the Javascript shows that each is being initialized as 960 by 500, not 192 by 192 as in the first example.

有没有办法说我希望将width和height值传递到tagList的小部件中?

Is there a way to say that I want the width and height values to be passed into the widgets in the tagList?

P.S.这与rgl无关; leaflet在存在相同的问题

P.S. This has nothing to do with rgl; leaflet has the same problem in

```{r fig.width=2, fig.height=2}
library(leaflet); library(htmltools)
browsable(tagList(leaflet() %>% addTiles())
```

推荐答案

由于块中的代码可以读取该块的选项,因此最好的解决方案是编写一些小的函数,以像素为单位获取图形的大小,并在小部件的显式设置中使用它们.

Since code in a chunk can read the options for the chunk, probably the best solution here is to write small functions that obtain the size of the figure in pixels, and use those in explicit settings in the widget.

例如,

```{r fig.width=2, fig.height=2}
library(leaflet); library(htmltools)

# This one is too big:
browsable(tagList(leaflet() %>% addTiles()))

# Get the current figure size in pixels:
w <- function() 
  with(knitr::opts_current$get(c("fig.width", "dpi", "fig.retina")),
       fig.width*dpi/fig.retina)
h <- function() 
  with(knitr::opts_current$get(c("fig.height", "dpi", "fig.retina")),
       fig.height*dpi/fig.retina)

# This is what I wanted:
browsable(tagList(leaflet(width = w(), height = h()) %>% addTiles()))
```

这篇关于将fig.width传递到标签列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆