【发布时间】:2020-07-07 06:33:21
【问题描述】:
我正在尝试使用要使用 observeEvent 表达式更新的反应维度构建矩阵。我的想法如下:
首先,我创建了一个带有维度 input$length (--> reactive) 的矩阵和输入 0 的 reactiveValues 对象。然后我使用带有 actionButton 的 observeEvent 来触发矩阵中的更新。这需要使用反应值 (value()) 更新矩阵中的特定单元格,由反应索引向量 (ind()) 指示。
我理解这个问题:在 mat = ... 我不能使用另一个反应式表达式,但是我没有替代解决方案,非常感谢任何关于此的输入。
提前致谢!
亲切的问候,
朱利安
ui <- fluidPage(
numericInput("length", "Dimensions of the matrix", value = 5),
numericInput("a", "value for a", value = 2),
numericInput("b", "value for b", value = 2),
numericInput("ind1", "value for index vector 1", value = 1),
numericInput("ind2", "value for index vector 2", value = 1),
actionButton("go", "Update"),
tableOutput("matrix")
)
server <- function(input, output) {
### Calculate the value that will be used for the update
value <- reactive(
mean(rbeta(100, input$a, input$b))
)
### Create a reactive index vector used to determine the position of the cell in the matrix
ind <- reactive(
c(input$ind1, input$ind2)
)
### Create reactiveValues matrix with dimensions specified in length
beta.matrix <- reactiveValues(
mat = matrix(0, input$length, input$length)
)
### Update matrix at positon ind with new value
observeEvent(input$go, {
beta.matrix$mat[ind()[1], ind()[2]] <- value()
}
)
### Render matrix
output$matrix <- renderTable({
mat <- beta.matrix$mat
mat
})
}
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】: