如何解决updateMatrixInput 在 R 闪亮的观察循环中不起作用
我正在尝试通过在 lapply 循环中观察来更新 R 中的“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",label = paste0("Weights for Series",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)]],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 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
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。