我正在使用DT包中的Shiny和renderDataTable函数在我的Web应用程序中显示一个表。排序时,似乎将数字视为字符串,按第一个数字然后第二个顺序排序,依此类推,即应排序为1、2、5、100、250的东西将排序为1、100、2、250、5] >
我尝试在读取csv时指定colClasses,但似乎不起作用。
显示的server.R,我的ui.R只是一个dataTableOutput
library(DT)
library(dplyr)
date <- format(Sys.Date() - 1, '%Y_%m_%d')
date2 <- format(Sys.Date() - 2, '%Y_%m_%d')
# Read data in
tab1 <- read.csv(paste0(date, '_tabs.csv'), stringsAsFactors = FALSE, colClasses = c('character', rep('integer', 9)))
tab1 <- na.omit(tab1)
tab2 <- read.csv(paste0(date2, '_tabs.csv'), stringsAsFactors = FALSE, colClasses = c('character', rep('numeric', 9)))
# Ensuring both tables have matching values for country
tab3 <- tab2[tab2$X %in% tab1$X, ]
missingr <- setdiff(tab1$X, tab3$X)
for (j in missingr) {
tab3 <- rbind(tab3, rep(0, length(tab1)))
tab3[nrow(tab3), 1] <- j
}
# Sorting by country and creating a new dataframe of differences
Country <- tab1$X
tab1 <- arrange(tab1, X)
tab3 <- arrange(tab3, X)
tab1 <- tab1[, !(names(tab1) %in% 'X')]
tab3 <- tab3[, !(names(tab3) %in% 'X')]
tab2 <- tab1 - tab3
# Adding total column and country column to dataframes
c1 <- c('Total', colSums(tab1))
c2 <- c('Total', colSums(tab2))
rownames(tab2) <- Country
tab2 <- data.frame(Country, tab2)
tab1 <- data.frame(Country, tab1)
tab1 <- tab1[tab1$total > 100, ]
tab2 <- tab2[tab2$Country %in% tab1$Country, ]
tab1 <- rbind(tab1, c1)
tab2 <- rbind(tab2, c2)
shinyServer(function(input, output) {
output$tab1 <- renderDataTable({tab1},
rownames = FALSE, options = list(lengthMenu = list(c(20, 10, -1), c('20', '10', 'All')),
initComplete = JS("function(settings, json) {","$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});","}"),
autoWidth = TRUE,
columnDefs = list(list(width = '200px', targets = "_all"))
))
output$tab2 <- renderDataTable({tab2},
rownames = FALSE, options = list(lengthMenu = list(c(20, 10, -1), c('20', '10', 'All')),
initComplete = JS("function(settings, json) {","$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});","}"),
autowidth = TRUE,
columnDefs = list(list(width = '200px', targets = "_all"))
))
}
)
我正在使用DT包中的Shiny和renderDataTable函数在我的Web应用程序中显示一个表。排序时,似乎将数字视为字符串,按第一个数字然后第二个和...
[创建c1时,我没有意识到它是一个字符向量,当将其重新绑定到最后的数据帧时,整个数据帧最终变成了字符。
我们有同样的问题,也导致在我们的Rshiny应用程序上对列进行排序时出现问题。我们发现这是cbind将我们的数值转换为字符串引起的。
在# Sorting by country and creating a new dataframe of differences
下,我将包含as.numeric(tab1$X)
。通常,可以使用该函数将数字类型的值强制为实际数字。