作者:Yihui Xie
译者:郑宝童
日期:2021.05.28
2.11 Web pages and Shiny apps
Similar to HTML widgets, arbitrary web pages can be embedded in the book. You can use the function
knitr::include_url()
to include a web page through its URL. When the output format is HTML, aniframe
is used;8 in other cases, knitr tries to take a screenshot of the web page (or use the custom screenshot you provided). All chunk options are the same as those for HTML widgets. One option that may require your special attention is thedelay
option: HTML widgets are rendered locally, so usually they are fast to load for PhantomJS to take screenshots, but an arbitrary URL may take longer to load, so you may want to use a largerdelay
value, e.g., use the chunk optionscreenshot.opts = list(delay = 5)
.
类似于HTML小部件,任意的网页都可以嵌入到书中。你可以使用knitr::include_url()
函数来通过URL去包含一个网页。当输出格式是HTML时,iframe
被使用;8 在其他情况下,knitr试图获取网页的截图(或使用您提供的自定义截图)。所有块选项都与HTML小部件相同。需要特别注意的一个选项是delay
选项:HTML小部件是本地呈现的,所以通常它们加载速度很快,以便PhantomJS进行截图,但是存在一些URL可能需要较长的时间来加载,所以你可能想要使用一个更大的delay
值,例如,使用块选项截图screenshot.opts = list(delay = 5)
.
A related function is
knitr::include_app()
, which is very similar toinclude_url()
, and it was designed for embedding Shiny apps via their URLs in the output. Its only difference withinclude_url()
is that it automatically adds a query parameter?showcase=0
to the URL, if no other query parameters are present in the URL, to disable the Shiny showcase mode, which is unlikely to be useful for screenshots or iframes. If you do want the showcase mode, useinclude_url()
instead ofinclude_app()
. Below is a Shiny app example (Figure 2.6):
一个相关的函数是knitr::include_app()
,它与include_url()
非常相似,它被设计用来通过输出中的url嵌入Shiny应用。它与include_url()
的唯一区别是,如果URL中没有其他查询参数,它会自动向URL添加一个查询参数?showcase=0
,以禁用Shiny showcase模式(Shiny showcase模式对屏幕截图或iframe不太可能有用)。如果您想要使用showcase模式,请使用include_url()
而不是include_app()
。下图是一个Shiny应用示例(图2.6):
knitr::include_app("https://yihui.shinyapps.io/miniUI/",
height = "600px")
FIGURE 2.6: A Shiny app created via the miniUI package; you can see a live version at https://yihui.shinyapps.io/miniUI/.
图2.6:通过miniUI包创建的Shiny应用;你可以在https://yihui.shinyapps.io/miniUI/上看到实时版本。
Again, you will see a live app if you are reading an HTML version of this book, and a static screenshot if you are reading other types of formats. The above Shiny app was created using the miniUI package (Cheng 2018), which provides layout functions that are particularly nice for Shiny apps on small screens. If you use normal Shiny layout functions, you are likely to see vertical and/or horizontal scrollbars in the iframes because the page size is too big to fit in an iframe. When the default width of the iframe is too small, you may use the chunk option
out.width
to change it. For the height of the iframe, use theheight
argument ofinclude_url()
/include_app()
.
同样,如果你正在阅读这本书的HTML版本,你会看到一个实时应用程序;如果你正在阅读其他类型的格式,你会看到一个静态截图。上面的Shiny应用程序是使用miniUI包(Cheng 2018)创建的,它提供了布局功能,特别适合在小屏幕上展示Shiny应用程序。如果你使用普通的Shiny布局功能,因为页面大小太大,无法容纳在iframe中,你可能会在iframe中看到垂直/水平滚动条。当iframe的默认宽度太小时,您可以使用chunk选项out.width
来改变它。对于iframe的高度,使用include_url()
/include_app()
的高度参数。
Shiny apps may take even longer to load than usual URLs. You may want to use a conservative value for the
delay
option, e.g., 10. Needless to say,include_url()
andinclude_app()
require a working Internet connection, unless you have previously cached the chunk (but web pages inside iframes still will not work without an Internet connection).
Shiny应用程序可能比普通的url加载时间更长。您可能希望对delay
选项使用保守值,例如10。不用说,include_url()
和include_app()
需要一个可用的的互联网连接,除非你已经缓存了块(但是iframes内的网页如果没有互联网连接仍然不能工作)。
References
Cheng, Joe. 2018. MiniUI: Shiny Ui Widgets for Small Screens. https://CRAN.R-project.org/package=miniUI.
- An
iframe
is basically a box on one web page to embed another web page.↩︎