将R有光泽的checkboxGroupInput与其他输入选择

时间:2018-05-01 01:27:33

标签: r checkbox shiny

我希望使用data scatter服务器plot R shiny这些library(dplyr) library(permute) set.seed(1) meta.df <- data.frame(gene_id=paste0("id",1:10),symbol=paste0("n",rep(permute::shuffle(5),2)),stringsAsFactors=F) clusters.df <- data.frame(cell=paste0("c",1:100),cluster=rep(permute::shuffle(10),10),sample=paste0("s",rep(permute::shuffle(5),20)),stringsAsFactors=F) mat <- matrix(rnorm(10*100),10,100,dimnames=list(meta.df$gene_id,clusters.df$cell)) tsne.obj <- Rtsne::Rtsne(t(mat)) tsne.df <- as.data.frame(tsne.obj$Y) %>% dplyr::rename(tSNE1=V1,tSNE2=V2) %>% cbind(clusters.df) samples <- c("all",unique(clusters.df$sample)) samples.choices <- 1:length(samples) names(samples.choices) <- samples

meta.df$symbol

由于我希望能够选择meta.df$gene_id中的多余sample,每个都有一个选择列表,其中第二个以第一个为条件。

由于数据由多个sample组成,我希望能够以反应方式按checkbox对数据进行子集化,因此我有一个样本选择"all" ,使用sample选项选择所有shiny s(只是因为它比检查所有框更容易)。

