简体   繁体   中英

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

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?

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM