R: Xtable and Dates

R: xtable and dates

This is arguably a bug in xtable - you may want to report it to the maintainer.

A temporary work-around is to call as.character() on the classes that xtable misinterprets (apart from "Date" I can think of "POSIXt" but there may be others), e.g.:

xtable <- function(x, ...) {
for (i in which(sapply(x, function(y) !all(is.na(match(c("POSIXt","Date"),class(y))))))) x[[i]] <- as.character(x[[i]])
xtable::xtable(x, ...)
}

How to retain date/time format in xtable for an email

Convert the date to character using as.character() before printing the table should do the job.

Disclaimer: I moved this answer here from the comments so other users can make use of the solution.

How do you get dates to show up in a date format when working with a Shiny Table?

I finally figured out the simple solution.

I just changed the code when I build the dataframe from

  data <- eventReactive(input$submit, {         
cbind(input$date,input$a, input$b)

to

  data <- eventReactive(input$submit, {         
cbind(as.character(input$date),input$a, input$b))

Adding the as.character() seems to have done the trick. I don't know if this will have consequences later, but the displayed table now looks nice.

R shiny table with dates

Try using this:

not_outlier_dates[,2] <- as.character(not_outlier_dates[,2])

The problem lies in the xtable library that Shiny uses to display tables. It seems there are bugs related to date columns, as stated in this SO question and this (closed) Shiny issue. Not sure if that still applies or if it applies to your xtable version, though.

LaTeX table through xtable

summary is producing four tables in a list, so using lapply like so:

mod <- aov(Yield ~ Block + A * B + Error(A * Block), data=aovdat)
lapply(summary(mod), xtable)

will produce a list of LaTeX tables.

A for loop with print will produce the LaTeX code only, with the table names in the LaTeX caption.

for (i in 1:4) print(xtable(summary(mod)[[i]], names(summary(mod)[i])))

If you want all the tables as one, you need to do a bit of work because rbind doesn't like tables with different numbers of columns. rbind.fill in the plyr package will do the job

library(plyr)

aovtab <- do.call(rbind.fill, lapply(1:length(summary(mod)),
function(x) unclass(summary(mod)[[x]])[[1]]))

but we have to manually add the rownames back as data because a) rbind.fill drops them, and b) they were duplicated anyway, which xtable wouldn't like.

SOV <- unlist(lapply(1:length(summary(mod)), 
function(x) rownames(unclass(summary(mod)[[x]])[[1]]))

print(xtable(cbind(SOV, aovtab), include.rownames=F)

Problems forming a LaTeX table using xtable-package in R

Here's my approach (with inspiration from)

library(xtable)
financial <- c(1.23, 1.19)
macro <- c(1.50, 1.40)
X <- rbind(financial, macro)
colnames(X) <- c("A","B")

addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 0
addtorow$pos[[6]] <- 0
addtorow$pos[[7]] <- 0
addtorow$command <- c('Country: & United States & \\\\\n',
'\\hline',
'Method: & Regression & \\\\\n',
'\\hline',
'Models: & RMSE Result & \\\\\n',
'\\hline',
'& A & B \\\\\n')

print(xtable(X), add.to.row = addtorow, include.colnames = FALSE )

add.to.row can be used to include custom latex code into xtable.

I did could not figure out how to place your column names & A & B after the custom header. My solution for this was to use include.colnames = FALSE and manually inserted column headers in add.to.row. (perhaps someone else has a more elegant solution?)

Result:

% latex table generated in R 4.0.5 by xtable 1.8-4 package
% Mon Jan 17 14:16:51 2022
\begin{table}[ht]
\centering
\begin{tabular}{rrr}
\hline
Country: & United States & \\
\hline Method: & Regression & \\
\hline Models: & RMSE Result & \\
\hline & A & B \\
\hline
financial & 1.23 & 1.19 \\
macro & 1.50 & 1.40 \\
\hline
\end{tabular}
\end{table}

Sample Image

How to put date on barplot() x-axis?

Easier option is formula method

barplot(values ~ dates, col = cols, xlab = "values")

-output

Sample Image


According to ?barplot, both 'height', 'width' are vector and Date is a vector with some attributes. So, if we convert to vector with as.numeric, it would work

barplot(height = values, width = as.numeric(dates),col = cols, xlab = "values")

data

dates <- structure(c(18538, 18539, 18556), class = "Date")

set different Digits in xtable

You may define the digits for each column. Your table has 12 columns. So the first 11 columns get 2 digits and the 12th column gets 0 digits.

xtable::xtable(head(mtcars), digits = c(rep(2, 11), 0))

Result

% latex table generated in R 3.6.3 by xtable 1.8-4 package
% Mon Oct 12 10:25:04 2020
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrrrrrrr}
\hline
& mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\
\hline
Mazda RX4 & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.62 & 16.46 & 0.00 & 1.00 & 4.00 & 4 \\
Mazda RX4 Wag & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.88 & 17.02 & 0.00 & 1.00 & 4.00 & 4 \\
Datsun 710 & 22.80 & 4.00 & 108.00 & 93.00 & 3.85 & 2.32 & 18.61 & 1.00 & 1.00 & 4.00 & 1 \\
Hornet 4 Drive & 21.40 & 6.00 & 258.00 & 110.00 & 3.08 & 3.21 & 19.44 & 1.00 & 0.00 & 3.00 & 1 \\
Hornet Sportabout & 18.70 & 8.00 & 360.00 & 175.00 & 3.15 & 3.44 & 17.02 & 0.00 & 0.00 & 3.00 & 2 \\
Valiant & 18.10 & 6.00 & 225.00 & 105.00 & 2.76 & 3.46 & 20.22 & 1.00 & 0.00 & 3.00 & 1 \\
\hline
\end{tabular}
\end{table}


Related Topics



Leave a reply



Submit