I have the following piece of code:
tableData <- head(original_table[,c("column1",
"column2",
"column3")])
library(xtable)
xt <- xtable(tableData)
print(xt,type="html")
The 'original_table' object is a table where the columns have very awkward names which I do not want in the final output from printing the xtable.
I have a lot of code using the 'original_table' object which comes after the xtable is created. So I do not want to change the column headings in the original table.
How can I change the column headings using xtable so they can appear as something like 'Height','Width' and 'Breadth' in my table output?
xtable inherits data.frame.
So,
library(xtable)
xt <- xtable(tableData)
names(xt) <- c('Height','Width','Breadth' )
will work.
Related
df column description
description
-----------
Whatever your header text looks like
Whatever header text looks like
Whatever your header text looks
Whatever your header text like
Whatever your text looks like
I want to loop through a df column description and display as pointwise from r-markdown
\begin{itemize}
```{r echo=FALSE,results='asis'}
df<-readxl::read_excel(PATh,sheet = 'sheet1')
for (i in 1:nrow(df)){
dummy <- paste0("\\item ",df$description[i])
}
```
`r dummy`
\end{itemize}
i want all rows of column to be shown pointwise. only last one is showing
The loop is rewriting the variable dummy each iteration. Consequently, only the final row will be stored. Something like this should work:
#define the dummy variable ahead of the loop
dummy <- character(nrow(df))
#Index the dummy variable to not overwrite it
for (i in 1:nrow(df)){
dummy[i] <- paste0("\\item ",df$description[i])
}
You would also (probably) need to collapse dummy into a single string
`r paste(dummy, collapse=" ")`
I have the following code in a R Markdown document
```{r echo=FALSE}
s <- summary(indf)
knitr::kable(na.omit(s[,c(1,2)]))
```
The result is:
How can I remove the extra empty column at the beginning?
I am compiling to ioslides
By default, kable adds row names. Trying setting row.names to FALSE:
knitr::kable(na.omit(s[,c(1,2)]), row.names = FALSE)
Try removing the rownames from the data frame.
s <- summary(indf)
rownames(s) <- NULL
knitr::kable(na.omit(s[,c(1,2)]))
I have a table with descriptive statistics I generated using the table1 package (I used it since it can produce descriptive statistics for both categorical and continuous variables). The issue I'm having is that table1 only produce tables in HTML-format. I'd like to transform my table1-table to a kable-table (or equivalent) so I knit my markdown to a pdf-file.
I tried the following code
library(table1)
library(kable)
tb <- table1(reformulate(colnames(df)), data=df, output = "latex")
kable(tb, caption = "Descriptive statistics")
But this produces an empty table looking like this:
Does anyone know how to solve this?
My data frame has ugly column names, but when displaying the table in my report, I want to their "real" names including special characters '(', new lines, greek letters, repeated names, etc.
Is there an easy way of replacing the names in knitr to allow such formatting?
Proposed solution
What I have tried to do is suppress the printing of the data frame names and use add_header_above for better names and names that span several columns. Some advice I've seen says to use:
x <- kable(df)
gsub("<thead>.*</thead>", "", x)
to remove the column names. That's fine, but the issue is that when I subsequently add_header_above, the original column names come back. If I use col.names=rep('',times=ncol(d.df)) in kable(...) the names are gone but the row remains, leaving a gap between my new column names and the table body. Here's a code chunk to illustrate:
```{r functions,echo=T}
drawTable <- function(d.df,caption='Given',hdr.above){
require(knitr)
require(kableExtra)
require(dplyr)
hdr.2 <- rep(c('Value','Rank'),times=ncol(d.df)/2)
x <- knitr::kable(d.df,format='latex',align='c',
col.names=rep('',times=ncol(d.df))) %>%
kable_styling(bootstrap_options=c('striped','hover',
'condensed','responsive'),position='center',
font_size = 9,full_width=F)
x %>% add_header_above(hdr.2) %>%
add_header_above(hdr.above)
}
```
```{r}
df <- data.frame(A=c(1,2),B=c(4,2),C=c(3,4),D=c(8,7))
hdr.above <- c('A2','B2','C2','D2')
drawTable(df,hdr.above = hdr.above)
```
I am not sure where you got the advice to replace rownames, but it seems excessively complex. It is much easier just to use the built-in col.names argument within kable. This solution works for both HTML and LaTeX outputs:
---
output:
pdf_document: default
html_document: default
---
```{r functions,echo=T}
require(knitr)
df <- data.frame(A=c(1,2),B=c(4,2),C=c(3,4),D=c(8,7))
knitr::kable(df,
col.names = c("Space in name",
"(Special Characters)",
"$\\delta{m}_1$",
"Space in name"))
```
PDF output:
HTML output:
If you're targeting HTML, then Δ is an option too.
I couldn't get the accepted answer to work on HTML, so used the above.
I am trying to combine two tables in R Markdown into a single table, one below the other & retaining the header. The figure below shows the desired output. After putting my markdown code I will show the actual output. I realize that the way I have structured the pander statements will not allow me to get the output I want but searching SO I was unsuccessful in finding the right way to do so.
I can do some post processing in Word to get the output exactly as I want but I am trying to avoid that overhead.
The testdat.RData file is here: https://drive.google.com/file/d/0B0hTmthiX5dpWDd5UTdlbWhocVE/view?usp=sharing
The R Markdown RMD file is here: https://drive.google.com/file/d/0B0hTmthiX5dpSEFIcGRNQ1MzM1E/view?usp=sharing
Desired Output
```{r,echo=FALSE,message = FALSE, tidy=TRUE}
library(pander)
load("testdat.RData")
pander::pander(t1,big.mark=',', justify=c('left','right','right','right'))
pander::pander(t2,big.mark=',', justify=c('left','right','right','right'))
```
Actual Output
Thanks,
Krishnan
Here's my attempt using the xtable package:
```{r,echo=FALSE, message = FALSE, results="asis"}
library(xtable)
# Add thousands separator
t1[-1] = sapply(t1[-1], formatC, big.mark=",")
t2[-1] = sapply(t2[-1], formatC, big.mark=",")
t1$Mode = as.character(t1$Mode)
# Bind together t1, extra row of column names, and t2
t1t2 = rbind(t1, names(t1), t2)
# Render the table using xtable
print(xtable(t1t2, align="rrrrr"), # Right-align all columns (includes extra value for row names)
include.rownames=FALSE, # Don't print rownames
hline.after=NULL,
# Add midrules before/after each set column names
add.to.row = list(pos = list(-1,0,4,5),
command = rep("\\midrule \n",4)))
```
And here's the output:
Allow me to make a formal answer since my comment seemed to work for you.
pander(rbind(t1,names(t2),t2))