I am unable to print table rownames in Shiny using the code below:
library(shinydashboard)
library(shiny)
myData <- matrix(
data=c('13,867','$229,153','30,128','$16.53','98.17%','39.69%'),
nrow = 6, ncol = 1, dimnames = list(letters[1:6], c("Metrics"))
)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
box(dataTableOutput("table"))
)
)
server <- function(input, output) {
output$table <- renderDataTable({
myData
})
}
shinyApp(ui, server)
And the output I get is shown below. Could someone please point out the mistake?
Output
You can add rownames with the DT package. rownames is an argument of DT::datatable which can also be used in DT::renderDT via the the options parameter.
library(shinydashboard)
library(shiny)
library(DT)
myData <- matrix(
data=c('13,867','$229,153','30,128','$16.53','98.17%','39.69%'),
nrow = 6, ncol = 1, dimnames = list(letters[1:6], c("Metrics"))
)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
box(DTOutput("table"))
)
)
server <- function(input, output) {
output$table <- renderDT({
myData
}, options = list(rownames = TRUE))
}
shinyApp(ui, server)
Related
In the DT::datatable() of my shiny app below I have found how to add "thousands" mark )(.) in my table but I want to get rid of the decimals numbers.
library(shiny)
library(shinydashboard)
library(DT)
## app.R ##
library(shiny)
library(shinydashboard)
library(DT)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
),
dashboardBody(
dataTableOutput("table")
)
)
server <- function(input, output) {
iris<-iris[,1:4]*100000
output$table <- renderDataTable({
datatable(iris) %>%
formatCurrency(columns = c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width"), currency = "", interval = 3, mark = ".") %>%
formatStyle(
columns = c("Sepal.Length")
) })
}
shinyApp(ui, server)
Just add digits=0 to the formatCurrency().
Is it possible to change the column names manually in rhandsontable? Also why cant I add a new row or column here?
## app.R ##
library(shiny)
library(shinydashboard)
library(rhandsontable)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
),
dashboardBody(
rHandsontableOutput('table')
)
)
server <- function(session,input, output) {
output$table <- renderRHandsontable({
rhandsontable(iris, width = 550, height = 300) %>%
hot_context_menu(allowRowEdit = FALSE, allowColEdit = FALSE)
})
}
shinyApp(ui, server)
Is there a way to make the Search bar of the datatable empty instead of having the 'setosa' inside it by default while keeping the 'setosa' highlighted inside the table? Or at least find another way to highlight or underline the 'setosa'?
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(
),
dashboardBody(
DT::dataTableOutput("t")
)
)
server <- function(input, output) {
output$t <- renderDT(
datatable(iris, options = list(searchHighlight = TRUE, search = list(search = 'setosa')))
)
}
shinyApp(ui, server)
Ok, you can do something like this.
library(DT)
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(
),
dashboardBody(
DT::dataTableOutput("t")
)
)
server <- function(input, output) {
data <- reactive({
mydata <- iris
rownames(mydata) <- gsub("setosa",tags$span(style="color:red", "setosa"),rownames(mydata))
for(i in 1:ncol(mydata)){
mydata[,i] <- gsub("setosa",tags$span(style="color:red", "setosa"),mydata[,i])
}
mydata
})
output$t <- renderDT(
datatable(data(), options = list(searchHighlight = TRUE, search = list(search = '')), escape = F)
)
}
shinyApp(ui, server)
I'm using the attached code to generate sub-tables based on groups. For some reason only the last portion of the data is rendered for every table.
It would be great if someone can tell me what is going wrong.
BR
library(shiny)
library(shinydashboard)
library(DT)
library(data.table)
tabnames <- LETTERS[1:6]
DT <- data.table(mtcars[1:30,], keep.rownames=TRUE)
DT[, grp:=rep(tabnames, each=trunc(nrow(mtcars)/length(tabnames)))]
ui = dashboardPage(
dashboardHeader(title = "Dynamic DTs"),
dashboardSidebar(),
dashboardBody(
uiOutput("tables"),
p(),
verbatimTextOutput("selectedCells")
)
)
server <- function(input, output, session) {
output$tables <- renderUI({
output_list <- list()
for(i in seq(tabnames)){
output_list[[i]] <- column(4, DT::dataTableOutput(outputId=tabnames[i]))
}
print(fluidRow(output_list))
return(fluidRow(output_list))
})
for(i in seq(tabnames)){
tabname <- tabnames[i]
local({
print(DT[grp %in% tabname, 1:3])
output[[tabname]] <- DT::renderDataTable({
DT[grp %in% tabname, 1:3]
}, selection=list(mode="multiple", target="cell"))
})
}
output$selectedCells <- renderPrint(input$A_cells_selected)
}
shinyApp(ui = ui, server = server)
Ok, found a solution: needed to pass it in a separate variable:
library(shiny)
library(shinydashboard)
library(DT)
library(data.table)
tabnames <- LETTERS[1:6]
DT <- data.table(mtcars[1:30,], keep.rownames=TRUE)
DT[, grp:=rep(tabnames, each=trunc(nrow(mtcars)/length(tabnames)))]
ui = dashboardPage(
dashboardHeader(title = "Dynamic DTs"),
dashboardSidebar(),
dashboardBody(
uiOutput("tables"),
p(),
verbatimTextOutput("selectedCells")
)
)
server <- function(input, output, session) {
output$tables <- renderUI({
output_list <- list()
for(i in seq(tabnames)){
output_list[[i]] <- column(4, DT::dataTableOutput(outputId=tabnames[i]))
}
print(fluidRow(output_list))
return(fluidRow(output_list))
})
for(i in seq(tabnames)){
tabname <- tabnames[i]
local({
subDT <- DT[grp %in% tabname, 1:3]
output[[tabname]] <- DT::renderDataTable({
subDT
}, selection=list(mode="multiple", target="cell"))
})
}
output$selectedCells <- renderPrint(input$A_cells_selected)
}
shinyApp(ui = ui, server = server)
Below is the code I have written. I am not able to use formattable in my shiny. formattable helps in formatting the tables and improves the visualization also.
library("shinydashboard")
library("shiny")
library("formattable")
body <- dashboardBody(
fluidRow(
column(width = 12,
box(tableOutput(formattable(test.table, list())))
)
)
)
ui <- dashboardPage(
dashboardHeader(title = "Column layout"),
dashboardSidebar(),
body
)
server <- function(input, output) {
test.table <- data.frame(lapply(1:8, function(x) {1:10}))
output$table <- renderTable({test.table})
}
shinyApp(ui = ui, server = server)
you have to use renderFormattable, formattableOutput and formattable, all three for it to work
library("shinydashboard")
library("shiny")
library("formattable")
body <- dashboardBody(
fluidRow(
column(width = 12,
box(formattableOutput("table"))
)
)
)
ui <- dashboardPage(
dashboardHeader(title = "Column layout"),
dashboardSidebar(),
body
)
server <- function(input, output) {
test.table <- data.frame(lapply(1:8, function(x) {1:10}))
output$table <- renderFormattable({formattable(test.table, list())})
}
shinyApp(ui = ui, server = server)