【问题标题】:ggplot graph not displaying in shiny app when reading multiple files - cannot open connection读取多个文件时,ggplot图未显示在闪亮的应用程序中-无法打开连接
【发布时间】:2020-02-21 00:14:34
【问题描述】:

我编写了下面的代码,这是一个简单的应用程序,用于显示实验室设备生成的数据文件中的曲线。 我需要将选定文件夹中的所有文件合并,做一个简单的数据处理,然后显示叠加曲线。

当我尝试加载文件时,绘图区域出现cannot open the connection 错误。我还尝试在datatableoutput 中显示multiple_datatable,但我得到了完全相同的错误。


编辑:感谢您的评论,这是一个更简单的版本,只有所需的代码。


一些数据


文件 1

c("#FILE:                       ;Grofillex 1000°C 10K 5' air _ AW70ter.ngb-ss1", 
"#FORMAT:                     ;NETZSCH5                                     ", 
"#IDENTITY:                   ;STA2020-018                                  ", 
"#DECIMAL:                    ;POINT                                        ", 
"#SEPARATOR:                  ;SEMICOLON                                    ", 
"#MTYPE:                      ;DSC                                          ", 
"#INSTRUMENT:                 ;NETZSCH STA 449F1                            ", 
"#PROJECT:                    ;Grosfillex                                   ", 
"#DATE/TIME:                  ;29/01/2020 11:04:11                          ", 
"#CORR. FILE:                 ;                                             ", 
"#LABORATORY:                 ;LFM                                          ", 
"#OPERATOR:                   ;AM                                           ", 
"#REMARK:                     ;                                             ", 
"#SAMPLE:                     ;Grosfillex AW70 ter                            ", 
"#SAMPLE MASS /mg:            ;23.99                                        ", 
"#MATERIAL:                   ;                                             ", 
"#REFERENCE:                  ;Référence                                    ", 
"#REFERENCE MASS /mg:         ;0                                            ", 
"#TYPE OF CRUCIBLE:           ;DSC/TG pan Al2O3                             ", 
"#SAMPLE CRUCIBLE MASS /mg:   ;216.98                                       ", 
"#REFERENCE CRUCIBLE MASS /mg:;214                                          ", 
"#CORR. CODE:                 ;000                                          ", 
"#EXO:                        ;+1                                           ", 
"#RANGE:                      ;20....1000/-40.0....10.0K/min                ", 
"#SEGMENT:                    ;S1-4/4                                       ", 
"", "##Temp./°C;Time/min;DSC/(uV/mg);Mass/%;Gas Flow(purge1)/(ml/min);Gas Flow(protective)/(ml/min);Sensit./(uV/mW);Segment", 
"  20.72500;0.0000e+000;-1.7173e-003;100.00000;80.00000;20.00000;1.00000;1", 
"  20.74233;5.0000e-002;-1.6219e-003;100.27209;80.00000;20.00000;1.00000;1", 
"  20.74440;    0.10000;-1.5583e-003; 99.84077;80.00000;20.00000;1.00000;1", 
"  20.74786;    0.15000;-1.5583e-003; 99.67799;80.00000;20.00000;1.00000;1", 
"  20.74822;    0.20000;-1.5265e-003; 99.54200;80.00000;20.00000;1.00000;1", 
"  20.74882;    0.25000;-1.5583e-003; 99.43706;80.00000;20.00000;1.00000;1", 
"  20.74938;    0.30000;-1.4629e-003; 99.34389;80.00000;20.00000;1.00000;1", 
"  20.75073;    0.35000;-1.3357e-003; 99.27095;80.00000;20.00000;1.00000;1", 
"  20.75147;    0.40000;-1.4629e-003; 99.20383;80.00000;20.00000;1.00000;1", 
"  20.75376;    0.45000;-1.5901e-003; 99.15830;80.00000;20.00000;1.00000;1", 
"  20.75500;    0.50000;-1.6537e-003; 99.12828;80.00000;20.00000;1.00000;1", 
"  20.75571;    0.55000;-1.5901e-003; 99.11922;80.00000;20.00000;1.00000;1", 
"  20.75788;    0.60000;-1.6855e-003; 99.10786;80.00000;20.00000;1.00000;1", 
"  20.75924;    0.65000;-1.9717e-003; 99.08993;80.00000;20.00000;1.00000;1", 
"  20.76088;    0.70000;-2.1308e-003; 99.07180;80.00000;20.00000;1.00000;1", 
"  20.76088;    0.75000;-2.2580e-003; 99.05336;80.00000;20.00000;1.00000;1", 
"  20.76212;    0.80000;-2.0990e-003; 99.03449;80.00000;20.00000;1.00000;1", 
"  20.76312;    0.85000;-2.2262e-003; 99.01772;80.00000;20.00000;1.00000;1", 
"  20.76624;    0.90000;-2.2580e-003; 99.00156;80.00000;20.00000;1.00000;1", 
"  20.76641;    0.95000;-2.3534e-003; 98.98437;80.00000;20.00000;1.00000;1", 
"  20.76782;    1.00000;-2.4170e-003; 98.96738;80.00000;20.00000;1.00000;1", 
"  20.77018;    1.05000;-2.3534e-003; 98.94946;80.00000;20.00000;1.00000;1", 
"  20.77147;    1.10000;-2.4170e-003; 98.93206;80.00000;20.00000;1.00000;1"
)