所以这是我的code server <- function(input, output) { chosen.samples <- reactive({ validate( need(input$samples.choice != "",'Please choose at least one of the sample checkboxes') ) samples.choice <- input$samples.choice if("all" %in% samples.choice) samples.choice <- samples[-which(samples == "all")] samples.choice }) output$gene_id <- renderUI({ selectInput("gene_id", "Gene ID", choices = unique(dplyr::filter(meta.df,symbol == input$symbol)$gene_id)) }) scatter.plot <- reactive({ if(!is.null(input$symbol) & !is.null(input$gene_id)){ # subset of data gene.symbol <- input$symbol gene.id <- input$gene_id row.idx <- which(rownames(mat) == gene.id) col.idx <- which(colnames(mat) %in% dplyr::filter(clusters.df,sample %in% chosen.samples())$cell) gene.df <- suppressWarnings(dplyr::left_join(tsne.df %>% dplyr::filter(sample %in% chosen.samples()),data.frame(cell=colnames(mat)[col.idx],value=mat[row.idx,col.idx],stringsAsFactors=F),by=c("cell"="cell"))) scatter.plot <- plotly::plot_ly(marker=list(size=12),type='scatter',mode="markers",color=~gene.df$value,x=~gene.df$tSNE1,y=~gene.df$tSNE2,showlegend=F) %>% plotly::layout(xaxis=list(title="tSNE1",zeroline=F,showticklabels=F),yaxis=list(title="tSNE2",zeroline=F,showticklabels=F)) scatter.plot } }) output$Embedding <- renderPlot({ scatter.plot() }) output$save <- downloadHandler( filename = function() { paste0(dplyr::filter(meta.df,symbol == input$symbol,gene_id == input$gene_id)$symbol,"_",dplyr::filter(meta.df,symbol == input$symbol,gene_id == input$gene_id)$gene_id,".pdf") }, content = function(file) { plotly::export(scatter.plot(),file=file) } ) } ui <- fluidPage( # App title ---- titlePanel("Results Explorer"), # Sidebar layout with a input and output definitions ---- sidebarLayout( # Sidebar panel for inputs ---- sidebarPanel( # select samples checkboxGroupInput("samples.choice", "Samples",choices = samples.choices,selected=1), # select gene symbol selectInput("symbol", "Gene Symbol", choices = unique(meta.df$symbol)), # select gene id uiOutput("gene_id"), # select plot type selectInput("plot.type", "Plot Type", choices = c("tSNE","PCA")), # save plot as html downloadButton('save', 'Save as PDF') ), # Main panel for displaying outputs ---- mainPanel( # The plot is called Embedding and will be created in ShinyServer part plotOutput("Embedding") ) ) ) shinyApp(ui = ui, server = server)

sample

问题在于它似乎没有实际选择sample,因此显示的情节没有分数。

如果我只是通过替换来消除code的选择col.idx <- which(colnames(mat) %in% dplyr::filter(clusters.df,sample %in% chosen.samples())$cell) gene.df <- suppressWarnings(dplyr::left_join(tsne.df %>% dplyr::filter(sample %in% chosen.samples()),data.frame(cell=colnames(mat)[col.idx],value=mat[row.idx,col.idx],stringsAsFactors=F),by=c("cell"="cell"))) ,它就可以找到:

col.idx <- which(colnames(mat) %in% dplyr::filter(clusters.df,sample %in% samples[2:3])$cell)
gene.df <- dplyr::left_join(tsne.df %>% dplyr::filter(sample %in% samples[2:3]),data.frame(cell=colnames(mat)[col.idx],value=mat[row.idx,col.idx],stringsAsFactors=F),by=c("cell"="cell"))

使用:

dat_reac

我在this example中看到整个数据都在reactive block sample中进行了子集化。我希望简单地将 $('#clear').click(function(event) { event.stopImmediatePropagation(); $("#clear").removeClass("active"); }); s转换为子集就足够了。知道为什么它不起作用以及如何正确使用它?

1 个答案:

答案 0 :(得分:2)

您的代码中有两个错误。第一个是checkboxGroupInput

而不是

checkboxGroupInput("samples.choice", "Samples",choices = samples.choices,selected=1)

应该是

checkboxGroupInput("samples.choice", "Samples",choices = names(samples.choices),selected="all")

第二个是scatter.plot() plotly object因此您应该使用plotly::plotlyOutput("Embedding")output$Embedding <- plotly::renderPlotly({ scatter.plot() })

以下是具有上述修改的代码:

server <- function(input, output)
{
  chosen.samples <- reactive({
    validate(
      need(input$samples.choice != "",'Please choose at least one of the sample checkboxes')
    )
    samples.choice <- input$samples.choice
    if("all" %in% samples.choice) samples.choice <- samples[-which(samples == "all")]
    samples.choice
  })

  output$gene_id <- renderUI({
    selectInput("gene_id", "Gene ID", choices = unique(dplyr::filter(meta.df,symbol == input$symbol)$gene_id))
  })

  scatter.plot <- reactive({

    if(!is.null(input$symbol) & !is.null(input$gene_id)){
      # subset of data
      gene.symbol <- input$symbol
      gene.id <- input$gene_id
      row.idx <- which(rownames(mat) == gene.id)
      col.idx <- which(colnames(mat) %in% dplyr::filter(clusters.df,sample %in% chosen.samples())$cell)
      gene.df <- suppressWarnings(dplyr::left_join(tsne.df %>% dplyr::filter(sample %in% chosen.samples()),data.frame(cell=colnames(mat)[col.idx],value=mat[row.idx,col.idx],stringsAsFactors=F),by=c("cell"="cell")))

      scatter.plot <- plotly::plot_ly(marker=list(size=12),type='scatter',mode="markers",color=~gene.df$value,x=~gene.df$tSNE1,y=~gene.df$tSNE2,showlegend=F) %>%
        plotly::layout(xaxis=list(title="tSNE1",zeroline=F,showticklabels=F),yaxis=list(title="tSNE2",zeroline=F,showticklabels=F))
      scatter.plot
    }
  })

  output$Embedding <- plotly::renderPlotly({
    scatter.plot()
  })

  output$save <- downloadHandler(
    filename = function() {
      paste0(dplyr::filter(meta.df,symbol == input$symbol,gene_id == input$gene_id)$symbol,"_",dplyr::filter(meta.df,symbol == input$symbol,gene_id == input$gene_id)$gene_id,".pdf")
    },
    content = function(file) {
      plotly::export(scatter.plot(),file=file)
    }
  )
}

ui <- fluidPage(

  # App title ----
  titlePanel("Results Explorer"),

  # Sidebar layout with a input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # select samples
      checkboxGroupInput("samples.choice", "Samples",choices = names(samples.choices),selected="all"),

      # select gene symbol
      selectInput("symbol", "Gene Symbol", choices = unique(meta.df$symbol)),

      # select gene id
      uiOutput("gene_id"),

      # select plot type
      selectInput("plot.type", "Plot Type", choices = c("tSNE","PCA")),

      # save plot as html
      downloadButton('save', 'Save as PDF')
    ),

    # Main panel for displaying outputs ----
    mainPanel(
      # The plot is called Embedding and will be created in ShinyServer part
      # plotOutput("Embedding")
      plotly::plotlyOutput("Embedding")
    )
  )
)

shinyApp(ui = ui, server = server)

希望它有所帮助!