繁体   English   中英

Rshiny,反应热图,无法更改变量

[英]Rshiny, reactive heatmap, can't change variable

我正在尝试与Rshiny做一个热图。 用户必须能够选择2个变量:时间和方向。 取决于此,热图将改变。

当我确定时间和方​​向的值时,我的代码正在工作。 当我尝试使用无功输入时,它不起作用。

错误消息是: .getReactiveEnvironment()$ currentContext()中的错误:如果没有活动的反应性上下文,则不允许进行操作。 (您试图做只能从反应式表达式或观察器内部完成的操作。)


JSd_inter是我可以向您显示的功能。 在第一个示例中,我将固定时间设置为“ T1” ,将方向设置为“ both”“两个”。这2个示例是我用于创建热图数据框的2种不同方式。 示例1工作,但我不能更改值,示例2不工作。 在第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])


}}

第二个示例不起作用(即使我在函数中使用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])


}}

第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)


将ui.R与代码一起在我的shyniapp中使用热点代码:

tabPanel(“ Heatmap”,plotOutput(“ heat”)),

请让我知道是否需要更多信息

谢谢你的时间

direction()time()是反应性的。 您不能在反应式上下文之外使用它们。

尝试这个:

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
})

接着

melted_cormat <- reactive({
  cormat <- reorder_cormat(myMatrix())
  upper_tri <- get_upper_tri(cormat)
  melt(upper_tri, na.rm = TRUE)
})

最后,您必须在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM