Home > database >  Output two questions from a loop in one page-r markdown
Output two questions from a loop in one page-r markdown

Time:01-19

I currently have a data frame that looks like

idx = c(1:6)
Prob = c("4 0.4","1.5 9","1.1 0.6","0.4 1","0.25 0.5","0.25 1.1")
D2 = data.frame(idx,Prob)

I am trying to use Rmarkdown file to create a booklet looping through each question in the file. My current code looks like this and I knit it to pdf:

{r echo=F, comment=NA, results='asis'}
    for (i in 1:nrow(D2)){
      a = D2%>% select(Prob) %>% slice(i) %>% pull
      cat("  \n") 
      cat("\\vspace{1cm}")
      cat(i,".",a)
      cat("  \n") 
      cat("\\vspace{1cm}")
      cat("\\begin{Form}
        \\TextField[width = 16cm,%
                   height = 7cm,%
                   multiline=true,%
                   value = {%
                                 Please show your work here
                            }%
                   ]{}
    \\end{Form}")
      cat("  \n") 
      cat("\\vspace{7cm}")
      cat("  \n") 
      cat("\\begin{Form}
        \\TextField[width = 16cm,%
                   height = 3cm,%
                   multiline=true,%
                   value = {%
                                 Please write your answer here
                            }%
                   ]{}
    \\end{Form}")
      cat("\\newpage  ")
    }

The current output looks like this with one problem on each page. output I would like to have two problems on one page but I don't know how to do it. Any help would be appreciated.

CodePudding user response:

I had to remove spaces from your code, but this works. I commented out some of the vertical space. Because of the page size this automatically set two per page. (Although, I did add \newpage in R Markdown before the chunk so the title and all that wouldn't be on the same page as the forms.

If you wanted to enforce the next page, you can use the if statement commented out at the end to add \\newpage to every other form. (It's checking to see if i is even or odd by looking for a modulo AKA remainder.)

\newpage

```{r echo=F, comment=NA, results='asis'}
    for (i in 1:nrow(D2)){
      a = D2%>% select(Prob) %>% slice(i) %>% pull
      cat("  \n") 
      cat("\\vspace{1cm}")          # removed
      cat(i,".",a)
      cat("  \n") 
      # cat("\\vspace{1cm}")
      cat("\\begin{Form}
        \\TextField[width = 16cm,%
                   height = 6cm,%.   # I reduced here by 1, as well
                   multiline=true,%
                   value = {%
                                 Please show your work here
                            }%
                   ]{}
    \\end{Form}")
      cat("  \n") 
      # cat("\\vspace{7cm}")        # removed
      cat("  \n") 
      cat("\\begin{Form}
        \\TextField[width = 16cm,%
                   height = 3cm,%
                   multiline=true,%
                   value = {%
                                 Please write your answer here
                            }%
                   ]{}
    \\end{Form}")
     # if((i %% 2) == 0) cat("\\newpage  ")
    }
```

enter image description here

  •  Tags:  
  • Related