如何在 R shiny 中获得 PCA 结果

How to get PCA results in R shiny

在 R shiny 中开发应用程序,我想要输入数据的 PCA 结果作为应用程序的一部分。我很困惑如何从数据库中删除 NA 并将整个数据转换为数字和形式以及数据的 PCA。 我附上了我用于 server.R 和 ui.R.

的代码

我有 运行 这段代码并得到错误:enter image description here 错误是什么意思? shniy 的代码:

library(factoextra)
library(dplyr)
library(tidyverse)
library(FactoMineR)
options(shiny.maxRequestSize = 20*1024^2)

shinyServer(function(input,output){

  # This reactive function will take the inputs from UI.R and use them for read.table() to read the data from the file. It returns the dataset in the form of a dataframe.
  # file$datapath -> gives the path of the file
  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read.csv(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)

  })
# this reactive output contains the summary of the dataset and display the summary in table format
 output$filedf <- renderTable({
   if(is.null(data())){return ()}
   input$file
 })

 # this reactive output contains the summary of the dataset and display the summary in table format
 output$sub <- renderTable({
   if(is.null(data())){return ()}
   subset(data(),select=c(CNV,Clinical,Genes))

 })
 p<-sub

 # This reactive output contains the dataset and display the dataset in table format
 output$table <- renderTable({
   if(is.null(data())){return ()}
   data()
 })
 ##pca
 output$pcasub<-renderTable({
   if(is.null(data())){return ()}

 PCA(p,scale.unit = TRUE)
 })
 # the following renderUI is used to dynamically generate the tabsets when the file is loaded. Until the file is loaded, app will not show the tabset.
 output$tb <- renderUI({
   if(is.null(data()))
     h5("PRED")
   else
     tabsetPanel(tabPanel("PCA", tableOutput("pcasub")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("pcasub")))
 })
})

library(shiny)
shinyUI(fluidPage(
   titlePanel("File Input"),
   sidebarLayout(
       sidebarPanel(
         #  fileInput("file","Upload clinical data file"), # fileinput() function is used to get the file upload contorl option
           fileInput("file","Upload miRNA raw read count file"), # fileinput() function is used to get the file upload contorl option

           helpText("Default max. file size is 5MB"),
           tags$hr(),
           h5(helpText("Select the read.csv parameters below")),
           checkboxInput(inputId = 'header', label = 'Header', value = TRUE),
           checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
           br(),
           radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
       ),
       mainPanel(
           uiOutput("tb")

           # use below code if you want the tabset programming in the main panel. If so, then tabset will appear when the app loads for the first time.
           #       tabsetPanel(tabPanel("Summary", verbatimTextOutput("sum")),
           #                   tabPanel("Data", tableOutput("table")))
       )

   )
))

我认为有几件事可能对这里有所帮助。

首先,您可以为子集数据使用单独的 reactive 表达式。这可以是 used/shared 其他选项卡结果。

此外,您的 tabsetPanel 中的 pcasub 似乎有两个相同的 tableOutput。会让每一个都独一无二。我将摘要更改为 pcasummary.

PCA 结果不会是数据框。显示结果的一种简单方法是使用 verbatimTextOutputrenderPrint。这适用于 pcasubpcasummary。根据您的需要,您可能希望 PCA() 在单独的反应函数中,或与 sub_data.

结合使用

看看这是否更接近您的需要。

server <- function(input,output){

  # This reactive function will take the inputs from UI.R and use them for read.table() to read the data from the file. It returns the dataset in the form of a dataframe.
  # file$datapath -> gives the path of the file
  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read.csv(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
  })

  sub_data <- reactive({
    subset(data(),select=c(CNV,Clinical,Genes))
  })

  # this reactive output contains the summary of the dataset and display the summary in table format
  output$filedf <- renderTable({
    if(is.null(data())){return ()}
    input$file
  })

  # this reactive output contains the summary of the dataset and display the summary in table format
  output$sub <- renderTable({
    if(is.null(data())){return ()}
    sub_data()
  })

  # This reactive output contains the dataset and display the dataset in table format
  output$table <- renderTable({
    if(is.null(data())){return ()}
    data()
  })

  ##pca
  output$pcasub<-renderPrint({
    if(is.null(data())){return ()}
    PCA(sub_data(),scale.unit = TRUE)
  })

  ##pca summary
  output$pcasummary<-renderPrint({
    if(is.null(data())){return ()}
    summary(PCA(sub_data(),scale.unit = TRUE))
  })

  # the following renderUI is used to dynamically generate the tabsets when the file is loaded. Until the file is loaded, app will not show the tabset.
  output$tb <- renderUI({
    if(is.null(data()))
      h5("PRED")
    else
      tabsetPanel(tabPanel("PCA", verbatimTextOutput("pcasub")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", verbatimTextOutput("pcasummary")))
  })
}