在 Shiny 中编辑反应函数的策略,'data' 必须是矢量类型,是 'NULL' 错误
Strategies for editing reactive functions in Shiny, 'data' must be of a vector type, was 'NULL' error
目标:我正在尝试创建一个闪亮的应用程序来显示 (1) 非度量多维缩放解决方案的应力图,(2) 点配置的 ggplot,以及 (3) 聚类结果通过绘制点配置和叠加聚类的 chulls 来配置点。
问题:前两个图没有困难。我得到的不是第三个图,而是错误:'data' must be of a vector type, was 'NULL'
如果有任何关于如何解决具体问题的建议,我将不胜感激,即 "error in array: 'data' must be of a vector type, was 'NULL'"
如果有任何有关如何调试 shiny 的一般性建议,我也将不胜感激。我唯一的策略是将代码视为非响应式代码,我怀疑这种策略不是非常有效。
我的尝试解决:我已经搜索了关于 rseek 和堆栈溢出的错误并查看了帖子。在一些出现类似错误的情况下,问题是没有计算必要的数据。我检查了代码,将其视为普通(非反应性)代码,并使用了假数据。当我这样做时我没有遇到任何问题,所以我认为这是关于反应性的问题?关于如何调试的问题 2 是对尝试像代码不是动态的那样进行调试并没有发现问题这一事实的反应。
可重现的示例:我组装了一个具有随机生成数据的闪亮应用程序。在进行测试之前,我更新了 R 和我使用的所有包。
# Packages and options
library(shiny)
library(vegan)
library(cluster)
library(tidyverse)
options(digits = 3)
# Create dissimilarity matrix
d <- rnorm(1000)
mat <- matrix(d, ncol = 10)
diss_m <- daisy(mat) %>% as.matrix()
# Function
find_chulls <- function(df, x, y) {
ch <- chull(df[[x]], df[[y]])
df[ch,] %>% as.data.frame()
}
ui <- fluidPage(
titlePanel("Research"),
sidebarLayout(
sidebarPanel(
numericInput('dim', 'Dimensions', 2, min = 2, max = 15)
),
mainPanel(
h3('Stressplot'),
plotOutput('plot0'),
h3('Non-Metric Multidimensional Scaling'),
plotOutput('plot1'),
h3('2d Density Plot'),
plotOutput('plot2'),
h3('Cluster Analysis'),
plotOutput('plot3')
)
)
)
server <- function(input, output, session) {
nmds <- reactive({
metaMDS(diss_m,
distance = "euclidean",
k = input$dim,
trymax = 200,
autotransform = FALSE,
noshare = FALSE,
wascores = FALSE)
})
output$plot0 <- renderPlot({
stressplot(nmds())
})
pts <- reactive({
nmds()$points %>% as.data.frame()
})
output$plot1 <- renderPlot({
ggplot(pts(), aes(x = MDS1, y = MDS2)) +
geom_point()
})
output$plot2 <- renderPlot({
ggplot(pts(), aes(x = MDS1, y = MDS2)) +
geom_point() +
geom_density2d()
})
df_cl <- reactive({
km <- kmeans(x = pts(), centers = input$clust)
cl <- km$cluster
data.frame(pts(), clust = cl)
})
df_ch <- reactive({
df_ch_temp <- df_cl() %>% group_by(clust) %>% do(find_chulls(., 1, 2))
df_ch_temp %>% as.data.frame()
})
下面的情节是行不通的
output$plot3 <- renderPlot({
ggplot(df_ch(), aes(x = MDS1, y = MDS2, fill = as.factor(clust))) + geom_polygon(alpha = 0.10)
})
}
# Run the application
shinyApp(ui = ui, server = server)
您的 input$clust
未定义于:
df_cl <- reactive({
km <- kmeans(x = pts(), centers = input$clust)
cl <- km$cluster
data.frame(pts(), clust = cl)
})
您需要为集群添加输入绑定,例如:
numericInput('clust', 'Clusters', 2, min = 2, max = 15)
至于调试:我在 df_cl
的顶部添加了 browser()
,然后执行停止,您可以在终端中检查变量和 运行 代码(例如在 Rstudio 中)。当我 运行 km <- kmeans(x = pts(), centers = input$clust)
我得到你描述的错误然后可以看到输入不包含 clust 元素。
目标:我正在尝试创建一个闪亮的应用程序来显示 (1) 非度量多维缩放解决方案的应力图,(2) 点配置的 ggplot,以及 (3) 聚类结果通过绘制点配置和叠加聚类的 chulls 来配置点。
问题:前两个图没有困难。我得到的不是第三个图,而是错误:'data' must be of a vector type, was 'NULL'
如果有任何关于如何解决具体问题的建议,我将不胜感激,即 "error in array: 'data' must be of a vector type, was 'NULL'"
如果有任何有关如何调试 shiny 的一般性建议,我也将不胜感激。我唯一的策略是将代码视为非响应式代码,我怀疑这种策略不是非常有效。
我的尝试解决:我已经搜索了关于 rseek 和堆栈溢出的错误并查看了帖子。在一些出现类似错误的情况下,问题是没有计算必要的数据。我检查了代码,将其视为普通(非反应性)代码,并使用了假数据。当我这样做时我没有遇到任何问题,所以我认为这是关于反应性的问题?关于如何调试的问题 2 是对尝试像代码不是动态的那样进行调试并没有发现问题这一事实的反应。
可重现的示例:我组装了一个具有随机生成数据的闪亮应用程序。在进行测试之前,我更新了 R 和我使用的所有包。
# Packages and options
library(shiny)
library(vegan)
library(cluster)
library(tidyverse)
options(digits = 3)
# Create dissimilarity matrix
d <- rnorm(1000)
mat <- matrix(d, ncol = 10)
diss_m <- daisy(mat) %>% as.matrix()
# Function
find_chulls <- function(df, x, y) {
ch <- chull(df[[x]], df[[y]])
df[ch,] %>% as.data.frame()
}
ui <- fluidPage(
titlePanel("Research"),
sidebarLayout(
sidebarPanel(
numericInput('dim', 'Dimensions', 2, min = 2, max = 15)
),
mainPanel(
h3('Stressplot'),
plotOutput('plot0'),
h3('Non-Metric Multidimensional Scaling'),
plotOutput('plot1'),
h3('2d Density Plot'),
plotOutput('plot2'),
h3('Cluster Analysis'),
plotOutput('plot3')
)
)
)
server <- function(input, output, session) {
nmds <- reactive({
metaMDS(diss_m,
distance = "euclidean",
k = input$dim,
trymax = 200,
autotransform = FALSE,
noshare = FALSE,
wascores = FALSE)
})
output$plot0 <- renderPlot({
stressplot(nmds())
})
pts <- reactive({
nmds()$points %>% as.data.frame()
})
output$plot1 <- renderPlot({
ggplot(pts(), aes(x = MDS1, y = MDS2)) +
geom_point()
})
output$plot2 <- renderPlot({
ggplot(pts(), aes(x = MDS1, y = MDS2)) +
geom_point() +
geom_density2d()
})
df_cl <- reactive({
km <- kmeans(x = pts(), centers = input$clust)
cl <- km$cluster
data.frame(pts(), clust = cl)
})
df_ch <- reactive({
df_ch_temp <- df_cl() %>% group_by(clust) %>% do(find_chulls(., 1, 2))
df_ch_temp %>% as.data.frame()
})
下面的情节是行不通的
output$plot3 <- renderPlot({
ggplot(df_ch(), aes(x = MDS1, y = MDS2, fill = as.factor(clust))) + geom_polygon(alpha = 0.10)
})
}
# Run the application
shinyApp(ui = ui, server = server)
您的 input$clust
未定义于:
df_cl <- reactive({
km <- kmeans(x = pts(), centers = input$clust)
cl <- km$cluster
data.frame(pts(), clust = cl)
})
您需要为集群添加输入绑定,例如:
numericInput('clust', 'Clusters', 2, min = 2, max = 15)
至于调试:我在 df_cl
的顶部添加了 browser()
,然后执行停止,您可以在终端中检查变量和 运行 代码(例如在 Rstudio 中)。当我 运行 km <- kmeans(x = pts(), centers = input$clust)
我得到你描述的错误然后可以看到输入不包含 clust 元素。