Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6011957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:20:03+00:00 2026-05-23T02:20:03+00:00

getCommentary=function(){ Commentary=readLines(file(C:\\Commentary\\com.txt)) return(Commentary) close(readLines) closeAllConnections() } I have no idea what is wrong with

  • 0
getCommentary=function(){
    Commentary=readLines(file("C:\\Commentary\\com.txt"))
    return(Commentary)
    close(readLines)
    closeAllConnections()
}

I have no idea what is wrong with this function. When I run this in R, it keeps giving me the following warning:

Warning message:
closing unused connection 5 ("C:\\Commentary\\com.txt") 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:20:04+00:00Added an answer on May 23, 2026 at 2:20 am

    readLines() is a function, you don’t close() it. You want to close the connection opened by the file() function. Also, you are return()ing before you close any connections. As far as the function is concerned, the lines after the return() statement don’t exist.

    One option is to save the object returned by the file() call, as you shouldn’t be closing all connections only those your function opens. Here is a non-function version to illustrate the idea:

    R> cat("foobar\n", file = "foo.txt")
    R> con <- file("foo.txt")
    R> out <- readLines(con)
    R> out
    [1] "foobar"
    R> close(con)
    

    To write your function, however, I would probably take a slightly different tack:

    getCommentary <- function(filepath) {
        con <- file(filepath)
        on.exit(close(con))
        Commentary <-readLines(con)
        Commentary
    }
    

    Which is used as follows, with the text file created above as an example file to read from

    R> getCommentary("foo.txt")
    [1] "foobar"
    

    I used on.exit() so that once con is created, if the function terminates, for whatever reason, the connection will be closed. If you left this just to a close(con) statement just before the last line, e.g.:

        Commentary <-readLines(con)
        close(con)
        Commentary
    }
    

    the function could fail on the readLines() call and terminate, so the connection would not be closed. on.exit() would arrange for the connection to be closed, even if the function terminates early.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC application and I'm trying to load the following html
I am a newbie at performance tuning applications and figuring out the workings of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.