如何从Rmarkdown部分重构闪亮代码的服务器部分

如何从Rmarkdown部分重构闪亮代码的服务器部分,r,shiny,refactoring,r-markdown,flexdashboard,R,Shiny,Refactoring,R Markdown,Flexdashboard,我有以下完全运行的闪亮仪表板应用程序: --- title: "Test" runtime: shiny output: flexdashboard::flex_dashboard: orientation: rows theme: bootstrap vertical_layout: scroll --- ```{r setup, include=FALSE} library(flexdashboard) library(tidyverse) ``` Basic

我有以下完全运行的闪亮仪表板应用程序:

---
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
  })

)
```
---
标题:“测试”
运行时间:闪亮
输出:
flexdashboard::flex_仪表板:
方向:行
主题:引导
垂直布局:滚动
---
```{r设置,include=FALSE}
库(flexdashboard)
图书馆(tidyverse)
```
基本的
===================================== 
基本输入{.sidebar}
-------------------------------------
```{r io_进程}
选择输入(“mpg\u thres”,label=“mpg threshold”,
选项=c(10,20,30,40),选择=10)
选择输入(“循环”,label=“循环阈值”,
选项=c(4,5,6,7,8),选择=4)
```
行{数据高度=500}
-------------------------------------
###散点图
```{r show_scattr}
主面板(
渲染图({
dat%
选择(mpg,气缸)%>%
过滤器(mpg>输入$mpg\u thres和cyl>输入$cyl\u thres)
ggplot(dat、aes(mpg、cyl))+
几何点()
})
)
```
行{数据高度=500}
-------------------------------------
###一字不差
```{r逐字显示}
主面板(
renderPrint({
dat%
选择(mpg,气缸)%>%
过滤器(mpg>输入$mpg\u thres和cyl>输入$cyl\u thres)
dat
})
)
```
请注意,代码的以下部分是多余的 在两个不同的Rmarkdown部分中,散点图和逐字显示

dat%
选择(mpg,气缸)%>%
过滤器(mpg>输入$mpg\u thres和cyl>输入$cyl\u thres)
我如何将其分解


为完整起见,应用程序的屏幕截图如下:


使用反应式数据表达式,将输出块更改为:

### 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()
  })
)
```
散点图 ```{r show_scattr} dat% 选择(mpg,气缸)%>% 过滤器(mpg>输入$mpg\u thres和cyl>输入$cyl\u thres) } ) 主面板( 渲染图({ ggplot(dat(),aes(mpg,cyl))+ 几何点() }) ) ``` ###一字不差 ```{r逐字显示} 主面板( renderPrint({ dat() }) ) ``` 注意
reactive
的使用以及调用
dat
作为函数(
dat()

reactive
确保每次更改输入时都重新计算
dat

### 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()
  })
)
```