【发布时间】:2021-05-19 08:04:23
【问题描述】:
我是新来的闪亮和闪亮矩阵,虽然我已经完成了教程。下面的代码从概念上得到了我需要的东西。运行时,您可以通过单击网格底部的空单元格并键入一个值来添加到输入矩阵,并且基本上可以使矩阵尽可能大。通过点击“更新”按钮,绘图将使用附加数据进行更新。很不错。
我的问题是:
-
如何从更新的矩阵输入中创建数据框或等效项?为了在 R 中进一步使用。按照目前的编码,“m”矩阵仅包含初始 2 个值,而不包含额外的网格输入。
-
运行时,要使矩阵插入正常工作,我必须先在“y”列的空单元格中输入一个网格值,然后在左侧的“x”列中输入一个网格值。如果我先在“x”列中插入一个值,通常会阻止“y”列输入。有什么办法解决这个问题?
-
运行时,请注意左侧的空列。我只想要 2 列,用于 x 和 y。如何消除第一个空列?
-
有没有更好的方法来做到这一点?输入矩阵需要在概念上是可变的,如下所示。我需要能够访问数据框中的这些矩阵输入以供进一步使用。
代码(来源为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)
【问题讨论】: