R 在代码块中使用标记来定义大型函数

R 在代码块中使用标记来定义大型函数,r,shiny,r-markdown,R,Shiny,R Markdown,我想在一个大项目上使用R降价。该项目使用许多非常大的自定义函数。因此,我想使用降价来评论函数的某些部分 ```{r} my_function <- function(x,y){ test <- x + seq(1,10) ``` 当然,我意识到我可以简单地使用#符号添加评论,但这并不像降价那么好。这与大型闪亮服务器功能或类似功能相关。 你知道我该怎么做吗 在R-markdown中,不能跨块(afaict)定义对象(例如函数)。另一种方法是不可见地定义函数,然后使用非执行代码块来

我想在一个大项目上使用R降价。该项目使用许多非常大的自定义函数。因此,我想使用降价来评论函数的某些部分

```{r}
my_function <- function(x,y){
  test <- x + seq(1,10)
```
当然,我意识到我可以简单地使用#符号添加评论,但这并不像降价那么好。这与大型闪亮服务器功能或类似功能相关。
你知道我该怎么做吗

在R-markdown中,不能跨块(afaict)定义对象(例如函数)。另一种方法是不可见地定义函数,然后使用非执行代码块来讨论部分。但我不喜欢这种选择,因为它会加倍努力,并增加具有不同功能的可能性

另一种方法是在函数中定义清晰的标记,抓住函数体,然后由这些标记分割。试试这个:

---
title: test markdown
---

```{r echo = FALSE, include = FALSE}
my_function <- function(x,y){
  test <- x + seq(1,10) ###BREAK###
  # normal comment
  output <- test + y    ###BREAK###
  return(output)
}
my_function_body <- strsplit(
  paste(head(capture.output(print.function(my_function)), n = -1), collapse = "\n"),
  "###BREAK###[\n\r]*")[[1]]
```

```{r echo = FALSE, include = TRUE, comment = ''}
cat(my_function_body[[1]])
```

Then I would like to use Markdown to describe the second part of the function


```{r echo = FALSE, include = TRUE, comment = ''}
cat(my_function_body[[2]])
```

Now the third/last part of the function


```{r echo = FALSE, include = TRUE, comment = ''}
cat(my_function_body[[3]])
```

And then I would like to apply the function

```{r}
my_function(1,2)
```

And the whole of the function (excluding the markers):

```{r echo = FALSE, include = TRUE, comment = ''}
cat(
  gsub("###BREAK###", "",
       paste(head(capture.output(print.function(my_function)), n = -1), collapse = "\n"))
)
```
---
标题:测试降价
---
```{r echo=FALSE,include=FALSE}
my_函数
```{r}
my_function(1,2)
```
---
title: test markdown
---

```{r echo = FALSE, include = FALSE}
my_function <- function(x,y){
  test <- x + seq(1,10) ###BREAK###
  # normal comment
  output <- test + y    ###BREAK###
  return(output)
}
my_function_body <- strsplit(
  paste(head(capture.output(print.function(my_function)), n = -1), collapse = "\n"),
  "###BREAK###[\n\r]*")[[1]]
```

```{r echo = FALSE, include = TRUE, comment = ''}
cat(my_function_body[[1]])
```

Then I would like to use Markdown to describe the second part of the function


```{r echo = FALSE, include = TRUE, comment = ''}
cat(my_function_body[[2]])
```

Now the third/last part of the function


```{r echo = FALSE, include = TRUE, comment = ''}
cat(my_function_body[[3]])
```

And then I would like to apply the function

```{r}
my_function(1,2)
```

And the whole of the function (excluding the markers):

```{r echo = FALSE, include = TRUE, comment = ''}
cat(
  gsub("###BREAK###", "",
       paste(head(capture.output(print.function(my_function)), n = -1), collapse = "\n"))
)
```