我在使用fileInput在R Shiny中遇到了困难 . 我能够成功上传多个.txt或.csv文件,但我希望我的应用程序能够上传一个名为“.fcs”文件的文件类型 . 为了读取这样的“.fcs”文件,我使用了bioConductor包下的库flowCore . 在这个flowcore包中,有一个名为read.FCS的函数,我经常用它来读入“.fcs”文件到R.

read.FCS()函数的第一个参数是文件位置的字符串 . 这就是我的问题所在:因为函数需要将文件路径放在charcater字符串中,fileInput()生成的数据路径中的反斜杠不会与read.FCS所期望的字符串相互作用(例如,当我打印时)从fileInput获取的数据路径,Shiny返回类似于:... \ AppData \ Local \ Temp \ Rtm ... PdV / 99 ... 42/0 - 注意前向和反斜杠) .

我的问题是:有没有办法修改数据路径不包含反斜杠,以便read.FCS()函数可以正常工作?

为了说明这个问题,我将在下面附加我的脚本的简化版本以进行调试,以及生成.txt文件和.fcs文件的方法(必须首先安装软件包flowCore) . 您将能够将这两个读入R,但.fcs文件将无法读入Shiny应用程序 . 启动闪亮后,当您选择“.txt”文件类型选项并上传示例.txt文件(在下面生成)时,Shiny会对此文件进行处理 . 当您重新启动应用程序并选择“.fcs”文件类型选项并上传示例.fcs文件(在下面生成)时,该文件无法正确上载 .

起初我认为这是Shiny在上传后以某种方式损坏FCS文件 . 但这种情况并非如此 . 作为prrof,在mainTab区域中,您将看到打印的文件路径 . 如果您复制此文件路径(当您尝试加载fcs文件时),将其放在引号中并将其放入read.FCS(在此处插入带引号的复制文件路径),您将收到错误 . 另一方面,如果在read.FCS命令中将所有“\”替换为“/”并再次尝试,则R将成功读取上传到Shiny的文件 . 因此,我被认为如果我能以某种方式在文件路径中用“/”替换“\”,我将能够读取我的FCS文件 .

非常感谢您的帮助 - 我真的很感激:下面附带示例代码

install.packages(flowCore)
library(flowCore)

x = rnorm(n=20, mean=5, sd=10)
y = rnorm(n=20, mean=5, sd=10)
z = rnorm(n=20, mean=5, sd=10)
data<-data.frame(as.integer(x),as.integer(y),as.integer(z))
my_save_path<-"C:/Users/drew/Desktop/"
write(as.matrix(data), paste(my_save_path, "sample_textfile.txt", sep=""), ncolumns=3, sep="\t")
df.export<-new("flowFrame", as.matrix(data))
write.FCS(df.export, paste(my_save_path, "sample_fcsfile.fcs", sep=""))
#write.fcs is experimental, but the problem persists for raw .fcs files from other sources other than this export.

#####Verify that these files are real and can be looked at in r

txt.real<-read.delim(paste(my_save_path,"sample_textfile.txt", sep=""))
txt.real #yup, reading into R works

fcs.real<-read.FCS( paste(my_save_path, "sample_fcsfile.fcs", sep=""), transform=FALSE)
fcs.real<-exprs(fcs.real)
fcs.real #yup, this is real too

用户界面

shinyUI(navbarPage("App",
tabPanel("Overview",
                            sidebarLayout(
                              sidebarPanel("Load in Data",
                                           selectInput("filetype", label = h3("File Type"), 
                                                       choices=list(".fcs file", ".txt file"), selected=".txt file"),
                                           fileInput("files", h3("File(s) Input"))),
                              mainPanel(
                                tableOutput("table1"),
                                textOutput("text")                                
                              )))
))

服务器

library(flowCore)

shinyServer(function(input, output) {

  fileName<-reactive({
    inFile <- input$files
    inFile$datapath
  })

  dataInput<-reactive({

    if(input$filetype==".fcs file"){
      library(flowCore)
      FCS<-read.FCS(fileName(), transform=FALSE)
      exprs(FCS)

    }

    if(input$filetype==".txt file") {
      TXT<-read.delim(fileName())
      TXT
    }
 })

  ####Heading the Table (debug)  
  output$table1<-renderTable({
    if (is.null(input$files))
      return(NULL)

    head(dataInput())

  })

  #Seeing the file path
  output$text<-renderText({
    x<-fileName()
    x
  })

}
)