0

I'm trying to assign an R output

my_r_output <- data.frame( Abundances=c(50.00, 30.00, 20.00), Microbes=c("Microbe1", "Microbe2", "Microbe3"), Test=c(1,1,1))

to a javascript variable in my RMarkdown document to obtain something like this when it is converted into a .md document:

var abundances = 'Abundances    Microbes    Test
50.00   Microbe1    1
30.00   Microbe2    1
20.00   Microbe3    1
'

My R output is a dataframe and the Javascript variable has to be a TSV formatted string.

I tried the following command:

var abundances = '`r write.table( my_r_output, file = "", sep = "\t" )`'
  • file = "" to write to stdout
  • sep = "\t"to obtain TSV formatted string

But I obtain the following result when the .Rmd is converted into a .md document:

var abundances = '

UPDATE1:

Based on Martin Schmelzer reply I tried the following command in my .Rmd document:

var abundances = '`r paste(capture.output(write.table( my_r_output, file = "", sep = "\t" )), "\n", sep="") `'

And I obtain the follwing results in my .md document:

var abundances = 'Abundances    Microbes    Test
, 50.00 Microbe1    1
, 30.00 Microbe2    1
, 20.00 Microbe3    1
'

But it added commas at the beginning of each row.

thbtmntgn
  • 151
  • 2
  • 8
  • What exactly do you mean by "it did not work." What exactly happened? When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 07 '18 at 19:56
  • 1
    Try `capture.output(write.table( my_r_output, file = "", sep = "\\t"))` – Martin Schmelzer Mar 08 '18 at 09:40
  • Almost it thanks!! Except that each row are now separate by comma instead of new line.. – thbtmntgn Mar 08 '18 at 14:37

0 Answers0