Zapis wierszy tekstu do pliku w R

W języku skryptowym R, jak pisać wiersze tekstu, np. następujące dwie linie

Hello
World

Do pliku o nazwie " output.txt"?

 270
Author: amarillion, 2010-03-18

10 answers

fileConn<-file("output.txt")
writeLines(c("Hello","World"), fileConn)
close(fileConn)
 330
Author: Mark,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-18 13:54:44

Właściwie ty możesz zrobić to za pomocą sink():

sink("outfile.txt")
cat("hello")
cat("\n")
cat("world")
sink()

Stąd zrobić:

file.show("outfile.txt")
# hello
# world
 128
Author: aL3xa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-06 18:37:26

Użyłbym komendy cat() jak w tym przykładzie:

> cat("Hello",file="outfile.txt",sep="\n")
> cat("World",file="outfile.txt",append=TRUE)

Możesz następnie wyświetlić wyniki za pomocą r z

> file.show("outfile.txt")
hello
world
 96
Author: ps1,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-09-20 10:11:23

A co z prostym writeLines()?

txt <- "Hallo\nWorld"
writeLines(txt, "outfile.txt")

Lub

txt <- c("Hallo", "World")
writeLines(txt, "outfile.txt")
 39
Author: petermeissner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-31 13:34:29

1.Użycie argumentu file W cat.

 cat("Hello World", file="filename")

2.Użyj funkcji sink, Aby przekierować wszystkie dane wyjściowe z print i cat do pliku.

 sink("filename")                     # Begin writing output to file
 print("Hello World")
 sink()                               # Resume writing output to console

Uwaga : funkcja print nie może przekierować swojego wyjścia, ale funkcja sink może Wymuś wszystkie dane wyjściowe do pliku.

3.Wykonywanie połączenia z plikiem i zapisanie .

con <- file("filename", "w")
cat("Hello World", file=con)
close(con)
 26
Author: Prateek Joshi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-25 01:40:33

Możesz to zrobić w jednym oświadczeniu

cat("hello","world",file="output.txt",sep="\n",append=TRUE)
 11
Author: Charan Raj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-08 16:46:42

Proponuję:

writeLines(c("Hello","World"), "output.txt")

Jest krótsza i bardziej bezpośrednia niż obecna akceptowana odpowiedź. Nie jest konieczne:

fileConn<-file("output.txt")
# writeLines command using fileConn connection
close(fileConn)

Ponieważ dokumentacja dla writeLines() mówi:

Jeśli con jest ciągiem znaków, funkcja wywołuje file, aby uzyskać połączenie plików, które jest otwierane na czas trwania funkcji sprawdzam.

# default settings for writeLines(): sep = "\n", useBytes = FALSE
# so: sep = "" would join all together e.g.
 3
Author: Gwang-Jin Kim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-21 09:20:36

Aby dopełnić możliwości, możesz użyć writeLines() z sink(), Jeśli chcesz:

> sink("tempsink", type="output")
> writeLines("Hello\nWorld")
> sink()
> file.show("tempsink", delete.file=TRUE)
Hello
World

Dla mnie to zawsze wydaje się najbardziej intuicyjne w użyciu print(), ale jeśli to zrobisz, wyjście nie będzie tym, czego chcesz:

...
> print("Hello\nWorld")
...
[1] "Hello\nWorld"
 2
Author: gung,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-28 23:29:29

Na podstawie najlepsza odpowiedź :

file <- file("test.txt")
writeLines(yourObject, file)
close(file)

Zauważ, że yourObject musi być w formacie string; użyj as.character() do konwersji, jeśli potrzebujesz.

Ale to za dużo typowania dla każdej próby zapisu. Stwórzmy fragment w RStudio.

W opcjach globalnych > > Kod > > urywek wpisz:

snippet wfile
    file <- file(${1:filename})
    writeLines(${2:yourObject}, file)
    close(file)

Następnie, podczas kodowania, Wpisz wfile i naciśnij Tab.

 1
Author: Luis Martins,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-01 19:22:21

The ugly system option

ptf <- function (txtToPrint,outFile){system(paste(paste(paste("echo '",cat(txtToPrint),sep = "",collapse = NULL),"'>",sep = "",collapse = NULL),outFile))}
#Prints txtToPrint to outFile in cwd. #!/bin/bash echo txtToPrint > outFile
 0
Author: kpie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-21 21:53:16