我正在尝试使用我想使用observeEvent表达式更新的具有反应性尺寸的矩阵。我的想法如下:
首先,我将创建一个尺寸为input $ length(-> react)且输入为0的矩阵的reactValues对象。然后,我将observeEvent与actionButton一起使用以触发矩阵中的更新。这需要用反应性值(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)
我认为您将需要两个反应性“阶段”。
请检查以下内容:
library(shiny)
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))
beta.matrix <- reactiveValues(mat = NULL)
beta.matrix.ini <- reactive({
mat = matrix(0, input$length, input$length)
})
observe({
beta.matrix$mat <- beta.matrix.ini()
})
### 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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句