我想创建一个 Shiny 文档,其中显示两个并排的 grob,并希望每个 grob 内的文本是滑块输入的选定值。
我能够在非反应性上下文中轻松地并排输出两个 grob(下面的第一个示例),但显然无法提取滑块值。但是,当我尝试在 renderPlot
响应式上下文中输出它们时,我能够提取滑块值,但只有第二个 grob 出现并且连接箭头不可见。
谁能提供一个解决方案,同时显示 grobs 和无功值?
---
runtime: shiny
output:
html_document
---
```{r global-options,include = FALSE}
## set global options
knitr::opts_chunk$set(echo = F,error = F,warning = F,message = F)
```
```{r echo = F,message = F}
## libraries
library(shiny)
library(Gmisc)
library(grid)
library(knitr)
## slider input
sliderInput("nSelect","Select N:",min = 1,max = 10,value = 5)
```
```{r fig.width = 10,fig.height = 1}
## output grobs normally,but no way to use reactive slider values
g1 <- boxGrob(5,x = 0.3,y = 0.5)
g2 <- boxGrob(5,x = 0.8,y = 0.5)
g1
g2
connectGrob(g1,g2,type = "horizontal")
```
```{r fig.width = 10,fig.height = 1}
## output grobs using render plot (allows inputs)
renderPlot({
g3 <- boxGrob(input$nSelect,y = 0.5)
g4 <- boxGrob(input$nSelect,y = 0.5)
connectGrob(g3,g4,type = "horizontal")
g3
g4
})
```