Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save the result of str() as a string in R?

Tags:

r

shiny

I am learning Shiny by developing a shiny app which creates a report on chosen csv file. I was able to output dataFrame head and summary. However, I stuck with saving a str() representation of df, as str() function returns NULL, printing stuff to console instead.

Is there any workaround to save str() to variable for the purpose of representing it in the shiny app?

like image 709
Philipp_Kats Avatar asked Jun 21 '15 11:06

Philipp_Kats


People also ask

How do I save output in R?

Saving your workspace is how you save your data within R. Click on the Console window, go to the File menu and select “Save Workspace...”. In another R session, you open this workspace with the “Load Workspace...” command. To save everything that has scrolled past on the Console window, click on the Console window.

What does str () create in R code?

str() function in R Language is used for compactly displaying the internal structure of a R object. It can display even the internal structure of large lists which are nested. It provides one liner output for the basic R objects letting the user know about the object and its constituents.

How do I display a string in R?

To display ( or print) a text with R, use either the R-command cat() or print(). Note that in each case, the text is considered by R as a script, so it should be in quotes. Note there is subtle difference between the two commands so type on your prompt help(cat) and help(print) to see the difference.


1 Answers

capture.output will create a character vector (one element for each line printed to the console). If you want it in one string, you could concatenate it with paste(foo, collapse="\n").

data(iris)
(out <- capture.output(str(iris)))
out2 <- paste(out, collapse="\n")
like image 179
Joshua Ulrich Avatar answered Nov 15 '22 04:11

Joshua Ulrich