updateMatrixInput 在 R 闪亮的观察循环中不起作用
updateMatrixInput not working in observe loop in R shiny
我正在尝试通过 lapply 循环中的观察更新 R shiny 中的“matrixInput”。但是,它似乎没有用。下面是代码。
library("shinydashboard")
library("shiny")
library("Rblpapi")
library("shinyMatrix")
ui <- sidebarLayout(
sidebarPanel(
actionButton("addSeries", HTML("Click here to add Series<br/>...")),
),
mainPanel(),
) #end of UI
server <- function(input, output, session){
observeEvent(input$addSeries, {
insertUI(
selector = "#addSeries",
multiple=TRUE,
where = "afterEnd",
ui = tags$hr(tags$div(
HTML(paste0("<b>" ,"------DataSet",input$addSeries,"------")),
selectInput(inputId = paste0("SeriesComponent",input$addSeries),
label = "select components here",
choices = c("hacker","not hacker","old man"),
selected="",
multiple=TRUE),
matrixInput(inputId = paste0("SeriesWeights",input$addSeries),
label = paste0("Weights for Series", input$addSeries),
value = matrix(c(1),1,1),
class = "numeric",
rows=list(names=TRUE),
cols = list(names=TRUE)
),
)#end of tags$div for adding data
)# end of tags$hr
)#end of insertUI for adding data
})#end of observe event for adding data
#lapply to update the Series weights reactively
lapply(seq_along(input$addSeries),
function(i){
observe({
updateMatrixInput(session, inputId=paste0("SeriesWeights",i),
value=matrix(input[[paste0("SeriesWeights",i)]], 1, length(input[[paste0("SeriesComponent",i)]]),dimnames=list("Weights",input[[paste0("SeriesComponent",i)]])))
})
}#end of function within lapply
)#end of lapply
}#end of server
shinyApp(ui, server)
我期望矩阵输入在我选择“SeriesComponents”下的元素时反应性地改变它的元素。但是,我收到以下错误:
Warning: Error in : Can't access reactive value 'addSeries' outside of reactive consumer.
i Do you need to wrap inside reactive() or observer()?
54: <Anonymous>
Error : Can't access reactive value 'addSeries' outside of reactive consumer.
i Do you need to wrap inside reactive() or observer()?
我不应该能够在反应环境之外访问 input$addSeries 吗?
我应该如何解决?
为您的 lapply
试试这个
####lapply to update the Series weights reactively
observe({
lapply(seq_along(req(input$addSeries)),
function(i){
req(input[[paste0("SeriesWeights",i)]])
updateMatrixInput(session, inputId=paste0("SeriesWeights",i),
value=matrix(input[[paste0("SeriesWeights",i)]], 1, length(input[[paste0("SeriesComponent",i)]]),dimnames=list("Weights",input[[paste0("SeriesComponent",i)]])) )
}#end of function within lapply
)#end of lapply
})
我正在尝试通过 lapply 循环中的观察更新 R shiny 中的“matrixInput”。但是,它似乎没有用。下面是代码。
library("shinydashboard")
library("shiny")
library("Rblpapi")
library("shinyMatrix")
ui <- sidebarLayout(
sidebarPanel(
actionButton("addSeries", HTML("Click here to add Series<br/>...")),
),
mainPanel(),
) #end of UI
server <- function(input, output, session){
observeEvent(input$addSeries, {
insertUI(
selector = "#addSeries",
multiple=TRUE,
where = "afterEnd",
ui = tags$hr(tags$div(
HTML(paste0("<b>" ,"------DataSet",input$addSeries,"------")),
selectInput(inputId = paste0("SeriesComponent",input$addSeries),
label = "select components here",
choices = c("hacker","not hacker","old man"),
selected="",
multiple=TRUE),
matrixInput(inputId = paste0("SeriesWeights",input$addSeries),
label = paste0("Weights for Series", input$addSeries),
value = matrix(c(1),1,1),
class = "numeric",
rows=list(names=TRUE),
cols = list(names=TRUE)
),
)#end of tags$div for adding data
)# end of tags$hr
)#end of insertUI for adding data
})#end of observe event for adding data
#lapply to update the Series weights reactively
lapply(seq_along(input$addSeries),
function(i){
observe({
updateMatrixInput(session, inputId=paste0("SeriesWeights",i),
value=matrix(input[[paste0("SeriesWeights",i)]], 1, length(input[[paste0("SeriesComponent",i)]]),dimnames=list("Weights",input[[paste0("SeriesComponent",i)]])))
})
}#end of function within lapply
)#end of lapply
}#end of server
shinyApp(ui, server)
我期望矩阵输入在我选择“SeriesComponents”下的元素时反应性地改变它的元素。但是,我收到以下错误:
Warning: Error in : Can't access reactive value 'addSeries' outside of reactive consumer.
i Do you need to wrap inside reactive() or observer()?
54: <Anonymous>
Error : Can't access reactive value 'addSeries' outside of reactive consumer.
i Do you need to wrap inside reactive() or observer()?
我不应该能够在反应环境之外访问 input$addSeries 吗? 我应该如何解决?
为您的 lapply
试试这个 ####lapply to update the Series weights reactively
observe({
lapply(seq_along(req(input$addSeries)),
function(i){
req(input[[paste0("SeriesWeights",i)]])
updateMatrixInput(session, inputId=paste0("SeriesWeights",i),
value=matrix(input[[paste0("SeriesWeights",i)]], 1, length(input[[paste0("SeriesComponent",i)]]),dimnames=list("Weights",input[[paste0("SeriesComponent",i)]])) )
}#end of function within lapply
)#end of lapply
})