【问题标题】:R Shiny, Creating reactive plot based on user uploaded fileR Shiny,根据用户上传的文件创建反应图
【发布时间】:2023-04-02 13:02:01
【问题描述】:

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

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

到目前为止我的代码

用户界面: 图书馆(闪亮)

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")

【问题讨论】:

  • 如果你使用mat &lt;- as.matrix(data(), row.numbers=1) 会怎样?
  • 我已经尝试了一些变体,但热图选项卡仍然空白。
  • 感谢它现在尝试生成热图,但出现错误警告“'x' must be a numeric matrix”。在选择上传文件之前和之后都会出现此错误,这就是为什么我认为一旦选择了数据集,我就必须使函数及其组件具有反应性以重现绘图。
  • @Niall 你能把你的问题编辑成你目前在你的代码中有什么供审查吗?我能够在没有任何错误的情况下进行复制。你在尝试什么数据文件?是否可以创建character 矩阵而不是numeric?您可能希望转换为没有行名的矩阵,然后再添加行名。

标签: r shiny heatmap bioinformatics reactive


【解决方案1】:

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

"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)

【讨论】:

  • 非常感谢@ben,它现在正在使用您对代码的调整运行。我认为“'x' must be a numeric matrix”警告是由于 mydataset 的性质造成的。
  • 很高兴听到 - 这是有道理的。
猜你喜欢
  • 2015-11-06
  • 2022-01-20
  • 2020-11-13
  • 2015-01-23
  • 1970-01-01
  • 2017-10-21
  • 2021-12-24
  • 2019-05-15
  • 2014-10-27
相关资源
最近更新 更多