I have two tables. First columns of both of them should have fixed width, say, 1,5 inches.
Below there is a sample script where for the table named df1 I used flextable() function and for the table named df2 I used flextable() and width() function. However, desired columns' width is not getting fixed to the given length, i.e. 1,5 inches.
Can someone show me how to get column width fixed?
library(shinydashboard)
library(shiny)
library(rhandsontable)
library(flextable)
df1 = data.table(
"Сolumn1"= as.character(),
"Column2"= as.numeric(),
"Сolumn3"= as.character()
)
flextable(
df1,
col_keys = names(df1),
cwidth = 0.75,
cheight = 0.25
)
qflextable(df1)
df2 = data.table(
"Сolumn1"= as.character(),
"Column2"= as.numeric(),
"Сolumn3"= as.character()
)
df2 <- flextable(head(df2))
df2 <- width(df2, width = 1.5)
ui <- dashboardPage(
dashboardHeader(title = "Assets"),
dashboardSidebar(
menuItem("Home", tabName = "home"),
menuItem("Current assets",
tabName = "CurrentAssets",
menuSubItem("Inventory", tabName = "inventory")
)
),
dashboardBody(
tabItems(
tabItem(
tabName = "inventory",
column(
width = 7,
"df1",
rHandsontableOutput("Table1")
),
column(
width = 7,
"df2",
rHandsontableOutput("Table2")
)
)
)
)
)
server <- function(input, output, session) {
output$Table1 <- renderRHandsontable({
rhandsontable(df1, stretcH="all")
})
output$Table2 <- renderRHandsontable({
rhandsontable(df2, stretcH="all")
})
}
shinyApp(ui, server)
It's not clear to me what you are trying to achieve with
flextable. As is your code returns an error as you are passing aflextableobject torhandsontable. You can only have one of both. Either do you create your tables usingflextableor usingrhandsontable. But it's not possible to first create aflextablewith a fixed column width and then convert it magically to arhandsontable. If you want the latter then you have to set the column width using the options provided by therhandsontablepackage.From the docs on sizing a
rhandsontableyou could set the column widths using thecolWidths=argument ofhot_cols(). Additionally,rhandsontable()has an argumentrowHeaderWidthto set the width of the row names column.Below I set the
rowHeaderWidthto 200 and use a numeric vector to set width of the first column to 100 and to 50 for the second and third.Note: For a column layout you have only 12 columns in total, i.e. setting the column widths to 7 for both tables will not work when you want both tables in one row. Hence, I set the width to 6 for both.