2017-05-29 5 views
3

I持って、次の完全ランニングシャイニー-ダッシュボードアプリからシャイニーコードのサーバーの一部をリファクタリングする方法:Rmarkdownセクション

--- 
title: "Test" 
runtime: shiny 
output: 
    flexdashboard::flex_dashboard: 
    orientation: rows 
    theme: bootstrap 
    vertical_layout: scroll 
--- 
```{r setup, include=FALSE} 
library(flexdashboard) 
library(tidyverse) 
``` 

Basic 
===================================== 

Inputs_basic {.sidebar} 
------------------------------------- 

```{r io_processes} 
selectInput("mpg_thres", label = "MPG threshold", 
       choices = c(10,20,30,40), selected = 10) 
selectInput("cyl_thres", label = "CYL threshold", 
       choices = c(4,5,6,7,8), selected = 4) 
``` 

Rows {data-height=500} 
------------------------------------- 


### Scatter Plot 

```{r show_scattr} 
mainPanel(

    renderPlot({ 
    dat <- as.tibble(mtcars) %>% 
      select(mpg, cyl) %>% 
      filter(mpg > input$mpg_thres & cyl > input$cyl_thres) 
    ggplot(dat, aes(mpg, cyl)) + 
     geom_point() 

    }) 

) 
``` 


Rows {data-height=500} 
------------------------------------- 

### Show verbatim 
```{r show_verbatim} 
mainPanel(

    renderPrint({ 
    dat <- as.tibble(mtcars) %>% 
      select(mpg, cyl) %>% 
      filter(mpg > input$mpg_thres & cyl > input$cyl_thres) 
    dat 
    }) 

) 
``` 

次のコードの一部は、2つの異なるRmarkdownセクション散布の中 冗長であることに注意してくださいプロットおよび凡例を表示

dat <- as.tibble(mtcars) %>% 
     select(mpg, cyl) %>% 
     filter(mpg > input$mpg_thres & cyl > input$cyl_thres) 

どうすれば分数化できますか?

enter image description here

答えて

5

は、反応性のデータ表現を使用するように、出力チャンクを変更します:

### Scatter Plot 

```{r show_scattr} 
dat <- reactive({ 
    as.tibble(mtcars) %>% 
    select(mpg, cyl) %>% 
    filter(mpg > input$mpg_thres & cyl > input$cyl_thres) 
}) 

mainPanel(
    renderPlot({ 
    ggplot(dat(), aes(mpg, cyl)) + 
     geom_point() 
    }) 
) 
``` 

### Show verbatim 
```{r show_verbatim} 
mainPanel(
    renderPrint({ 
    dat() 
    }) 
) 
``` 

注の使用


は完全を期すためにアプリのスクリーンショットは、これですreactiveを呼び出し、datを関数(dat())として呼び出します。

reactive入力が変更されるたびにdatが再計算されることを確認します。