【问题标题】:R - Shiny App - Using plotly to visualize shinyMatrix inputR - Shiny App - 使用 plotly 可视化 shinyMatrix 输入
【发布时间】:2021-09-25 04:45:16
【问题描述】:

)

我想使用plotlymesh3d-plot 可视化shinyMatrix 的输入。我使用for-loop 将矩阵输入转换为具有 3 列(x、y 和 z)的数据框 - 见下文。这在闪亮的应用程序之外运行时有效。不幸的是,我坚持在reactive() 环境中使用这个for循环来将它传递给plot_ly()。它说“闭包类型的对象不是子集”。我读到,如果您不将反应性对象视为函数(我确实这样做了),则通常会出现此错误。

我知道我是一个初学者,我对闪亮应用程序的语法没有多少线索。很可能我犯了一个愚蠢的错误:-)但我不知道如何解决这个问题。

谢谢!

library(shiny)
library(shinyMatrix)

ui <- fluidPage(   titlePanel("shinyMatrix: Simple App"),   
                   sidebarPanel(width = 6,tags$h4("Data"),     
                                     matrixInput("mat", 
                                                 value = matrix(1:100, 10, 10), 
                                                 rows = list(names=T, extend = TRUE), 
                                                 cols = list(names = TRUE))),
                   mainPanel(width = 6,
                             plotlyOutput(
                                 "plotly",
                                 width = "100%",
                                 height = "400px",
                                 inline = FALSE,
                                reportTheme = TRUE
                             ))) 
   
 
server <- function(input, output, session) {  

df <- data.frame(x="", y="", z="")   

df <- reactive({    
    n=1
    for(i in 1:nrow(input$mat)){
        for(j in 1:ncol(input$mat)){
            df[n,1] <- j*laenge
            df[n,2] <- i*laenge
            df[n,3] <- input$mat[i,j]
            n=n+1
        }
    } 
    })

output$plotly <- renderPlotly({plot_ly(df(), x=~x, y=~y, z=~z, type="mesh3d")})   
}


shinyApp(ui, server)

【问题讨论】:

    标签: r matrix shiny reactive


    【解决方案1】:

    主要问题是您对反应式和数据框使用了相同的名称df。此外,您的响应式必须返回一些内容才能使您的代码正常工作,即在您的 for 循环之后返回数据帧。

    server <- function(input, output, session) {
      dd <- data.frame(x = NA, y = NA, z = NA)
    
      df <- reactive({
        n <- 1
        for (i in 1:nrow(input$mat)) {
          for (j in 1:ncol(input$mat)) {
            dd[n, 1] <- j * laenge
            dd[n, 2] <- i * laenge
            dd[n, 3] <- input$mat[i, j]
            n <- n + 1
          }
        }
        dd
      })
    
      output$plotly <- renderPlotly({
        plot_ly(df(), x = ~x, y = ~y, z = ~z, type = "mesh3d")
      })
    }
    

    【讨论】:

    • 非常感谢!周末愉快:-)
    猜你喜欢
    • 2017-02-23
    • 2021-11-19
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 2015-06-25
    • 1970-01-01
    相关资源
    最近更新 更多