当按下“填充矩阵”按钮时,我想根据第一行的用户输入值更新矩阵输入。矩阵正确更新(如打印),但输入字段不会相应更改。任何建议将不胜感激!
ui.R
library(shiny)
library(shinyIncubator) #for matrixInput
df <- data.frame(matrix(c("0","0"), 1, 2))
colnames(df) <- c("Input1", "Input2")
shinyUI(
fluidPage(
matrixInput( inputId = "matrix",
label = "matrixLabels",
data = df),
actionButton(inputId = "fillMatrix", label = "Fill matrix"),
verbatimTextOutput(outputId = "matrix")
)
)
server.R
serverFunction <- function(input, output, session){
updateMatrixInput <- function(session, inputId, label = NULL, data = NULL) {
message <- list(label = label, data = data)
session$sendInputMessage(inputId, message)
}
observeEvent(input$fillMatrix, {
oldMatrix <- input$matrix
firstRow <- oldMatrix[1,]
currentProportions <- sum( oldMatrix[,2], na.rm = TRUE )
isMissing <- which( is.na(oldMatrix[,2]) )
nRows <- nrow(oldMatrix)
oldMatrix[2:nRows,1] <- firstRow[1]
oldMatrix[isMissing,2] <- (1 - currentProportions) / length(isMissing)
output$matrix <- renderPrint({ oldMatrix })
updateMatrixInput(session = session, inputId = "matrix",
label = "matrixLabels",
data = oldMatrix)
})
}
serverFunction