简体   繁体   中英

Rshiny, reactive heatmap, can't change variable

i'm trying to do a heatmap with Rshiny. The user must be able to choose 2 variable: time and direction. 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. (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. 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. example 1 work but i cant change value, example 2 doesnt work. In Part 3/ you can find the code i use to plot my heat map

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)

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)

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:

tabPanel("Heatmap", plotOutput("heat")),

Please let me know if you need more information

Thanks for your time

direction() and time() are reactive. 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() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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