文件 2

c("#FILE:                       ;Grofillex 1000°C 10K 5' air _ F1B.ngb-ss1", 
"#FORMAT:                     ;NETZSCH5                                 ", 
"#IDENTITY:                   ;STA2020-005                              ", 
"#DECIMAL:                    ;POINT                                    ", 
"#SEPARATOR:                  ;SEMICOLON                                ", 
"#MTYPE:                      ;DSC                                      ", 
"#INSTRUMENT:                 ;NETZSCH STA 449F1                        ", 
"#PROJECT:                    ;Grosfillex                               ", 
"#DATE/TIME:                  ;14/01/2020 16:33:22                      ", 
"#CORR. FILE:                 ;                                         ", 
"#LABORATORY:                 ;LFM                                      ", 
"#OPERATOR:                   ;AM                                       ", 
"#REMARK:                     ;                                         ", 
"#SAMPLE:                     ;Grosfillex F1B                           ", 
"#SAMPLE MASS /mg:            ;23.30                                    ", 
"#MATERIAL:                   ;                                         ", 
"#REFERENCE:                  ;Référence                                ", 
"#REFERENCE MASS /mg:         ;0                                        ", 
"#TYPE OF CRUCIBLE:           ;DSC/TG pan Al2O3                         ", 
"#SAMPLE CRUCIBLE MASS /mg:   ;216.31                                   ", 
"#REFERENCE CRUCIBLE MASS /mg:;214                                      ", 
"#CORR. CODE:                 ;000                                      ", 
"#EXO:                        ;+1                                       ", 
"#RANGE:                      ;20....1000/-40.0....10.0K/min            ", 
"#SEGMENT:                    ;S1-4/4                                   ", 
"", "##Temp./°C;Time/min;DSC/(uV/mg);Mass/%;Gas Flow(purge1)/(ml/min);Gas Flow(protective)/(ml/min);Sensit./(uV/mW);Segment", 
"  22.27000;0.0000e+000; 1.4735e-003;100.00000;80.00000;20.00000;1.00000;1", 
"  22.25833;5.0000e-002; 1.2770e-003;100.39850;80.00000;20.00000;1.00000;1", 
"  22.26100;    0.10000; 9.4958e-004; 99.79292;80.00000;20.00000;1.00000;1", 
"  22.26100;    0.15000; 8.8409e-004; 99.62661;80.00000;20.00000;1.00000;1", 
"  22.26267;    0.20000; 8.1860e-004; 99.50172;80.00000;20.00000;1.00000;1", 
"  22.26427;    0.25000; 6.8763e-004; 99.38573;80.00000;20.00000;1.00000;1", 
"  22.26492;    0.30000; 5.5665e-004; 99.29442;80.00000;20.00000;1.00000;1", 
"  22.26620;    0.35000; 4.9116e-004; 99.22543;80.00000;20.00000;1.00000;1", 
"  22.26606;    0.40000; 3.9293e-004; 99.14893;80.00000;20.00000;1.00000;1", 
"  22.26624;    0.45000; 2.6195e-004; 99.11513;80.00000;20.00000;1.00000;1", 
"  22.26700;    0.50000; 2.2921e-004; 99.07629;80.00000;20.00000;1.00000;1", 
"  22.26806;    0.55000;-6.5488e-005; 99.02597;80.00000;20.00000;1.00000;1", 
"  22.26859;    0.60000;-6.5488e-005; 99.01266;80.00000;20.00000;1.00000;1", 
"  22.26847;    0.65000;-3.2744e-005; 98.99828;80.00000;20.00000;1.00000;1", 
"  22.26924;    0.70000;-3.6019e-004; 98.98938;80.00000;20.00000;1.00000;1", 
"  22.27012;    0.75000;-2.9470e-004; 98.97006;80.00000;20.00000;1.00000;1", 
"  22.26982;    0.80000;-4.5842e-004; 98.94635;80.00000;20.00000;1.00000;1", 
"  22.27241;    0.85000;-7.2037e-004; 98.91567;80.00000;20.00000;1.00000;1", 
"  22.27235;    0.90000;-6.8763e-004; 98.89496;80.00000;20.00000;1.00000;1", 
"  22.27435;    0.95000;-8.8409e-004; 98.87800;80.00000;20.00000;1.00000;1", 
"  22.27600;    1.00000;-9.8233e-004; 98.86030;80.00000;20.00000;1.00000;1", 
"  22.27671;    1.05000;-9.1684e-004; 98.83444;80.00000;20.00000;1.00000;1", 
"  22.27759;    1.10000;-1.1788e-003; 98.81330;80.00000;20.00000;1.00000;1"
)

