[英]Rshiny, reactive heatmap, can't change variable
i'm trying to do a heatmap with Rshiny. 我正在尝试与Rshiny做一个热图。 The user must be able to choose 2 variable: time and direction. 用户必须能够选择2个变量:时间和方向。 Depending on that the heatmap will change. 取决于此,热图将改变。
When i fix the value for time and direction, my code is working. 当我确定时间和方向的值时,我的代码正在工作。 When i try to use reactive input, it doesnt work. 当我尝试使用无功输入时,它不起作用。
error message is : Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. 错误消息是: .getReactiveEnvironment()$ currentContext()中的错误:如果没有活动的反应性上下文,则不允许进行操作。 (You tried to do something that can only be done from inside a reactive expression or observer.) (您试图做只能从反应式表达式或观察器内部完成的操作。)
JSd_inter is a function i can show you if needed. JSd_inter是我可以向您显示的功能。 In the first example i have fixed time with "T1" and direction with "both" Those 2 example are 2 different way i used to create my data frame for my heat map. 在第一个示例中,我将固定时间设置为“ T1” ,将方向设置为“ both”和“两个”。这2个示例是我用于创建热图数据框的2种不同方式。 example 1 work but i cant change value, example 2 doesnt work. 示例1工作,但我不能更改值,示例2不工作。 In Part 3/ you can find the code i use to plot my heat map 在第3部分中,您可以找到我用来绘制热图的代码
idt <- as.vector(unique(data$contratid))
n= length(idt)
a <- matrix(0, n,n)
colnames(a) <- idt
rownames(a) <- idt
for (i in 1:n) {
for (j in 1:n) {
a[i,j] <- JSD_inter(data,20,"T1","both",idt[i],idt[j])
}}
2nd example which doesnt work (even if i use input$timechoice instead of time() in my function, it's still doenst work) 第二个示例不起作用(即使我在函数中使用input $ timechoice而不是time(),它也仍然有效)
time <- reactive({input$timechoice})
direction<- reactive({input$dirchoice})
idt <- as.vector(unique(data$contratid))
n= length(idt)
a <- matrix(0, n,n)
colnames(a) <- idt
rownames(a) <- idt
for (i in 1:n) {
for (j in 1:n) {
a[i,j] <- JSD_inter(data,20,time(),direction(),idt[i],idt[j])
}}
Part 3/ code to plot the heat map (since example 1 is working, i dont think problem is coming from here) 第3部分/绘制热图的代码(由于示例1正常工作,我不认为问题出在这里)
reorder_cormat <- function(cormat){
# Utiliser la corrélation entre les variables
# comme mésure de distance
dd <- as.dist((1-cormat)/2)
hc <- hclust(dd)
cormat <-cormat[hc$order, hc$order]
}
# Obtenir le triangle supérieur
get_upper_tri <- function(cormat){
cormat[lower.tri(cormat)]<- NA
return(cormat)
}
# Reorder correlation matrix
cormat <- reorder_cormat(a)
upper_tri <- get_upper_tri(cormat)
# Fondre la matrice de corrélation
melted_cormat <- melt(upper_tri, na.rm = TRUE)
# Créer un ggheatmap
ggheatmap <- ggplot(melted_cormat, aes(Var2, Var1, fill = value))+
geom_tile(color = "white")+
scale_fill_gradient2(low = "white", high = "red",
midpoint = 0.09, limit = c(0,1), space = "Lab",
name="JSD") +
theme_minimal()+ # minimal theme
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))+
coord_fixed()
output$heat <- renderPlot(ggheatmap)
im priting the heat map in my shyniapp using ui.R with the code: 将ui.R与代码一起在我的shyniapp中使用热点代码:
tabPanel("Heatmap", plotOutput("heat")), tabPanel(“ Heatmap”,plotOutput(“ heat”)),
Please let me know if you need more information 请让我知道是否需要更多信息
Thanks for your time 谢谢你的时间
direction()
and time()
are reactive. direction()
和time()
是反应性的。 You cannot use them outside a reactive context. 您不能在反应式上下文之外使用它们。
Try this: 尝试这个:
myMatrix <- reactive({
idt <- as.vector(unique(data$contratid))
n= length(idt)
a <- matrix(0, n,n)
colnames(a) <- idt
rownames(a) <- idt
for (i in 1:n) {
for (j in 1:n) {
a[i,j] <- JSD_inter(data,20,time(),direction(),idt[i],idt[j])
}
}
a
})
and then 接着
melted_cormat <- reactive({
cormat <- reorder_cormat(myMatrix())
upper_tri <- get_upper_tri(cormat)
melt(upper_tri, na.rm = TRUE)
})
Finally you have to do the ggplot
inside renderPlot
, because it calls melted_cormat()
: 最后,您必须在ggplot
内执行renderPlot
,因为它调用了melted_cormat()
:
output$heat <- renderPlot({
ggplot(melted_cormat(), aes(Var2, Var1, fill = value))+
geom_tile(color = "white")+
scale_fill_gradient2(low = "white", high = "red",
midpoint = 0.09, limit = c(0,1), space = "Lab",
name="JSD") +
theme_minimal()+ # minimal theme
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))+
coord_fixed()
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.