R Markdown hard question...

Hello

I'm fairly new to R but really like the language. I have one problems I struggled with for quite some time but have not been able to solve and ask here for help.

Have a data frame with columns that I want to print in a markdown document. I have managed to loop over each line and print it in the document. What I do not succeed with, however, is to make a data table from in the same loop under the text. The information to be displayed there should only be from the same line from which I base my text. And that is included in the first block. Should I do a loop in a loop?

Can anyone help me with this I am eternally grateful =)

Ex.

Date 			time 		text_1 		text_2

1 2020-01-01 10:19:10 Hello bla bla1 Hi bla bla
2 2020-01-02 10:20:10 Hello bla bla2 Hi bla bla
4 2020-01-03 10:21:10 Hello bla bla3 Hi bla bla
5 2020-01-04 10:22:10 Hello bla bla4 Hi bla bla

Markdown

block 1

2020-01-01 Hello bla bla1 10:19:10 Hello bla bla

——datatable—— (only from index 1)

block 2

2020-01-02 Hello bla bla2 10:20:10 Hello bla bla

——datatable—— (only from index 2)

block 3

2020-01-03 Hello bla bla3 10:21:10 Hello bla bla

——datatable—— (only from index 3)

Osv

many thanks

Hi, welcome!

We don't really have enough info to help you out. Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

You can also format your code correctly in the post so that it is easier to read. FAQ: How to Format R Markdown Source

Thank you!

ok, then we start over.

In the chunk below, it loops over rows in an excel sheet. It prints blocks based on each line and then the text I entered for each block. What I also want is to make a kable table for each row. So a loop in the loop.

  1. text

template <-"\n\n\n\n %s som text, %s. %s some text.
some text %s %s sometext %s sometext* %s some text
" # dont't forget the newline

for (i in seq(nrow(input))) {
  current <- input[i, ]
  cat(sprintf(template, df$'name', df$'id', df$'object', df$'info', df$'date', df$'time', df$'user', df$'somthing'))
  break
}

I am missing the input here to run your example.

I think you would be interesting in these part of the Rmarkdown cookbook:

With this tool I believe you can manage to do what you want.

I do not really know how to explain it but I have 5 lines in an excel sheet. I want to generate text chunks from each row but also make a simple table from each row.

so first the text and then the table.

ex.

bla bla bla bla bla

table (first row)

bla bla bla bla

table (second row)

I will do a example and post here because have googled so much so the computer will probably burn up soon.

1 Like

Everything works fine but kable only wants to take the first line (in kabel argument) and not go through everything like i do in the beginning.

library('kableExtra')
library('knitr')

employee <- c('John Doe','Peter Gynn','Jolie Hope','John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800,1000, 23400, 26800)
startdate <- (c('2010-11-1','2008-3-25','2007-3-14','2010-11-1','2008-3-25','2007-3-14'))
df <- data.frame(employee, salary, startdate)

input <- df

template <- "\n\n\n\\n\n\\n\n\ text text text text text %s text \n\n\ text texttext text text
text text text  text\n\n\  text %s texttext text texttext text texttext text text\n\n\

%s
\n\n\ 
\n\n\n\ 
\n\n\n\ 
" # dont't forget the newline

for (i in seq(nrow(input))) {
  current <- input[i, ]
  cat(sprintf(template, df$'employee', df$'salary', knitr::kable(head(df, n = 1))))
break
}

Several questions:

  • Why do you have a break in the for loop ? This mean the loop will do one iteration and then break out of the loop
  • You define current in the for loop but you are not using it. You use directly df in the sprintf call - so the all dataframe.
  • Did you try one of the advice pointed above with knitr template ?

For string interpolation, you could be interesting in glue package : https://glue.tidyverse.org/reference/glue.html

For iteration, the purrr package could be of help: https://purrr.tidyverse.org/

Anyway, this is no more a Rmarkdown question per-se but rather a R question on how to wrangle data and loop through it.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.