繁体   English   中英

updateMatrixInput 在 R shiny 的观察循环中不起作用

[英]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 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
  })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM