我的dygraph在flexdashboard中没有响应:输出不会调整自身大小以适应边界

时间:2017-10-15 15:23:37

标签: r dygraphs flexdashboard

在下面的示例中,dygraph的底部部分在RStudio html Viewer中既不可见,也不在Web浏览器中。

---
title: "Flexdashboard with params"
params:
  names: "Diane"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE)
```

First tab
===============================

### Load packages 

```{r}
# devtools::install_github("thinkr-open/prenoms")
library(prenoms)
library(dplyr)
library(tidyr)
library(dygraphs)
library(stringr)
```


### Define a function 

```{r}
draw_names_dygraph <- function(names){

prenoms %>% 
  group_by(year,name) %>% 
  summarise(total = sum(n))    %>% 
  filter(name %in% names) %>% 
  spread(key = name,value =total) %>% 
  dygraph()

}
```

Second tab
===========================================

### Draw a dynamic graph...

... with parameters

```{r}
draw_names_dygraph(names = str_split(params$name, "," ) [[1]])
```

在第二个标签中添加行无法解决问题。我究竟做错了什么 ?是不是flexdashboard应该响应?

关于我的会话信息:

> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.3 LTS

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
 [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8    
 [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
 [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] shiny_1.0.5      bindrcpp_0.2     stringr_1.2.0   
[4] tidyr_0.7.1      dplyr_0.7.4      prenoms_0.1.0   
[7] dygraphs_1.1.1.4

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.13      compiler_3.4.1    bindr_0.1        
 [4] shinyjs_0.9.1     tools_3.4.1       xts_0.10-0       
 [7] digest_0.6.12     memoise_1.1.0     jsonlite_1.5     
[10] evaluate_0.10.1   tibble_1.3.4      lattice_0.20-35  
[13] pkgconfig_2.0.1   rlang_0.1.2       reprex_0.1.1     
[16] rstudioapi_0.7    yaml_2.1.14       withr_2.0.0      
[19] knitr_1.17        devtools_1.13.3   htmlwidgets_0.9  
[22] rprojroot_1.2     grid_3.4.1        flexdashboard_0.5
[25] tidyselect_0.2.0  glue_1.1.1        R6_2.2.2         
[28] rmarkdown_1.6     callr_1.0.0       whisker_0.3-2    
[31] clipr_0.3.3       purrr_0.2.3       magrittr_1.5     
[34] backports_1.1.1   htmltools_0.3.6   assertthat_0.2.0 
[37] xtable_1.8-2      mime_0.5          httpuv_1.3.5     
[40] miniUI_0.1.1      stringi_1.1.5     zoo_1.8-0

1 个答案:

答案 0 :(得分:1)

我发现了这个问题:唯一要做的就是添加fig.height(和fig.width,但仅限于输出的优点)。可以处理这些图形参数以获得良好的输出。

然后,在## ...输出后添加dygraph之类的内容,将其大小限制为当前窗口。

请注意,我已在draw_names_dygraph()中添加了高度参数。

### Define a function 

```{r}
draw_names_dygraph <- function(names, height_dy = 3, height_RangeSelector = 20){

prenoms %>% 
  group_by(year,name) %>% 
  summarise(total = sum(n))    %>% 
  filter(name %in% names) %>% 
  spread(key = name,value =total) %>% 
  dygraph(height = height_dy) %>% 
    dyRangeSelector(height = height_RangeSelector)
}
```

Second tab
===========================================

### Draw a dynamic graph... 

... with parameters

```{r, fig.height=6, fig.width=7}
draw_names_dygraph(names = str_split(params$name, "," ) [[1]], height_dy = NULL)
```

## ... 

这是我从上面修改后的代码中获得的Flexdashboard,发布于 Rpubs