0

I'm trying to get a table I've made to output into a pdf for distribution, however, I keep getting an argument length of zero as an error.

my df looks like this, but is 80 rows long:

       DM   MPH_Over Variance
1    ATLR  1.8277662 9.972406
2    BAKA  2.9154519 6.692614
3    SERF  9.1898734 6.633396
4    DKRE  0.9621212 6.723101

The code I've gotten is as follows.

library(gridExtra)
names(dmover) <- c("DM","MPH_Over","Variance")

dmovertab <- as.data.frame(dmover[1,])
EmptyLine <- data.frame(DM = "",MPH_Over = "",Variance = "")

pdf(file = "q.pdf")

for (i in 2:nrow(dmover)) 
{
  if ((nrow(dmover$MPH_Over[i]))  ==  (nrow(dmover$MPH_Over[i-1])))
  {dmovertab <- rbind(dmovertab, dmover[i,])}

  else {
    dmovertab <- rbind(dmovertab, EmptyLine)
    dmovertab <- rbind(dmovertab, dmover[i,]) 
  }
}

grid.table(dmovertab,rows=NULL)
dev.off()

It keeps throwing an error (below) while outputting a .pdf table of 1 row? I do feel like I'm doing something small wrong and I'm going to be an idiot when someone tells me what I've messed up. Any help you can give me to fix this if conditional would be great.

Error in if ((nrow(dmover$MPH_Over[i])) == (nrow(dmover$MPH_Over[i - 1]))) { : argument is of length zero"

  • See the result of `nrow(1:10)` and `nrow(1:10) == nrow(1:10)` and `nrow(1:10)`. maybe you just want `dmover$MPH_Over[i] == dmover$MPH_Over[i-1]`? – lmo Jan 11 '17 at 19:08
  • Doing that breaks the output to an inoperable PDF. But there aren't any errors. – Jordan Cole Jan 11 '17 at 19:16
  • I usually use `xtables` for ouputting latex tables. An easier method is to use knitr and the `kable` function. See [my answer here](http://stackoverflow.com/questions/41365502/aligning-columns-with-knitr-kable-function/41365608#41365608) for a simple example using rmarkdown. – lmo Jan 11 '17 at 19:20
  • undefined columns means that you are asking R to subset non-existent columns. try removing the `[]` and whatever is inside of it, like `1:5`. – lmo Jan 11 '17 at 19:32
  • Sure. I'll try your method. Never done an .rmd before, so here's to hoping I get it right – Jordan Cole Jan 11 '17 at 19:38
  • These are nice. You can also put together memos/reports etc with this technology. Graphs, text, tables, a bit of formatting, latex math, and access to your variables all in one file. – lmo Jan 11 '17 at 19:44
  • It appears your method outputs a broken pdf, too? – Jordan Cole Jan 11 '17 at 19:51
  • It works for me. This sounds potentially like an OS issue with the PDF constructor. I'd search for this issue, on the internet. Maybe update all your software and trying it again? – lmo Jan 11 '17 at 19:55
  • I just reinstalled Rstudio and R today, actually. – Jordan Cole Jan 11 '17 at 19:56
  • and updated all the packages? If so, it's probably not the R software. I've heard of some issues with OSX, which I don't use. – lmo Jan 11 '17 at 19:58
  • I use Windows 10. – Jordan Cole Jan 11 '17 at 19:59
  • Just tinkered a bit and it's saying that object dmover doesn't exist. – Jordan Cole Jan 11 '17 at 20:03
  • You have to construct a codeblock that constructs this object. Easiest to just save it in you current session and then put a `load` statement in the Rmd file, but maybe better to use the code that constructs it in the codeblock. – lmo Jan 11 '17 at 20:06
  • Unfortunately, I don't know how to do that. Got any resources I could look up on? – Jordan Cole Jan 11 '17 at 20:09
  • You could start with `?save` and `?load`. The parts of the Rmd file delimited by 3 backticks are called codeblocks. More on Rmarkdown [here](http://rmarkdown.rstudio.com/) and basics [here](http://rmarkdown.rstudio.com/authoring_basics.html). – lmo Jan 11 '17 at 20:21
  • Thanks. It tells me I need pandoc or Miktex but the install.packages states that its' not available for 3.3.2. Is there a LaTeX engine for 3.3.2? – Jordan Cole Jan 11 '17 at 20:47
  • Aha. That must be it. MikTex is a standalone application that is used with Windows for processing Latex documents. Here is [their site](https://miktex.org/). – lmo Jan 11 '17 at 21:02

0 Answers0