和代码


library(shiny)
library(xlsx)
library(readxl)
library(dplyr)
library(ggplot2)
library(reshape2)
library(viridis)
library(zoo)
library(lubridate)
library(shinyFiles)
library(htmltools)

ui <- fluidPage(
  navbarPage("ATG-DSC App",
             tabPanel("Multiple file comparison",
                      pageWithSidebar(
                      headerPanel(""),
                      sidebarPanel(width = 2,shinyDirButton('directory', 'Folder select', 'Please select a folder')),

                 mainPanel(fluidRow( 
                                column(12, 

                                      dataTableOutput("multiple_datatable"),
                                      uiOutput("plot.ui2"))))))))


server = function(input, output, session){

  volumes <- getVolumes()
  shinyDirChoose(input, 'directory', roots=volumes, session=session)
  path1 <- reactive({
    return(print(parseDirPath(volumes, input$directory)))
  })

filenames <- eventReactive(input$directory, {
  file.names <- dir(path1(), pattern =".txt")
  return(file.names)
  })

#data reading
dataset_multiple <- eventReactive(input$directory, {
  out.file<-""
  file.names <- dir(path1(), pattern =".txt")
  for(i in 1:length(file.names)){
    lines <- readLines(file.names[i])
    column_names <- gsub("#",'',unlist(strsplit(lines[27],";")))
    header <- lines[1:26]
    lines <- lines[seq(27,length(lines),5)]
    col <- length(column_names)
    data <- as.data.frame(matrix(unlist(strsplit(lines,";")),ncol=col,byrow=T))
    data <- data[2:nrow(data),]
    colnames(data) <- column_names
    for (i in 1:ncol(data)){
      data[,i] <- as.numeric(gsub(",",".",as.character(data[,i])))
    }
    data$sample <- unlist(strsplit(header,";"))[28]
    out.file <- rbind(out.file, data)
    for (i in 1:(ncol(out.file)-1)){
      out.file[,i] <- as.numeric(gsub(",",".",as.character(out.file[,i])))
    }
  }

  out.file$`Sensit./(uV/mW)` <- NULL 
  out.file$`Time/min` <- NULL
  out.file <- out.file[complete.cases(out.file),]
  out.file <- out.file[out.file$Segment==2,]
  out.file$Segment <- NULL
  out.file$`Gas Flow(protective)/(ml/min)` <- NULL
  out.file$`Gas Flow(purge1)/(ml/min)` <- NULL
  names(out.file) <- c("Temp", "DSC", "Mass", "Sample")
  return(out.files)
  })

output$multiple_plot <- renderPlot({

  multiple_data <- dataset_multiple()
  multiple_data_melt <- melt(multiple_data, id=c("Temp","Sample"))
  col_pal <- color_palette()

  g <- ggplot(multiple_data_melt,aes(multiple_data_melt$Temp,multiple_data_melt$value, group=sample, color=sample))
  + geom_point()
  + facet_grid(variable~.,scales = "free_y")

  print(g)
})


  output$multiple_datatable <- eventReactive(input$directory, {
    multiple_data <- dataset_multiple()
    multiple_data_melt <- melt(multiple_data, id=c("Temp","Sample"))
    return(head(multiple_data_melt))
  })


  output$plot.ui2 <- renderUI({
    plotOutput("multiple_plot")
  })
}

