如何使用 R shiny 绘制热图
How to plot heatmap with R shiny
我正在为 pheatmap 使用 R shiny,我想读取文件并绘制热图,但它没有用。可以读取csv文件,但是网页上看不到内容,无法绘制热图。
library(shiny)
library(pheatmap)
ui = fluidPage("Test",
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
tabPanel('map',
sidebarLayout(
sidebarPanel('side',
actionButton('getHmap', 'get heatmap')
),
mainPanel('main',
plotOutput("themap")
)
))
)
server = function(input, output, session) {
a <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
tbl <- read.csv(inFile$datapath, header=input$header, sep=input$sep, dec = input$dec)
return(tbl)
})
output$table.output <- renderTable({
a()
})
observeEvent(input$getHmap, {
row.names(a) <- a$Name
a <- a[,-1]
a[is.na(a)] <- 0
output$themap = renderPlot({
pheatmap(a)
})
})
}
shinyApp(ui, server)
```[![The original data I used][1]][1]
[1]: https://i.stack.imgur.com/S83cH.png
这可能是一个完整的工作示例。这似乎对我有用。进行了以下更改:
- 已将
tableOutput("table.output")
添加到 ui
- 已将
read.csv
简化为 inputs
,因为 sep
和 dec
缺失
- 创建了
plotData
函数作为 eventReactive
使用操作按钮绘制热图
- 在为绘图添加行名之前将数据转换为矩阵
output$themap
调用plotData
函数
library(shiny)
library(pheatmap)
ui = fluidPage("Test",
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
tabPanel('map',
sidebarLayout(
sidebarPanel('side',
actionButton('getHmap', 'get heatmap')
),
mainPanel('main',
plotOutput("themap"),
tableOutput("table.output")
)
))
)
server = function(input, output, session) {
a <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
tbl <- read.csv(inFile$datapath, header=input$header) #, sep=input$sep, dec = input$dec)
return(tbl)
})
output$table.output <- renderTable({
a()
})
plotdata <- eventReactive(input$getHmap, {
a <- as.matrix(a()[-1])
row.names(a) <- a()$Name
a[is.na(a)] <- 0
a
})
output$themap = renderPlot({
pheatmap(plotdata())
})
}
shinyApp(ui, server)
我正在为 pheatmap 使用 R shiny,我想读取文件并绘制热图,但它没有用。可以读取csv文件,但是网页上看不到内容,无法绘制热图。
library(shiny)
library(pheatmap)
ui = fluidPage("Test",
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
tabPanel('map',
sidebarLayout(
sidebarPanel('side',
actionButton('getHmap', 'get heatmap')
),
mainPanel('main',
plotOutput("themap")
)
))
)
server = function(input, output, session) {
a <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
tbl <- read.csv(inFile$datapath, header=input$header, sep=input$sep, dec = input$dec)
return(tbl)
})
output$table.output <- renderTable({
a()
})
observeEvent(input$getHmap, {
row.names(a) <- a$Name
a <- a[,-1]
a[is.na(a)] <- 0
output$themap = renderPlot({
pheatmap(a)
})
})
}
shinyApp(ui, server)
```[![The original data I used][1]][1]
[1]: https://i.stack.imgur.com/S83cH.png
这可能是一个完整的工作示例。这似乎对我有用。进行了以下更改:
- 已将
tableOutput("table.output")
添加到ui
- 已将
read.csv
简化为inputs
,因为sep
和dec
缺失 - 创建了
plotData
函数作为eventReactive
使用操作按钮绘制热图 - 在为绘图添加行名之前将数据转换为矩阵
output$themap
调用plotData
函数
library(shiny)
library(pheatmap)
ui = fluidPage("Test",
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
tabPanel('map',
sidebarLayout(
sidebarPanel('side',
actionButton('getHmap', 'get heatmap')
),
mainPanel('main',
plotOutput("themap"),
tableOutput("table.output")
)
))
)
server = function(input, output, session) {
a <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
tbl <- read.csv(inFile$datapath, header=input$header) #, sep=input$sep, dec = input$dec)
return(tbl)
})
output$table.output <- renderTable({
a()
})
plotdata <- eventReactive(input$getHmap, {
a <- as.matrix(a()[-1])
row.names(a) <- a()$Name
a[is.na(a)] <- 0
a
})
output$themap = renderPlot({
pheatmap(plotdata())
})
}
shinyApp(ui, server)