如何基于Shiny selectInput更新Heatmap

时间:2015-11-11 15:43:07

标签: r shiny

我正在创建一个坐标列表,供ggplot应用程序中的Shiny使用。但是,我可以选择的可能文件中的列名称是不同的。有些数据集包含mock.coords中的所有内容,有些则不包括。

例如,当我没有Spot11时,以下代码有效,但是一旦我添加Spot11,它就告诉我Error: undefined columns selected。如果我没有在我Spot11中选择的数据集中显示selectInput,是否可以不包含Position9,以避免此错误?

我还尝试删除Error: arguments imply differing number of rows: 0, 1,例如收到了library(grid) library(ggplot2) library(gridExtra) sensor.data <- read.csv("C:/dataset") # Create position -> coord conversion pos.names <- names(sensor.data)[ grep("*Pos",names(sensor.data)) ] # pos.names gets all column names with pos in it. # Get column names with "Pos" in them mock.coords <- list ("Position1"=data.frame("x"=0.1,"y"=0.2), "Position2"=data.frame("x"=0.2,"y"=0.4), "Position3"=data.frame("x"=0.3,"y"=0.6), "Position4"=data.frame("x"=0.4,"y"=0.65), "Position5"=data.frame("x"=0.5,"y"=0.75), "Position6"=data.frame("x"=0.6,"y"=0.6), "Position7"=data.frame("x"=0.7,"y"=0.6), "Position8"=data.frame("x"=0.8,"y"=0.43), "Position8.1"=data.frame("x"=0.85,"y"=0.49), "Position9"=data.frame("x"=0.9,"y"=0.27), "Position10"=data.frame("x"=0.75,"y"=0.12)) # Change format of your data matrix df.l <- list() cnt <- 1 for (i in 1:nrow(sensor.data)){ for (j in 1:length(pos.names)){ name <- pos.names[j] curr.coords <- mock.coords[[name]] df.l[[cnt]] <- data.frame("x.pos"=curr.coords$x, "y.pos"=curr.coords$y, "heat" =sensor.data[i,j]) cnt <- cnt + 1 } } df <- do.call(rbind, df.l) # Load image library(jpeg) img <- readJPEG("any_imagecan_go_here") g <- rasterGrob(img, interpolate=TRUE,width=1,height=1) # Manually set number of rows and columns in the matrix containing max of heat for each square in grid nrows <- 50 ncols <- 50 # Define image coordinate ranges x.range <- c(0,1) # x-coord range y.range <- c(0,1) # x-coord range x.bounds <- seq(from=min(x.range),to=max(x.range),length.out = ncols + 1) y.bounds <- seq(from=min(y.range),to=max(y.range),length.out = nrows + 1) # Create matrix and set all entries to 0 heat.max.dat <<- matrix(nrow=nrows,ncol=ncols) lapply(1:length(mock.coords), function(i){ c <- mock.coords[[i]] # calculate where in matrix this fits x <- c$x; y <- c$y x.ind <- findInterval(x, x.bounds) y.ind <- findInterval(y, y.bounds) heat.max.dat[x.ind,y.ind] <<- max(sensor.data[names(mock.coords)[i]]) }) heat.max.dat[is.na(heat.max.dat)]<-0 require(fields) # Look at the image plots to see how the smoothing works #image(heat.max.dat) h.mat.interp <- image.smooth(heat.max.dat) #image(h.mat.interp$z) mat <- h.mat.interp$z require(reshape2) m.dat <- melt(mat) # Change to propper coors, image is assumed to have coors between 0-1 m.dat$Var1 <- seq(from=min(x.range),to=max(x.range),length.out=ncols)[m.dat$Var1] m.dat$Var2 <- seq(from=min(y.range),to=max(y.range),length.out=ncols)[m.dat$Var2] # Show where max temperature is heat.dat <- sensor.data[pos.names] # Get max for each position max.df <- apply(heat.dat,2,max) dat.max.l <- lapply(1:length(max.df), function(i){ h.val <- max.df[i] c.name <- names(h.val) c.coords <- mock.coords[[c.name]] data.frame("x.pos"=c.coords$x, "y.pos"=c.coords$y,"heat"=h.val) }) dat.max <- do.call(rbind,dat.max.l) coords <- data.frame("x"=c(0,1),"y"=c(0,1)) coords2 <- do.call(rbind, mock.coords) coords2$labels <- names(mock.coords) ggplot(data=coords,aes(x=x,y=y)) + annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) + geom_raster(data=m.dat,aes(x=Var1,y=Var2,fill=value), interpolate = TRUE, alpha=0.5) + scale_fill_gradientn(colours = rev( rainbow(3) ),guide=FALSE) + geom_text(data=coords2,aes(x=x,y=y,label=labels),vjust=-1,color="white",size=5) + geom_text(data=dat.max,aes(x=x.pos,y=y.pos,label=round(heat,3)),vjust=0,color="white",size=5) + scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0)) + ggtitle("Heatmap") + theme(plot.title = element_text(lineheight=0.8, face="bold")) 。试图避免这种情况,而不是让它如此静止。

可在此处找到数据集:https://docs.google.com/spreadsheets/d/11I19uQyND7YehKrYd-NPiib4bQSHmHmWENFvausigvU/edit

Position9

更新 当我进入我的Shiny应用程序并附加heatmap.R时出现问题。当我使用修改过的heatmap.R时,我的Shiny应用程序会出现我所描述的错误。如果我将Spot9更改为Spot9SET $str = COALESCE($str, '');的文字仍会显示,但没有任何数据。如果文本不在所选的数据集中,我不希望文本显示出来。

0 个答案:

没有答案