首页 > 解决方案 > R Shiny,根据用户上传的文件创建反应图

问题描述

我正在开发一个应用程序(除其他外) 1. 允许用户上传 csv 数据集(这似乎可行) 2. 根据用户在单独的选项卡中选择的文件绘制热图

该应用程序似乎正在运行,但是我无法获取热图来绘制用户选择的数据,我认为它需要以某种方式进行响应,但我无法调用上传的文件。

到目前为止我的代码

UI:图书馆(闪亮)

shinyUI(fluidPage(
  tabsetPanel(
    tabPanel("File Input",
    sidebarLayout(
    sidebarPanel(
      fileInput("file","Upload the File"),
      tags$hr(),
      h5(helpText("Select the read.table parameters below")),
      checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
      checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
      br(),
      radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
    ),
    mainPanel(
      uiOutput("tb")

    )
    )),
  tabPanel("Heatmap",
    plotOutput('heatmap', width = "100%", height = "500px")

))
))

服务器

library(shiny)
shinyServer(function(input,output,session){

  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)

  })


  output$filedf <- renderTable({
    if(is.null(data())){return ()}
    input$file
  })


  output$sum <- renderTable({
    if(is.null(data())){return ()}
    summary(data())

  })

  output$table <- renderTable({
    if(is.null(data())){return ()}
    data()
  })


  output$tb <- renderUI({
    if(is.null(data()))
      h5("Powered by copious amounts of coffee", tags$img(src='coffee.png', heigth=200, width=200))
    else
      tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
  })
 mps<-reactive({
if(is.null(file_to_read)){
  return()


   }
plots<-reactive({
   output$heatmap <- renderPlot({ heatmap(data, main = "heatmap")
       })})
    })})

有没有办法将数据命名为要在热图中使用的对象,例如

mat<-as.matrix(user uploaded file, row.numbers=1)
output$heatmap <- renderPlot({ heatmap(mat, main = "heatmap")

标签: rshinyheatmapbioinformaticsreactive

解决方案


@Niall - 这是我必须创建的热图。希望这会有所帮助。我mtcars在 .csv 文件中对其进行了测试:

"mpg","cyl","disp","hp","drat","wt","qsec","vs","am","gear","carb"
"Mazda RX4",21,6,160,110,3.9,2.62,16.46,0,1,4,4
"Mazda RX4 Wag",21,6,160,110,3.9,2.875,17.02,0,1,4,4
"Datsun 710",22.8,4,108,93,3.85,2.32,18.61,1,1,4,1
...

header闪亮的应用程序中选择选项。

library(shiny)

ui <- fluidPage(
  tabsetPanel(
    tabPanel("File Input",
             sidebarLayout(
               sidebarPanel(
                 fileInput("file","Upload the File"),
                 tags$hr(),
                 h5(helpText("Select the read.table parameters below")),
                 checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
                 checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
                 br(),
                 radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
               ),
               mainPanel(
                 uiOutput("tb")

               )
             )),
    tabPanel("Heatmap",
             plotOutput('heatmap', width = "100%", height = "500px")
    ))
)

server <- function(input,output,session){

  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
  })

  output$filedf <- renderTable({
    if(is.null(data())){return ()}
    input$file
  })

  output$sum <- renderTable({
    if(is.null(data())){return ()}
    summary(data())

  })

  output$table <- renderTable({
    if(is.null(data())){return ()}
    data()
  })

  output$tb <- renderUI({
    if(is.null(data()))
      h5("Powered by copious amounts of coffee", tags$img(src='coffee.png', heigth=200, width=200))
    else
      tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
  })

  output$heatmap <- renderPlot({ 
    if (is.null(data())) {
      return()
    } 
    heatmap(as.matrix(data()), main = "heatmap")
  })
}

shinyApp(ui = ui, server = server)

推荐阅读