Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据复选框选择从Rshiny中的data.frame中选择多列并显示热图_R_Checkbox_Shiny_Data Visualization - Fatal编程技术网

根据复选框选择从Rshiny中的data.frame中选择多列并显示热图

根据复选框选择从Rshiny中的data.frame中选择多列并显示热图,r,checkbox,shiny,data-visualization,R,Checkbox,Shiny,Data Visualization,大家好 我是Rshiny的新手,一直在玩弄用户反应元素。我正在尝试创建一个热图,它基本上以data.frame作为输入,用户可以在其中选择要显示的行数和列数。特别是,如果用户选择复选框选项,将选择许多列(如果未选中复选框,则取消选择) 我的示例代码如下所示-它接受一个包含99个元素(行)的TSV输入文件,并有20列值。我提供了增加或减少行作为滑块的选项,以及5个复选框(表示“col_group_xx”),每个复选框选择一组4列(从热图中添加或删除这些列)。i、 e.“col_group_1”将选

大家好

我是Rshiny的新手,一直在玩弄用户反应元素。我正在尝试创建一个热图,它基本上以data.frame作为输入,用户可以在其中选择要显示的行数和列数。特别是,如果用户选择复选框选项,将选择许多列(如果未选中复选框,则取消选择)

我的示例代码如下所示-它接受一个包含99个元素(行)的TSV输入文件,并有20列值。我提供了增加或减少行作为滑块的选项,以及5个复选框(表示“col_group_xx”),每个复选框选择一组4列(从热图中添加或删除这些列)。i、 e.“col_group_1”将选择或取消选择前4列,“col_group_2”将选择第5列至第8列,依此类推

我的行滑块工作,热图适当地减少或增加行,但我似乎不知道如何连接复选框来选择每组列-

它返回此错误-

Warning: Error in [.data.frame: undefined columns selected
  [No stack trace available]
在此处下载示例TSV输入文件-

github链接到代码,便于下载-

我的代码如下-

library(d3heatmap)
library(RColorBrewer)
library(shiny)
library(shinythemes)
library(reprex)
library(dplyr)


data<-read.csv("example_matrix_for_heatmap.txt", header=TRUE, row.names = 1, sep="\t")
rownames(data)
nrow(data)
dim(data)

new_data_matrix <- data.frame(rownames(data))

colnames <- c("col_group_1","col_group_2","col_group_3","col_group_4","col_group_5")

####ui####
ui<-fluidPage(
  titlePanel("example_heatmap"), 
  theme=shinytheme("cerulean"),

  sidebarPanel(
    sliderInput("obs",
                "Number of observations:",
                min = 1,
                max = nrow(data),
                value = nrow(data)),
    tableOutput("values"),

    #group of checkboxes
    checkboxGroupInput("checkGroup", 
                       label = h3("columns to select"),
                       choices = colnames,
                       selected = colnames)
  ),

  mainPanel(
    d3heatmapOutput("heatmap", 
                    height="1200px", width="80%")
  ),


  fluidRow(column(3, verbatimTextOutput("value")))
)

####server####
server <- function(input, output) 
{
  output$value <- renderPrint({ input$checkGroup })

  observeEvent(input$checkGroup,{
    if("col_group_1" %in% input$checkGroup){
      print("col_group_1") ##debuging
      new_data_matrix <- cbind(new_data_matrix,data[,1:4])
    }
    if("col_group_2" %in% input$checkGroup ){
      print("col_group_2") ##debuging
      new_data_matrix <- cbind(new_data_matrix,data[,5:8])
    }
    if("col_group_3" %in% input$checkGroup ){
      print("col_group_3") ##debuging
      new_data_matrix <- cbind(new_data_matrix,data[,9:12])
    }
    if("col_group_4" %in% input$checkGroup ){
      print("col_group_4") ##debuging
      new_data_matrix <- cbind(new_data_matrix,data[,13:16])
    }
    if("col_group_5" %in% input$checkGroup ){
      print("col_group_5") ##debuging
      new_data_matrix <- cbind(new_data_matrix,data[,17:20])
    }
    dim(new_data_matrix) ##debuging
  })

  output$heatmap <- renderD3heatmap({
    d3heatmap(new_data_matrix[1:input$obs,2:ncol(new_data_matrix)],
              col=brewer.pal(9,"Reds"),
              scale="none")}
  )
}

shinyApp(ui, server)
库(D3热图)
图书馆(RColorBrewer)
图书馆(闪亮)
图书馆(shinythemes)
图书馆(reprex)
图书馆(dplyr)

数据同意@r2evans的建议,您可以使用
反应式
块。您可以设置两个块(一个用于选择列,另一个用于根据这些选定列对数据进行子集)

在下面的示例中,我仅使用一个
反应性
块来基于观察值和列进行子集。此外,使用一个简单的表将组与特定列范围相关联可能更容易(在本例中为
col\u group
)。这可以根据您的需要进行修改,并允许一些灵活性

reactive
块将确定要使用的列范围。使用
Map
功能,您可以将所有字段放在一个向量中,以用于子集数据

还添加了
renderD3heatmap
中的
validate
,这将确保您至少检查了一个组,并且基于输入滑块至少有两个观察值

library(d3heatmap)
library(RColorBrewer)
library(shiny)
library(shinythemes)
library(reprex)
library(dplyr)

data<-read.csv("example_matrix_for_heatmap.txt", header=TRUE, row.names = 1, sep="\t")

col_group <- data.frame(
  group = c("col_group_1","col_group_2","col_group_3","col_group_4","col_group_5"),
  min_col = c(1, 5, 9, 13, 17),
  max_col = c(4, 8, 12, 16, 20)
)

####ui####
ui<-fluidPage(
  titlePanel("example_heatmap"), 
  theme=shinytheme("cerulean"),

  sidebarPanel(
    sliderInput("obs",
                "Number of observations:",
                min = 1,
                max = nrow(data),
                value = nrow(data)),
    tableOutput("values"),

    #group of checkboxes
    checkboxGroupInput("checkGroup", 
                       label = h3("columns to select"),
                       choices = col_group[, "group"],
                       selected = col_group[, "group"])
  ),

  mainPanel(
    d3heatmapOutput("heatmap", 
                    height="1200px", width="80%")
  ),


  fluidRow(column(3, verbatimTextOutput("value")))
)

####server####
server <- function(input, output) 
{
  new_data_matrix <- reactive({
    col_ranges <- col_group %>% 
      filter(group %in% input$checkGroup)

    all_cols <- unlist(Map(`:`, col_ranges$min_col, col_ranges$max_col))

    data[1:input$obs, all_cols]
  })

  output$heatmap <- renderD3heatmap({
    validate(
      need(input$checkGroup, 'Check at least one group!'),
      need(input$obs >= 2, 'Need at least 2 groups to cluster!')
    )
    d3heatmap(new_data_matrix(),
              col=brewer.pal(9,"Reds"),
              scale="none")
  })
}

shinyApp(ui, server)
库(D3热图)
图书馆(RColorBrewer)
图书馆(闪亮)
图书馆(shinythemes)
图书馆(reprex)
图书馆(dplyr)

数据您误解了变量的范围以及如何反应性地处理数据。我建议将被动(不观察)块
myfields设置为true,我的印象是被动块只接受用户交互元素的直接输入,但显然我可以将它们链接起来以执行复杂的操作。谢谢你的提示。同意你的评论,我没有完全理解反应式的工作原理——正如我对@r2evans的回复——我的印象是,反应式只接受用户交互元素的直接输入。也感谢您使用地图功能!这是一些新的东西,在探索了它的工作原理之后——似乎这就是我在编码过程中需要的东西!