读取多个文件时,ggplot图形未显示在闪亮的应用程序中-无法打开连接

布伊多

我编写以下代码,该代码是一个简单的应用程序,用于显示实验室设备生成的数据文件中的曲线。我需要将所有文件合并到选定的文件夹中,进行简单的数据处理,然后显示超高曲线。

我得到一个cannot open the connection在绘图区域错误,当我尝试加载的文件。我也尝试在中显示multiple_datatabledatatableoutput但得到完全相同的错误。


编辑:感谢您的评论,这是仅包含所需代码的简单版本。


一些数据


文件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))

有人可以帮忙吗?

布伊多

读取文件时出现问题,该文件的前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)
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Boot在连接Oracle数据库时出现错误:无法打开JDBC连接以执行DDL

文件错误(文件,“ rt”):无法打开连接-无法打开文件“ specdata”访问被拒绝

有时无法使用Spark打开本机连接

错误:无法在R Shiny中打开连接

无法打开连接到Firebase的Heroku应用程序

excel应用程序在打开时未显示在任务管理器中

在文件中获取错误(文件,“ rt”):无法打开连接

R:install.packages中的错误:无法打开连接

在ggplot2中制作的散点图在闪亮的应用程序中无法正确显示

文件错误(con,“ wb”):无法打开连接

R中的错误:无法打开与X11的连接

无法在Spring Web应用程序中打开属性文件

在Shiny Server中无法打开与Google Spreadsheets的连接

重新连接屏幕会话后无法打开显示

无法在Java中重新打开MySQL连接

无法在应用程序中打开.pdf文件

CDbConnection无法打开数据库连接:无法打开数据库文件

无法启动任何应用程序-无法打开与X的连接

连接无法打开数据库连接:找不到驱动程序

将新数据追加到文件中时无法重新打开文件的连接

Hibernate在多线程应用程序中随机抛出“ org.hibernate.exception.JDBCConnectionException:无法打开连接”

readLines()网络抓取无法在R中打开连接

保存错误:gzfile(file,“ wb”)中的错误:无法打开连接

Spring boot - 应用程序无法启动无法打开架构管理目标的 JDBC 连接

Shiny Server 无法打开与任何闪亮应用程序的连接

lapply 在 R 中,无法打开连接?

运行闪亮的应用程序时出错“无法打开连接”

无法打开文件或连接:尝试从计算机读取 Excel 时出现“rb”错误

azure.servicebus.common.errors.ServiceBusConnectionError:无法打开处理程序:无法在连接上打开身份验证会话