【问题标题】:Rshiny, reactive heatmap, can't change variableR Shiny,反应热图,不能改变变量
【发布时间】:2019-01-30 09:43:03
【问题描述】:

我正在尝试使用 Rshiny 制作热图。用户必须能够选择 2 个变量:时间和方向。取决于热图会改变。

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

错误消息是:.getReactiveEnvironment()$currentContext() 中的错误: 如果没有活动的反应上下文,则不允许操作。 (你试图做一些只能在反应式表达式或观察者内部完成的事情。)


JSd_inter 是一个我可以在需要时向您展示的功能。在第一个示例中,我使用 "T1" 设置了固定时间,使用 "both" 设置了方向 这两个示例是我用来为热图创建数据框的两种不同方式.示例 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("热图", plotOutput("heat")),

如果您需要更多信息,请告诉我

感谢您的宝贵时间

【问题讨论】:

    标签: r shiny heatmap reactive


    【解决方案1】:

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

    最后你必须在renderPlot里面做ggplot,因为它调用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()
    })
    

    【讨论】:

      猜你喜欢
      • 2015-06-25
      • 1970-01-01
      • 2019-03-27
      • 2021-09-25
      • 1970-01-01
      • 2021-01-07
      • 2019-11-06
      • 2020-02-03
      • 2022-01-02
      相关资源
      最近更新 更多