首页 > 解决方案 > 如何将更新的 Shiny 矩阵输入值保存到数据框(或工作内存)中,以便进一步访问 R 代码中的矩阵输入数据?

问题描述

我是新来的闪亮和闪亮矩阵,虽然我已经完成了教程。下面的代码从概念上得到了我需要的东西。运行时,您可以通过单击网格底部的空单元格并键入一个值来添加输入矩阵,并且基本上可以使矩阵变得任意大。通过点击“更新”按钮,绘图将使用附加数据进行更新。非常好。

我的问题是:

  1. 如何从更新的矩阵输入中创建数据框或等效项?为了在 R 中进一步使用。按照目前的编码,“m”矩阵仅包含初始 2 个值,而不包含额外的网格输入。

  2. 运行时,为了使矩阵插入正常工作,我必须先在“y”列的空单元格中输入一个网格值,然后在左侧的“x”列中输入一个网格值。如果我先在“x”列中插入一个值,通常会阻止“y”列输入。有任何解决这个问题的方法吗?

  3. 运行时,请注意左侧的空列。我只想要 2 列,用于 x 和 y。如何消除第一个空列?

  4. 有一个更好的方法吗?输入矩阵需要在概念上是可变的,如下所示。我需要能够访问数据框中的这些矩阵输入以供进一步使用。

代码(来源是https://www.r-bloggers.com/2020/02/shinymatrix-matrix-input-for-shiny-apps/):

library(shiny)
library(shinyMatrix)

m <- matrix(runif(2), 1, 2, dimnames = list(NULL, c("x", "y"))) 
ui <- fluidPage(   
  titlePanel("shinyMatrix: Demo"),   
  sidebarPanel(     
    width = 6,     
    tags$h4("Data"),     
    matrixInput("sample",value = m,rows = list(extend = TRUE),cols = list(names = TRUE))
  ),
  actionButton(inputId = "go",
               label = "Update matrix"),
  mainPanel(     
    width = 6,plotOutput("scatter")) 
) 

server <- function(input, output, session) {   
  output$scatter <- renderPlot({     
    plot(input$sample, col = "red", main = "Scatterplot")}) 
} 

shinyApp(ui, server)

糟糕,我省略了上面的 eventReactive 函数!为了完整起见,重新发布代码(尽管它不会影响我的问题或任何答案):

m <- matrix(c(1,1), 1, 2, dimnames = list(NULL, c("x", "y"))) 

ui <- fluidPage(   
  
  titlePanel("shinyMatrix: Demo"),   
  
  sidebarPanel(
    
    # Input: specify the number of observations to view ----
    sliderInput("integer", "Number of periods to spread data inputs along:",
                min = 1, max = 120, value = 60),
    
    # Input: matrix grid ----
    width = 6,     
    tags$h5(strong("Data inputs:")),     
    matrixInput("sample",value = m,
                rows = list(extend = TRUE),
                cols = list(names = TRUE))
  ),
  
  actionButton(inputId = "go",label = "Update below:"),
  
  mainPanel(
    
    # Show input graph  ----  
    h5(strong("Plot of inputs:")),
    width = 6,plotOutput("scatter"),
    
    # Show table of inputs, to be replaced with interpolated matrix ----    
    h5(strong("Table of inputs:")),
    tableOutput("view")
    
  ) 
) 

server <- function(input, output, session) {   
  
  data <- eventReactive(input$go,{input$sample}, ignoreNULL = FALSE)
  
  # Process output graph; type "l" for lines, "p" for points, "b" for combined ----  
  output$scatter <- renderPlot({     
    plot(data(), type="b", main = 'Variables over time', col = "blue", pch = 19, cex = 1.25)
  })
  
  # Process output table, to be replaced with interpolated matrix ----    
  output$view <- renderTable({
    data()
  })
  
} 

shinyApp(ui, server)

标签: rmatrixinputshiny

解决方案


好的,所以我做了更多的研究并解决了这个问题。按照与原始问题相同的顺序回答:

  1. 要从 Shiny 反应数据的值创建 R 数据框(或向量),您需要从该反应数据生成“全局分配的向量”。要在本例中执行此操作,请将以下行添加到上述代码的 server 部分,在 server 下第一行读取“data <- eventReactive(input$go,{input$sample}, ignoreNULL = FALSE)”的下方:

    观察事件(数据(),{m.update <<-唯一(数据())})

在本例中,反应数据现在被捕获在“m.update”数据帧中。

我从Storing a reactive output in a vector - Shiny R 中得到了这个解决方案,并附有完整的解释

  1. 有关插入额外行的问题已得到解决。我在 GitHub 上发布了这个问题,开发人员很快回复并解决了。shinyMatrix 的新版本 0.6.1 不再是问题

  2. 现在我知道如何更好地使用 shinyMatrix 我对最左边的列很好,用于标签。为左列自动生成名称(标签)很容易

  3. 经过大量研究和上述解决方案,shinyMatrix 看起来像是将多个数据字段输入到闪亮的反应函数中的最佳解决方案。我很满意!!


推荐阅读