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
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:
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.
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 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.
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
}