writeToFile: writeToFile

Description Usage Arguments Value Author(s) Examples

View source: R/pscript1.R

Description

Takes in a filename in the form of a string of a file in existence or one that can be created. User can choose to append to overwrite an existing file or they can create a new file. Asks for user's name and opens a file writing the name entered and the Sys.time. Then asks for user to type more or enter 'q' to quit. File can be reopened and more can be added by calling the function again.

Usage

1
writeToFile(filename)

Arguments

filename

The name of the existing file or the name that will be used for the new file

Value

No return type creates a txt file in the current project named whatever the user enters as the filename parameter or modifies an existing file by the name of filename

Author(s)

Matt Heffernan, University of Illinois at Chicago

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
writeToFile("output.txt")
Allows user to create and write to a file called output.txt or they can append to or overwrite the file output.txt if it already exists.
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
writeToFile <- function(filename)
{

  if(file.exists(filename))
  {
    answer <- readline("FILE ALREADY EXISTS. Would you like to 'a' (append), 'o' (overwrite), or 'q' (quit) : ")
    if(answer != 'a' && answer !='o')
    {
      return(cat("QUITTING"))
    }
    else if(answer == 'o')
    {
      a = FALSE
    }
    else
    {
      a = TRUE
    }
  }
  else
  {
    answer <- readline("NEW FILE NAME.  Do you wish to create a new file 'y' (yes) or 'n' (no) : ")
    if(answer != 'y')
    {
      return(cat("QUITTING"))
    }
    else
    {
      cat("NEW FILE CREATED: ", filename, "\n")
      a = TRUE
    }
  }
  name <- readline("Enter your name : ")
  cat(name, file=filename, sep = "\n",append=a)
  cat(format(Sys.time(),usetz=TRUE), file=filename, sep = "\n", append=TRUE)
  line <- readline("Write something ('q' to quit) : ")
  while(line != "q")
  {
    cat(line, file=filename, sep = "\n", append=TRUE)
    line <- readline("Write more ('q' to quit) : ")
  }

  return(cat("DONE"))

}

mheffe3/MyFirstPackage documentation built on Dec. 21, 2021, 5:52 p.m.