5
votes

I feel like I am making this more difficult than it is. All I need to do is make a table from my .csv document and then Knit it to show up in an HTML document and ioslides presentation. I am working in RMarkDown in RStudio. The start of the code is just this:

```{r echo=FALSE, results='hide',message=FALSE}    
DF <- read.csv('DF.csv', header = TRUE)
```

Where my data is something like this (but as a .csv):

     Animal  num1  num2 
0    22      36.6   213      
1    39      42.44  141      
2    40      39     157

And I have tried things like:

```{r}
DF
```

But that just throws all the data onto a slide without putting it into a table. When I try things such as

```{r table2, results='asis', message=FALSE} 
print(xtable(head(Df))) 
```

Or:

```{r table2, results='asis', message=FALSE} 
data.table(DF)
```

It runs fine when I just run the lines in R but as soon as I try to run Knitr it comes up with the error message "Error in eval(expr, envir, enclos): could not find function "data.table" Calls: handle-> withCallingHandlers -> withVisible -> eval-> eval Execution halted". So I thought maybe I had to covert the table to a markdown document and then I could Knit it. Such as:

```{r table2, results='asis', message=FALSE} 
kable(head(DF), format = "markdown")
```

No such luck though, clicking Knitr just ran it to that line with an error message of: ""Error in eval(expr, envir, enclos): could not find function "kable" Calls: handle-> withCallingHandlers -> withVisible -> eval-> eval Execution halted". Does Knitr not recognize these functions or am I just going about this completely wrong. I want the table to look like this:

table

1
you have to load the data table package for data table or knitr for kablerawr
I have both data.table and knitr packages loaded.Michael
you have to do it in the knitr document in a code chunkrawr
I knew I was making this harder than it is. Thank you.Michael
@rawr Are you going to make that an answer?Brian Diggs

1 Answers

8
votes

You need to include the libraries. What I do is create an invisible chunk like so:

```{r include=FALSE}
library(pander)
```

I recommend PANDER which will produce a table like the one in your screenshot

```{r, comment=NA}
pander(DF, type = 'grid')
```