Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.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_Formatting_Gridextra - Fatal编程技术网

R 表格格式

R 表格格式,r,formatting,gridextra,R,Formatting,Gridextra,我有R格式的下表(以下为csv格式)。因为有很多这样的格式,我需要格式化它们,所以我尝试使用tableGrob来节省大量时间,但我需要以非常特定的格式输出,包括Calibri字体(也在下面) “2015\nBril”、“2016\nBril”、“2017\nmarzo”、“2017\nBril” “Pedidos”、-23.3、-13.8、-26、-39 “存在”,7.6,1.2,5.3,10.5 “预期”,30.7,32.7,28.7,24 “ICI”、-0.1,5.9、-0.9、-8.5 “

我有R格式的下表(以下为csv格式)。因为有很多这样的格式,我需要格式化它们,所以我尝试使用tableGrob来节省大量时间,但我需要以非常特定的格式输出,包括Calibri字体(也在下面)

“2015\nBril”、“2016\nBril”、“2017\nmarzo”、“2017\nBril”

“Pedidos”、-23.3、-13.8、-26、-39

“存在”,7.6,1.2,5.3,10.5

“预期”,30.7,32.7,28.7,24

“ICI”、-0.1,5.9、-0.9、-8.5

“ICI测试*”,0.4,5.7,-3.2,-6.9

但是,每次我试图编辑主题并打印表格时,都会出现此错误

library(gridExtra)
t1 <- ttheme_default(core=list(
       fg_params=list(fontface=c(rep("plain", 4), "bold.italic")),
       bg_params = list(fill=c(rep(c("grey95", "grey90"),
                               length.out=4), "#6BAED6"),
                    alpha = rep(c(1,0.5), each=5))
))

t  <- tableGrob(tabla_fin)
grid.table(t, theme = t1)
库(gridExtra)

t1以下是一些对解决您的问题有用的想法

library(grid)
library(gridExtra)
library(gtable)

# Build table
tabla_fin <- rbind(
c("","2015\nabril","2016\nabril","2017\nmarzo","2017\nabril"),
c("Pedidos",-23.3,-13.8,-26,-39),
c("Existencias",7.6,1.2,5.3,10.5),
c("Expectativas",30.7,32.7,28.7,24),
c("ICI",-0.1,5.9,-0.9,-8.5),
c("ICI Desest*",0.4,5.7,-3.2,-6.9))

colnames(tabla_fin) <- tabla_fin[1,]
tabla_fin <- tabla_fin[-1,]

rownames(tabla_fin) <- tabla_fin[,1]
tabla_fin <- tabla_fin[,-1]

print(tabla_fin)

#              2015\nabril 2016\nabril 2017\nmarzo 2017\nabril
# Pedidos      "-23.3"     "-13.8"     "-26"       "-39"      
# Existencias  "7.6"       "1.2"       "5.3"       "10.5"     
# Expectativas "30.7"      "32.7"      "28.7"      "24"       
# ICI          "-0.1"      "5.9"       "-0.9"      "-8.5"     
# ICI Desest*  "0.4"       "5.7"       "-3.2"      "-6.9"


# Install Calibri font using the extrafont package
# read the help of font_install()
library(extrafont)
loadfonts(device="win")
# font_install()

# Theme for text tables
t1 <- ttheme_default(
        core=list(
         fg_params=list(fontface=rep("plain", 6), fontfamily="Calibri",
                      x=1, hjust=1),
         bg_params = list(
                     fill=c(rep("white",3),"grey80","grey90"),
                     col=c("white","white","white","grey80","grey90"),
                     alpha = rep(1,5))
        ),
        rowhead=list(
         fg_params=list(x=0, hjust=0, fontface="plain", fontfamily="Calibri"),
         bg_params = list(
                     fill=c("white",rep("white",3),"grey80","grey90"),
                     col=c("white","white","white","white","grey80","grey90"),
                     alpha = rep(1,5))
        ),
        colhead=list(
         fg_params=list(fontface="plain",fontfamily="Calibri"),
         bg_params = list(
                     fill="grey70",
                     col="grey70",
                     alpha = rep(1,5))
        )
)

# Create gtable containing text grobs
t  <- tableGrob(tabla_fin, theme=t1)

# Add horizontal and vertical lines
t <- gtable_add_grob(t,
        grobs = segmentsGrob( # line across the bottom
            x0 = unit(0,"npc"),
            y0 = unit(0,"npc"),
            x1 = unit(1,"npc"),
            y1 = unit(0,"npc"),
            gp = gpar(lwd = 4.0)),
        t = 1, b = 1, l = 1, r = ncol(t))
