Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.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
将R Shining checkboxGroupInput与其他输入选项组合_R_Checkbox_Shiny - Fatal编程技术网

将R Shining checkboxGroupInput与其他输入选项组合

将R Shining checkboxGroupInput与其他输入选项组合,r,checkbox,shiny,R,Checkbox,Shiny,我有这些数据我想使用R闪亮的服务器来散布绘图: 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(

我有这些
数据
我想使用
R
闪亮的
服务器来
散布
绘图

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
问题是它似乎没有实际选择
样本
s,因此显示的绘图没有点

如果我简单地通过替换以下内容来消除
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您的代码中有两个错误。第一个在
checkboxGroupInput

而不是

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

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

第二个是
scatter.plot()
plotly对象
,因此您应该使用
plotly::plotlyOutput(“嵌入”)
output$Embedding,非常感谢@SBista。我还有几个问题你可能会碰巧帮我解决:1。如果我从终端窗口启动闪亮的应用程序,它将在浏览器中打开,绘图仅显示在页面的右上象限。有可能扩大吗?2.当我按下“保存”按钮时,生成的PDF包含两页,其中第二页为空白。有没有办法防止这种情况?“只保存一页的意思?”丹,你说的“扩展”是什么意思?它采用了可用的宽度。我指的是垂直空间
checkboxGroupInput("samples.choice", "Samples",choices = samples.choices,selected=1)
checkboxGroupInput("samples.choice", "Samples",choices = names(samples.choices),selected="all")
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)