Простая вещь, но есть ли способ поделиться объектами из одного чанка (здесь: data-chunk
) с кодом сервера (здесь: server-chunk
)? Я получаю сообщение об ошибке, что server-chunk
не может получить доступ faithful2
:
Ошибка: объект «faithful2» не найден
---
title: "Untitled"
format: html
server: shiny
---
```{r data-chunk}
faithful2 <- faithful
```
```{r}
sliderInput("bins", "Number of bins:",
min = 1, max = 50, value = 30)
plotOutput("distPlot")
```
```{r server-chunk}
#| context: server
output$distPlot <- renderPlot({
x <- faithful2[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white',
xlab = 'Waiting time to next eruption (in mins)',
main = 'Histogram of waiting times')
})
```
Ответ оказывается с использованием опции context
чанка. Вы можете прочитать все об этом здесь (перевод фрагментов rmarkdown в фрагменты кварто): https://rmarkdown.rstudio.com/authoring_shiny_prerendered.HTML
Для моего примера это будет выглядеть примерно так:
---
title: "Untitled"
format: html
server: shiny
---
```{r data-chunk}
#| context: setup
faithful2 <- faithful
```
```{r}
#| context: render
sliderInput("bins", "Number of bins:",
min = 1, max = 50, value = 30)
plotOutput("distPlot")
```
```{r server-chunk}
#| context: server
output$distPlot <- renderPlot({
x <- faithful2[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white',
xlab = 'Waiting time to next eruption (in mins)',
main = 'Histogram of waiting times')
})
```