2017-06-09 30 views
1

我用的是knitr::opts_chunk$set(fig.align = "center")在rmarkdown文檔的開頭設定的數字排列。當我輸出HTML文件時,靜態數字與中心對齊,但HTML小部件(如leaflet()ggplotly()的輸出)具有默認對齊方式(在左側)。有沒有一個選項可以強制HTML小部件到中心?HTML控件排列在rmarkdwon

編輯:下面給出的例子。

--- 
title: "test" 
output: html_document 
--- 

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = TRUE, fig.align = "center") 
library(ggplot2) 
library(plotly) 
``` 

```{r static plot} 
# This is aligned to center 

g <- ggplot(mtcars, aes(mpg, cyl)) + 
geom_point() 
g 
``` 

```{r html widget} 
# html output isn't aligned 
p <- ggplotly(g) 
p 
``` 
+0

這可能幫助:https://stackoverflow.com/questions/39562947/how-to -arrange-html-widgets-inside-of-a-rmarkdown-document-pdf-html –

+0

儘管可能有更好的方法,我通常使用CSS來定位HTML小部件。如果你發佈一個例子,我會很樂意演示。 –

+0

以示例更新。 –

回答

0

你可以調整劇情文檔的默認寬度,然後使用一些CSS:

--- 
title: "Untitled" 
output: html_document 
--- 

<style> 
/* resize the widget container */ 
.plotly { 
    width: 100% !important; 
} 

/* center the widget */ 
div.svg-container { 
    margin: auto !important; 
} 
</style> 

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = TRUE, fig.align = "center") 
library(ggplot2) 
library(plotly) 
``` 


```{r static plot} 
# This is aligned to center 
g <- ggplot(mtcars, aes(mpg, cyl)) + 
geom_point() 
g 
``` 

```{r html widget} 
p <- ggplotly(g, browser.fill = F) %>% 
    layout(autosize = F, width = '100%', height = '100%') 
p 
``` 
+0

感謝您的回答。雖然這解決了我的問題,但我不喜歡這樣一個事實,即我必須在腳本的開頭添加一些CSS代碼,如果我還想要調整傳單映射,這不是很通用。你瞭解'styleWidget'函數[這裏](https://stackoverflow.com/a/39607828/4227151)。我希望我可以直接向作者提問,但我沒有足夠的評價。 –