【问题标题】:updateMatrixInput not working in observe loop in R shinyupdateMatrixInput 在 R 闪亮的观察循环中不起作用
【发布时间】:2021-05-17 14:16:39
【问题描述】:

我正在尝试通过在 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",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 吗? 我应该如何解决?

【问题讨论】:

  • 当您使用input$addSeries 时,您的lapply 需要在observer 内。

标签: r loops matrix shiny observers


【解决方案1】:

为你的 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
  })

【讨论】:

    猜你喜欢
    • 2016-07-25
    • 2015-10-01
    • 1970-01-01
    • 2022-01-17
    • 2014-02-12
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    相关资源
    最近更新 更多