R 跳过上载数据集末尾的某些行

R 跳过上载数据集末尾的某些行,r,shiny,R,Shiny,我正在我闪亮的应用程序中上传和读取一个文本文件。以下是如何在Shining应用程序中阅读: data <- reactive({ req(input$file) df <- read.table(file=input$file$datapath[input$file$name==input$Select], skip = 15, sep=input$sep, header = input$header, stringsAsFactors = input$stringA

我正在我闪亮的应用程序中上传和读取一个文本文件。以下是如何在Shining应用程序中阅读:

data <- reactive({
    req(input$file)
    df <- read.table(file=input$file$datapath[input$file$name==input$Select], skip = 15, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
    updateSelectInput(session, inputId = 'xcol', label = 'X Variable',
                      choices = names(df), selected = names(df)[1])
    updateSelectInput(session, inputId = 'ycol', label = 'Y Variable',
                      choices = names(df), selected = names(df)[2])
    return(df)
  })

数据您可以使用带有负指数的
头部

df <- data.frame(row = 1:20)
df
head(df,-15)

  row
1   1
2   2
3   3
4   4
5   5

df您可以使用带有负指数的
head

df <- data.frame(row = 1:20)
df
head(df,-15)

  row
1   1
2   2
3   3
4   4
5   5

df如果您知道文件的行数,那么
data.table::fread()
有一个
nrows=
参数,因此可以执行以下操作:

data.table::fread("file", nrows = 100) # file has 115 rows
更麻烦的是,当您不知道行数,但希望最后15行消失时,一种可能有点低效的方法是:

tmp <- readLines(file)
nr <- length(tmp)
data.table::fread("file", nrows = nr-15)

tmp如果您知道文件的行数,那么
data.table::fread()
有一个
nrows=
参数,因此可以:

data.table::fread("file", nrows = 100) # file has 115 rows
更麻烦的是,当您不知道行数,但希望最后15行消失时,一种可能有点低效的方法是:

tmp <- readLines(file)
nr <- length(tmp)
data.table::fread("file", nrows = nr-15)

tmp如果知道有多少行,可以使用附加参数
nrows=rows-15
如果知道有多少行,可以使用附加参数
nrows=rows-15
Ben-Toh,用户并不总是知道文件的行数,因为它们在每个文件中都是可变的。它允许您在读取文件之前检查文件的长度。Ben Toh,用户并不总是知道文件的行数,因为它们在每个文件中都是可变的。是的,这就是第二个解决方案的目的。它允许您在读入文件之前检查文件的长度。