Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将数字排序为字符串_R_Sorting_Shiny - Fatal编程技术网

R 将数字排序为字符串

R 将数字排序为字符串,r,sorting,shiny,R,Sorting,Shiny,我正在使用shiny和DT包中的renderDataTable函数在我的webapp中显示一个表。排序时,似乎将数字视为字符串,按第一位数字排序,然后按第二位排序,依此类推,即应排序为1、2、5、100、250的数字将排序为1、100、2、250、5 我尝试在读取csv时指定ColClass,但似乎不起作用 如图所示,我的ui.R只是一个dataTableOutput library(DT) library(dplyr) date <- format(Sys.Date() - 1, '%Y

我正在使用shiny和DT包中的renderDataTable函数在我的webapp中显示一个表。排序时,似乎将数字视为字符串,按第一位数字排序,然后按第二位排序,依此类推,即应排序为1、2、5、100、250的数字将排序为1、100、2、250、5

我尝试在读取csv时指定ColClass,但似乎不起作用

如图所示,我的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"))
    ))
}
)

在“按国家排序并创建新的差异数据框”下,我将包含一个as.numerictab1$X。通常,您可以使用该函数将数字类型值强制转换为实际数字

在创建c1时,我没有意识到它是一个字符向量,在最后将它绑定到数据帧时,整个数据帧最终变成了字符


多亏了@user5029763

,我们在Rshiny应用程序上对列进行排序时也遇到了同样的问题。我们发现这是由cbind将数值转换成字符串引起的

> foo=c(111,10,3,4,5)
> bar=c("should","it","order","like","yoda")

> df1 = data.frame(cbind(foo,bar))
> df1[order(df1[,1]),]

  foo    bar
2  10     it
1 111 should
3   3  order
4   4   like
5   5   yoda
您可以通过检查来验证这一点

> str(df1)
'data.frame':   5 obs. of  2 variables:
 $ foo: Factor w/ 5 levels "10","111","3",..: 2 1 3 4 5
 $ bar: Factor w/ 5 levels "it","like","order",..: 4 1 3 2 5
这是因为先调用cbind,然后创建一个字符数组。当您考虑它时,这是很自然的,因为数组需要具有相同类型的所有元素,并且数字可以转换为字符,但不能反过来转换。 通过在数据帧对象上使用cbind,可以很容易地避免这种情况:

> df2 = cbind(data.frame(foo),data.frame(bar))
> df2[order(df2[,1]),]

  foo    bar
3   3  order
4   4   like
5   5   yoda
2  10     it
1 111 should
或者在本例中,通过直接从字符串和数字数组创建新的数据帧,可能会更优雅:

> df3 = data.frame(foo,bar)
> df3[order(df3[,1]),]

  foo    bar
3   3  order
4   4   like
5   5   yoda
2  10     it
1 111 should

你在哪里订桌子?默认情况下,renderDataTable不会对表排序。的第4.1节解释了排序选项。单击列对其进行排序,就像在刚才链接的页面上一样,按最左边列排序的默认选项是我最初希望按国家排序的选项,然后用户可以单击列对其进行排序。尝试在您创建的表上使用str。它会告诉你你有什么样的数据,并将输出添加到你的问题中会很有帮助。我没有足够的代表来发布图像,但这里有一个链接,直到有人取下它。图片显示了当你点击最左边的数字列进行排序时会发生什么。在csv文件中读取时,X列是国家代码,因此无论如何都需要是字符。其余的列应该是nums/ints直接从因子到数值是丢失信息的一种很好的方式,例如,年份是正确的。OP说,它们似乎是“字符串”,按数字排序的事实表明它们是字符向量,而不是因子。但你是对的。为了安全起见,默认情况下使用as.numerica.character…Factors作为字符串排序是明智的,所以按数字排序表示因子的数量和表示字符的数量一样多。考虑到R中的StringsAsAffactors=TRUE默认值,并且这是一个数据帧,使得因子的可能性更大。你必须非常清楚在数据帧中得到一个字符向量而不是一个因子,我并不是说你不同意,我只是认为这是一个非常危险的建议。如果您将问题编辑为.numericas.charactertab1$X,或者至少建议查看strtab1或sapplytab1,以确保首先确定类,我会很高兴地将我的反对票改为赞成票。