Объединение R блестящий checkboxGroupInput с другими вариантами ввода - PullRequest
0 голосов
/ 01 мая 2018

У меня есть эти 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 s (просто потому, что это проще, чем отметка всех полей).

Так вот мой shiny 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 s 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 reactive block. Я ожидаю, что просто получить sample s для подмножества будет достаточно. Есть идеи, почему это не работает и как правильно это сделать?

1 Ответ

0 голосов
/ 01 мая 2018

В вашем коде есть две ошибки. Первый в 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)

Надеюсь, это поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...