R-闪亮的应用程序-使用情节可视化闪亮的矩阵输入



(

我想使用plotly网格3d图来可视化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)

主要问题是您对反应帧和数据帧使用了相同的名称df。此外,您的reactive必须返回一些东西才能使代码工作,即在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")
})
}

最新更新