shinyApp(ui = ui, server = server, options = list(launch.browser=TRUE))

有人可以帮忙吗?

【问题讨论】:

  • 你的应用太大了。尝试制作一个重现问题的 minimal 示例。否则很难提供帮助。
  • 请参阅here,了解有关如何制作最小可重现示例的信息。这样你就可以帮助别人帮助你!
  • @dario 我包含了最少的代码和一些数据。这些文件是 txt 文件,因此我无法将它们嵌入为数据表,因为它们在前 26 行中也有元数据标题(因此是我的代码)
  • @StéphaneLaurent 这里是最小的例子
  • @Boidot 你能告诉我如何加载你的示例数据file1file2 吗?或者 is 数据是单个字符串???

标签: r ggplot2 shiny


【解决方案1】:

读取前 26 行有 2 列,然后是多列的文件时出现问题。

我使用count.fields() 函数来获取每个文件的最大行数,然后我可以读取单个文件

no_col <- count.fields(file.path)
data <- read.table(file.path,sep=";",fill=TRUE,col.names=1:no_col)

所以读取数据的全局代码:

dataset_multiple <- reactive({
files <- filenames()
folder <- path1()
out.file <- ""



for (i in (1:length(files))){
    file.path <- paste(folder, files[i], sep='/')
    column_names <- readLines(file.path)[27]
    column_names <- gsub("#",'',unlist(strsplit(column_names,";")))
    no_col <- count.fields(file.path,sep=";")
    data <- read.table(file.path,sep=";",fill=TRUE,col.names=1:no_col)
    data <- data[seq(1,nrow(data),5),]
    colnames(data) <- column_names
    for (i in 1:ncol(data)){
      data[,i] <- as.numeric(gsub(",",".",as.character(data[,i])))
    }

    echantillon <- unlist(strsplit(readLines(file.path)[14],";"))[2]

    out.file <- rbind(out.file, data)
    for (i in 1:(ncol(out.file)-1)){
        out.file[,i] <- as.numeric(gsub(",",".",as.character(out.file[,i])))
      }
    }

    out.file$`Sensit./(uV/mW)` <- NULL
    out.file$`Time/min` <- NULL
    out.file <- out.file[complete.cases(out.file),]
    out.file <- out.file[out.file$Segment==2,]
    out.file$Segment <- NULL
    out.file$`Gas Flow(protective)/(ml/min)` <- NULL
    out.file$`Gas Flow(purge1)/(ml/min)` <- NULL
    names(out.file) <- c("Temp", "DSC", "Mass")

  return(out.file)
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-08
    • 2017-12-20
    • 2019-07-30
    • 2016-12-20
    • 2017-07-10
    • 2016-10-15
    • 1970-01-01
    • 2019-07-09
    相关资源
    最近更新 更多