How to assign output of cat to an object?
Instead of cat
ing to a file, why not use the paste
command to generate a string instead?
> paste(test, collapse="\n")
[1] "V 1\nx\n1 2 3\ny\n3 5 8\nV 2\nx\ny\nV 3\ny\n7 2 1\nV 4\nx\n9 3 7\ny"
Now instead of doing a cat
then readlines
you can just pass this string directly into strsplit
.
How do I assign the output of cat to an object in a list?
Capture the output as out
, insert ABC
and perform the replacement giving character vector v
, collapse that to a single newline separated string giving s
. Now assign v
or s
to the list component as in the question depending on what you want. (Can omit the line creating s
if v
is assigned.)
out <- capture.output(summary(aov(savings ~ single, data = credit)))
v <- c("ABC", sub("Pr(>F)", "p-value", out, fixed = TRUE), "\n") # char vec
s <- paste(v, collapse = "\n") # single string
The result of cat(s)
is:
ABC
Df Sum Sq Mean Sq F value p-value
single 1 94 93.72 3.415 0.0652 .
Residuals 498 13668 27.45
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
custom class with print method
I am not sure that the added complexity is worth it but if you really want to create an object which displays itself nicely you can create a new class with a custom print method:
print.selfcat <- cat
obj <- structure(s, class = "selfcat")
obj # result is same as for cat(s)
summary.aov subclass
Another possibility is to create a subclass of summary.aov
class, summary.aov2
which adds a name
attribute and a print
method to wrap up everything above together:
# convert summary.aov class to summary.aov2 class
as.summary.aov2 <- function(x, name = "", ...) {
structure(x, name = name, class = c("summary.aov2", "summary.aov"))
}
print.summary.aov2 <- function(x, ...) {
out <- capture.output(structure(x, class = "summary.aov"))
v <- c(attr(x, "name"), sub("Pr(>F)", "p-value", out, fixed = TRUE), "\n")
s <- paste(v, collapse = "\n")
cat(s, ...)
}
# test
library(fpp)
modsum <- summary(aov(savings ~ single, data = credit))
modsum2 <- as.summary.aov2(modsum, name = "ABC") # create new object w name
modsum2
giving:
ABC
Df Sum Sq Mean Sq F value p-value
single 1 94 93.72 3.415 0.0652 .
Residuals 498 13668 27.45
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
How to assign a cat() value to an object in r
cat
doesn't have a return
value. It just print
. We could write the output to a file by specifying the file
argument
cat(dQuote(paste0("Equal = ", paste("(", vars, ", Slope[0])",
collapse=",\n ", sep=""), ";"), FALSE),
file = 'file.txt')
The dQuote
output can be assigned and not the cat
wrapped on it
aa <- dQuote(paste0("Equal = ", paste("(", vars, ", Slope[0])",
collapse=",\n ", sep=""), ";"), FALSE)
save extracted (using cat) text in a variable in R
You need to use capture.output
in this case.
var2 <- capture.output(cat(sub('.*"(.*)".*', "\\1", var1)))
var2
Look at this question.
Getting the output of a cat command into an array
readarray array < <(awk '/swap/{print $1}' /proc/swaps)
How to assign the output of dput() into an object or insert the double quote and then comma after every words?
write.table to clipboard works.
write.table(t(as.vector(x)), "clipboard", row.names = FALSE, col.names = FALSE, sep = ",")
then ctrl+V to script returns
"ACCN-NJ-A55O-01A-11D-A25L-08","ACCN-NJ-A55O-11D-11D-A25L-08","ACCN-05-4249-01A-01D-1105-08","ACCN-S2-AA1A-15C-12D-A397-08"
I'm not sure if this is what you wanted.
bash : cat commands output only if it success
Perhaps capture the output into a variable, and echo the variable into the file if the exit status is zero:
output=$(command) && echo "$output" > file
Testing
$ out=$(bash -c 'echo good output') && echo "$out" > file
$ cat file
good output
$ out=$(bash -c 'echo bad output; exit 1') && echo "$out" > file
$ cat file
good output
Related Topics
Rank a Vector Based on Order and Replace Ties with Their Average
Change Level of Multiple Factor Variables
Simple Manual Rmarkdown Tables That Look Good in HTML, PDF and Docx
Ggplot Aes_String Does Not Work Inside a Function
Azure Put Blob Authentication Fails in R
Ggplot2: Issues with Dual Y-Axes and Loess Smoothing
R Xml - Combining Parent and Child Nodes(W Same Name) into Data Frame
R Column Check If Contains Value from Another Column
Displaying Data in the Chart Based on Plotly_Click in R Shiny
Compute All Fixed Window Averages with Dplyr and Rcpproll
Replacing the Duplicate Values Except 1 Row in R Dataframe
How to Get Rows, by Group, of Data Frame with Earliest Timestamp
Is R Superstitious Regarding Posixct Data Type
Side by Side Histograms in the Same Graph in R