Im trying to display a dataframe that I process as datatable in a shiny
app but I get:
Error in datatable("dt") :
'data' must be 2-dimensional (e.g. data frame or matrix)
## app.R ##
library(shiny)
library(shinydashboard)
library(DT)
library(tibble)
initial<-structure(list(case_id = c("3397364", "3397364"), action = c("3397364-RAAMELK",
"3397364-RAAMELK"), resource = c("RAAMELK", "RAAMELK"), lifecycle = c(1,
1), registration_type = structure(1:2, .Label = c("start", "complete"
), class = "factor"), timestamp = structure(c(1667523600, 1667531220
), tzone = "UTC", class = c("POSIXct", "POSIXt")), activity = c("RAAMELK",
"RAAMELK"), activity_description = c("Forbrukt r<e5>melk", "Forbrukt r<e5>melk"
), ...9 = c(NA, NA), product = c("K101152", "K101152"), product_type_text = c("200100 - Milk",
"200100 - Milk"), qty = c(NA, 31), in_out = c("in", "out"), qty_scrap = c(NA_real_,
NA_real_), `FP ordre` = c(NA_character_, NA_character_), Artikkeltype = c("SF",
"SF"), .order = 1:2), row.names = c(NA, -2L), class = c("eventlog",
"log", "tbl_df", "tbl", "data.frame"), case_id = "case_id", activity_id = "activity", activity_instance_id = "action", lifecycle_id = "registration_type", resource_id = "resource", timestamp = "timestamp")
shinyApp(
ui = dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
datatable("dt")
#selectInput("cols","Select column to convert",choices = colnames(initial),multiple = F,selected =colnames(initial)[1] ),
#actionButton("conv","Convert to numeric")
)
),
server = function(input, output, session) { # add session so we can update
output$dt<-renderDataTable({
dat<-data.frame(tibble(Name = colnames(initial), Class = sapply(initial, function(x)
paste(class(x), collapse=", "))))
datatable(dat)
})
}
)
CodePudding user response:
You are using the datatable
function instead of the dataTableOutput
function for within the as per the ui
Shiny function documentation. The DT documentation is always a good source as well if you are working with tables.
In addition, you do not need to wrap your dataframe dt
with the datatable
function in the server
function. You can just return the dataframe as a tibble or as a dataframe at the end of the renderTable
function.
I tidyed up your code, added the necessary R packages at the top for others to test, and implemented the fix that I mentioned just above.
library(shiny)
library(tidyverse) # for the tibble
# Tidy version of the reprex table
initial <- structure(
list(
case_id = c("3397364", "3397364"),
action = c(
"3397364-RAAMELK",
"3397364-RAAMELK"
),
resource = c("RAAMELK", "RAAMELK"),
lifecycle = c(1, 1),
registration_type = structure(
1:2,
.Label = c("start", "complete"), class = "factor"
),
timestamp = structure(c(1667523600, 1667531220),
tzone = "UTC",
class = c("POSIXct", "POSIXt")
),
activity = c(
"RAAMELK",
"RAAMELK"
),
activity_description = c("Forbrukt r<e5>melk", "Forbrukt r<e5>melk"), ...9 = c(NA, NA), product = c("K101152", "K101152"), product_type_text = c(
"200100 - Milk",
"200100 - Milk"
),
qty = c(NA, 31),
in_out = c("in", "out"),
qty_scrap = c(NA_real_, NA_real_),
`FP ordre` = c(NA_character_, NA_character_),
Artikkeltype = c("SF", "SF"),
.order = 1:2
),
row.names = c(NA, -2L), class = c(
"eventlog",
"log", "tbl_df", "tbl", "data.frame"
),
case_id = "case_id",
activity_id = "activity",
activity_instance_id = "action",
lifecycle_id = "registration_type",
resource_id = "resource",
timestamp = "timestamp"
)
shinyApp(
ui = fluidPage( # I used the fluidPage here for simplicity
dataTableOutput("dt")
#selectInput("cols","Select column to convert",choices = colnames(initial),multiple = F,selected =colnames(initial)[1] ),
#actionButton("conv","Convert to numeric")
)
,
server = function(input, output, session) { # add session so we can update
output$dt<-renderDataTable({
tibble(
Name = colnames(initial),
Class = sapply(initial, function(x) {
paste(class(x), collapse = ", ")
})
)
# datatable(dat) # this step was causing you trouble, and can be dropped
})
}
)
Hope that helps,