t <- gtable_add_grob(t,
        grobs = segmentsGrob( # line across the bottom
            x0 = unit(0,"npc"),
            y0 = unit(0,"npc"),
            x1 = unit(1,"npc"),
            y1 = unit(0,"npc"),
            gp = gpar(lwd = 4.0)),
        t = 4, b = 4, l = 1, r = ncol(t))
t <- gtable_add_grob(t,
        grobs = segmentsGrob( # line across the bottom
            x0 = unit(0,"npc"),
            y0 = unit(0,"npc"),
            x1 = unit(0,"npc"),
            y1 = unit(1,"npc"),
            gp = gpar(lwd = 4.0)),
        t = 1, b = nrow(t), l = 4, r =4)

# Draw table
grid.newpage()
grid.draw(t)
库(网格)
图书馆(gridExtra)
图书馆(gtable)
#生成表

tabla_fin以下是一些对解决您的问题有用的想法

library(grid)
library(gridExtra)
library(gtable)

# Build table
tabla_fin <- rbind(
c("","2015\nabril","2016\nabril","2017\nmarzo","2017\nabril"),
c("Pedidos",-23.3,-13.8,-26,-39),
c("Existencias",7.6,1.2,5.3,10.5),
c("Expectativas",30.7,32.7,28.7,24),
c("ICI",-0.1,5.9,-0.9,-8.5),
c("ICI Desest*",0.4,5.7,-3.2,-6.9))

colnames(tabla_fin) <- tabla_fin[1,]
tabla_fin <- tabla_fin[-1,]

rownames(tabla_fin) <- tabla_fin[,1]
tabla_fin <- tabla_fin[,-1]

print(tabla_fin)

#              2015\nabril 2016\nabril 2017\nmarzo 2017\nabril
# Pedidos      "-23.3"     "-13.8"     "-26"       "-39"      
# Existencias  "7.6"       "1.2"       "5.3"       "10.5"     
# Expectativas "30.7"      "32.7"      "28.7"      "24"       
# ICI          "-0.1"      "5.9"       "-0.9"      "-8.5"     
# ICI Desest*  "0.4"       "5.7"       "-3.2"      "-6.9"


# Install Calibri font using the extrafont package
# read the help of font_install()
library(extrafont)
loadfonts(device="win")
# font_install()

# Theme for text tables
t1 <- ttheme_default(
        core=list(
         fg_params=list(fontface=rep("plain", 6), fontfamily="Calibri",
                      x=1, hjust=1),
         bg_params = list(
                     fill=c(rep("white",3),"grey80","grey90"),
                     col=c("white","white","white","grey80","grey90"),
                     alpha = rep(1,5))
        ),
        rowhead=list(
         fg_params=list(x=0, hjust=0, fontface="plain", fontfamily="Calibri"),
         bg_params = list(
                     fill=c("white",rep("white",3),"grey80","grey90"),
                     col=c("white","white","white","white","grey80","grey90"),
                     alpha = rep(1,5))
        ),
        colhead=list(
         fg_params=list(fontface="plain",fontfamily="Calibri"),
         bg_params = list(
                     fill="grey70",
                     col="grey70",
                     alpha = rep(1,5))
        )
)

# Create gtable containing text grobs
t  <- tableGrob(tabla_fin, theme=t1)

# Add horizontal and vertical lines
t <- gtable_add_grob(t,
        grobs = segmentsGrob( # line across the bottom
            x0 = unit(0,"npc"),
            y0 = unit(0,"npc"),
            x1 = unit(1,"npc"),
            y1 = unit(0,"npc"),
            gp = gpar(lwd = 4.0)),
        t = 1, b = 1, l = 1, r = ncol(t))
t <- gtable_add_grob(t,
        grobs = segmentsGrob( # line across the bottom
            x0 = unit(0,"npc"),
            y0 = unit(0,"npc"),
            x1 = unit(1,"npc"),
            y1 = unit(0,"npc"),
            gp = gpar(lwd = 4.0)),
        t = 4, b = 4, l = 1, r = ncol(t))
t <- gtable_add_grob(t,
        grobs = segmentsGrob( # line across the bottom
            x0 = unit(0,"npc"),
            y0 = unit(0,"npc"),
            x1 = unit(0,"npc"),
            y1 = unit(1,"npc"),
            gp = gpar(lwd = 4.0)),
        t = 1, b = nrow(t), l = 4, r =4)

# Draw table
grid.newpage()
grid.draw(t)
库(网格)
图书馆(gridExtra)
图书馆(gtable)
#生成表

tabla\u fin如果没有可复制的示例,我不知道该建议什么,但是
grid.table(tabla\u fin,theme=t1)
比您当前的代码更有意义。如果没有可复制的示例,我不知道该建议什么,但是
grid.table(tabla\u fin,theme=t1)
比您当前的代码更有意义。