如何更改 R 中 DT::Datatable 中这些按钮的标签并更改行的颜色?
How can I change the labels of these buttons in DT::Datatable in R and change collors of rows?
我需要更改 R 中按钮的标签 DT::Datatable。
谢谢你的帮助!
DT:Datatable 具有允许某些格式化功能的扩展名,但不会记录所有内容。
它使用按钮扩展来过滤表格中的列。按钮 "search" 我设法更改了属性: language = list(search = 'Procurar:')
我想知道是否有人在 shiny 上使用过此功能,他们是否可以帮助我。
ui.R
library(shiny)
library(readr)
library(memoise)
library(tm)
library(wordcloud)
library(DT)
tesesFO <<- read.csv("TesesFOResumoUnico.csv", colClasses = "character",stringsAsFactors = TRUE, sep = ";")
shinyUI(
fluidPage
(
h1("TesesFO"),
# Adiciona o CSS
tags$style(my_css),
sidebarLayout(
sidebarPanel(
width = 3,
title = "Entrada dados",
hr(),
sliderInput(inputId = "ano", label = "Intervalo de Tempo",
min = 2000, max = 2025, step = 2,
value = c(2010, 2018)),
# Add an "All" value to the continent list
selectInput("depto", "Departamento",
choices = c("Todos", tesesFO$Depto),
multiple = TRUE,
selectize = TRUE,
selected = 'Todos'),
textAreaInput(inputId="texto1",
label ="Digite um texto",
value = "Texto para analise",
height = '200px',
width = '250px'),
hr(),
actionButton("update", "Atualizar"),
hr()
),
mainPanel(
tabsetPanel(
tabPanel(
title = "Tabela de Teses",
br(),
DT::dataTableOutput("table"),
# Add a download button
downloadButton(outputId = "download_data", label = "Download")
),
tabPanel("Cloud",plotOutput("plot",width = "100%"),
sliderInput("max", "Número máximo de palavras:",
min = 1, max = 300, value = 20)
)
#Fecha tabsetPanel
)
) #Fecha mainPanel
) #Fecha Sidebarlayout
) #Fecha e fluidPage
) #Fecha shinyUI
server.R
library(shiny)
shinyServer(function(input, output) {
filtered_data <- reactive ({
data <- tesesFO
data <- subset(
data,
Ano >= input$ano[1] & Ano <= input$ano[2]
)
# Trata a Caixa
if (input$depto != "Todos"){
data <- subset(
data,
Depto ==input$depto
)
}
data
})
getTermMatrix <- memoise(function(book) {
data <- tesesFO
data <- subset(
data,
Ano >= input$ano[1] & Ano <= input$ano[2]
)
# Trata a Caixa
if (input$depto != "Todos"){
data <- subset(
data,
Depto == input$depto
)
}
data
data <- data$ResumoBR
# Separando somente os resumos
resumosBR <- data
#text <- readLines(sprintf("./%s.txt.gz", book),
# encoding="UTF-8")
myCorpus = Corpus(VectorSource(resumosBR))
myCorpus = tm_map(myCorpus, content_transformer(tolower))
myCorpus = tm_map(myCorpus, removePunctuation)
myCorpus = tm_map(myCorpus, removeNumbers)
myCorpus = tm_map(myCorpus, removeWords,
c(stopwords("SMART"), "a","a","à","ainda","além","ambas","ambos","antes","ao","aonde","aos","após","após"
))
myDTM = TermDocumentMatrix(myCorpus,
control = list(minWordLength = 1))
m = as.matrix(myDTM)
sort(rowSums(m), decreasing = TRUE)
})
# Make the wordcloud drawing predictable during a session
wordcloud_rep <- repeatable(wordcloud)
output$plot <- renderPlot({
terms <- reactive({
# Change when the "update" button is pressed...
input$update
data <- tesesFO
data <- subset(
data,
Ano >= input$ano[1] & Ano <= input$ano[2]
)
# Trata a Caixa
if (input$depto != "Todos"){
data <- subset(
data,
Depto %in% input$depto
)
}
data
# ...but not for anything else
isolate({
withProgress({
setProgress(message = "Processing corpus...")
getTermMatrix(data)
})
})
})
v <- terms()
wordcloud_rep(names(v), v, scale=c(4,0.5),
min.freq = 1, max.words=input$max,
colors=brewer.pal(8, "Dark2"))
})
output$table <- DT::renderDataTable(
{
data <- filtered_data()
DT::datatable(data = data, extensions = c('AutoFill','Buttons','FixedHeader'),
options = list(pageLength = 10,autoWidth = TRUE,targets = 5,
language = list(search = 'Procurar:'),
dom = 'Bfrtip',
buttons = list(list(extend = 'colvis', columns = c(1:9)))),
style = 'default',
class = 'table-bordered table-condensed'
)
})
# Create a download handler
output$download_data <- downloadHandler(
filename = "teses_data.csv",
content = function(file) {
# The code for filtering the data is copied from the
# renderTable() function
data <- filtered_data()
# Write the filtered data into a CSV file
write.csv(data, file = "teses.csv", row.names = FALSE,col.names=TRUE, sep=",")
}
)
})
datatable(mtcars,
extensions = "Buttons",
options = list(dom="Bfrtip",
buttons = list(list(extend = "colvis",
text = "Exibir/Ocultar Colunas")),
language = list(paginate =
list('next'="MY_NEXT",
previous="MY_PREVIOUS"))))
@stéphane-laurent 回答的两个小附录。
1) 如果要添加更多按钮,则需要为每个按钮创建单独的子列表。例如:
datatable(
mtcars,
extensions = "Buttons",
options = list(
dom = "Bfrtip",
buttons = list(
list(
extend = "colvis",
text = "Exhibir/Ocultar Columnas"
),
list(
extend = "copy",
text = "Copiar"
)
)
)
)
将生成两个按钮,一个用于处理名称 "Copiar",另一个用于名称为 "Exhibir/Ocultar Columnas" 的列 selection。添加子列表的顺序将决定按钮从左到右填充的顺序。
2) 您可以使用分页选项更改语言并覆盖小部件中的每个文本实例。但您也可以 select 使用语言选项的语言,如下所示:
datatable(
mtcars,
extensions = "Buttons",
options = list(
options = list(
language = list(
url = 'https://cdn.datatables.net/plug-ins/1.10.11/i18n/Spanish.json'
),
dom = "Bfrtip",
buttons = list(
list(
extend = "colvis",
text = "Exhibir/Ocultar Columnas"
),
list(
extend = "copy",
text = "Copiar"
)
)
)
)
我不确定葡萄牙语的 url 是哪个,但我猜只是将“.../Spanish.json”替换为“.../Portuguese.json” ”。奇怪的是,这不会更改按钮标签,因此您将不得不手动执行此操作。
有关此 here 的数据表文档。
我需要更改 R 中按钮的标签 DT::Datatable。
谢谢你的帮助!
DT:Datatable 具有允许某些格式化功能的扩展名,但不会记录所有内容。
它使用按钮扩展来过滤表格中的列。按钮 "search" 我设法更改了属性: language = list(search = 'Procurar:')
我想知道是否有人在 shiny 上使用过此功能,他们是否可以帮助我。
ui.R
library(shiny)
library(readr)
library(memoise)
library(tm)
library(wordcloud)
library(DT)
tesesFO <<- read.csv("TesesFOResumoUnico.csv", colClasses = "character",stringsAsFactors = TRUE, sep = ";")
shinyUI(
fluidPage
(
h1("TesesFO"),
# Adiciona o CSS
tags$style(my_css),
sidebarLayout(
sidebarPanel(
width = 3,
title = "Entrada dados",
hr(),
sliderInput(inputId = "ano", label = "Intervalo de Tempo",
min = 2000, max = 2025, step = 2,
value = c(2010, 2018)),
# Add an "All" value to the continent list
selectInput("depto", "Departamento",
choices = c("Todos", tesesFO$Depto),
multiple = TRUE,
selectize = TRUE,
selected = 'Todos'),
textAreaInput(inputId="texto1",
label ="Digite um texto",
value = "Texto para analise",
height = '200px',
width = '250px'),
hr(),
actionButton("update", "Atualizar"),
hr()
),
mainPanel(
tabsetPanel(
tabPanel(
title = "Tabela de Teses",
br(),
DT::dataTableOutput("table"),
# Add a download button
downloadButton(outputId = "download_data", label = "Download")
),
tabPanel("Cloud",plotOutput("plot",width = "100%"),
sliderInput("max", "Número máximo de palavras:",
min = 1, max = 300, value = 20)
)
#Fecha tabsetPanel
)
) #Fecha mainPanel
) #Fecha Sidebarlayout
) #Fecha e fluidPage
) #Fecha shinyUI
server.R
library(shiny)
shinyServer(function(input, output) {
filtered_data <- reactive ({
data <- tesesFO
data <- subset(
data,
Ano >= input$ano[1] & Ano <= input$ano[2]
)
# Trata a Caixa
if (input$depto != "Todos"){
data <- subset(
data,
Depto ==input$depto
)
}
data
})
getTermMatrix <- memoise(function(book) {
data <- tesesFO
data <- subset(
data,
Ano >= input$ano[1] & Ano <= input$ano[2]
)
# Trata a Caixa
if (input$depto != "Todos"){
data <- subset(
data,
Depto == input$depto
)
}
data
data <- data$ResumoBR
# Separando somente os resumos
resumosBR <- data
#text <- readLines(sprintf("./%s.txt.gz", book),
# encoding="UTF-8")
myCorpus = Corpus(VectorSource(resumosBR))
myCorpus = tm_map(myCorpus, content_transformer(tolower))
myCorpus = tm_map(myCorpus, removePunctuation)
myCorpus = tm_map(myCorpus, removeNumbers)
myCorpus = tm_map(myCorpus, removeWords,
c(stopwords("SMART"), "a","a","à","ainda","além","ambas","ambos","antes","ao","aonde","aos","após","após"
))
myDTM = TermDocumentMatrix(myCorpus,
control = list(minWordLength = 1))
m = as.matrix(myDTM)
sort(rowSums(m), decreasing = TRUE)
})
# Make the wordcloud drawing predictable during a session
wordcloud_rep <- repeatable(wordcloud)
output$plot <- renderPlot({
terms <- reactive({
# Change when the "update" button is pressed...
input$update
data <- tesesFO
data <- subset(
data,
Ano >= input$ano[1] & Ano <= input$ano[2]
)
# Trata a Caixa
if (input$depto != "Todos"){
data <- subset(
data,
Depto %in% input$depto
)
}
data
# ...but not for anything else
isolate({
withProgress({
setProgress(message = "Processing corpus...")
getTermMatrix(data)
})
})
})
v <- terms()
wordcloud_rep(names(v), v, scale=c(4,0.5),
min.freq = 1, max.words=input$max,
colors=brewer.pal(8, "Dark2"))
})
output$table <- DT::renderDataTable(
{
data <- filtered_data()
DT::datatable(data = data, extensions = c('AutoFill','Buttons','FixedHeader'),
options = list(pageLength = 10,autoWidth = TRUE,targets = 5,
language = list(search = 'Procurar:'),
dom = 'Bfrtip',
buttons = list(list(extend = 'colvis', columns = c(1:9)))),
style = 'default',
class = 'table-bordered table-condensed'
)
})
# Create a download handler
output$download_data <- downloadHandler(
filename = "teses_data.csv",
content = function(file) {
# The code for filtering the data is copied from the
# renderTable() function
data <- filtered_data()
# Write the filtered data into a CSV file
write.csv(data, file = "teses.csv", row.names = FALSE,col.names=TRUE, sep=",")
}
)
})
datatable(mtcars,
extensions = "Buttons",
options = list(dom="Bfrtip",
buttons = list(list(extend = "colvis",
text = "Exibir/Ocultar Colunas")),
language = list(paginate =
list('next'="MY_NEXT",
previous="MY_PREVIOUS"))))
@stéphane-laurent 回答的两个小附录。
1) 如果要添加更多按钮,则需要为每个按钮创建单独的子列表。例如:
datatable(
mtcars,
extensions = "Buttons",
options = list(
dom = "Bfrtip",
buttons = list(
list(
extend = "colvis",
text = "Exhibir/Ocultar Columnas"
),
list(
extend = "copy",
text = "Copiar"
)
)
)
)
将生成两个按钮,一个用于处理名称 "Copiar",另一个用于名称为 "Exhibir/Ocultar Columnas" 的列 selection。添加子列表的顺序将决定按钮从左到右填充的顺序。
2) 您可以使用分页选项更改语言并覆盖小部件中的每个文本实例。但您也可以 select 使用语言选项的语言,如下所示:
datatable(
mtcars,
extensions = "Buttons",
options = list(
options = list(
language = list(
url = 'https://cdn.datatables.net/plug-ins/1.10.11/i18n/Spanish.json'
),
dom = "Bfrtip",
buttons = list(
list(
extend = "colvis",
text = "Exhibir/Ocultar Columnas"
),
list(
extend = "copy",
text = "Copiar"
)
)
)
)
我不确定葡萄牙语的 url 是哪个,但我猜只是将“.../Spanish.json”替换为“.../Portuguese.json” ”。奇怪的是,这不会更改按钮标签,因此您将不得不手动执行此操作。
有关此 here 的数据表文档。