繁体   English   中英

如何在 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 的标签。感谢您的帮助! 数据表

O DT:Datatable está com a extensão que permite alguns recursos de formatação, mas não documenta todos。

Ela usa a extensão Buttons para fitrar colunas na tabela。 O botão "search" eu consegui mudar o atributo: language = list(search = 'Procurar:')

Queria saber se alguém já usou esse recurso no slim e se poderia me ajudar。

用户界面

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 

服务器

    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”的列选择。 添加子列表的顺序将决定按钮从左到右填充的顺序。

2) 您可以使用分页选项更改语言并覆盖小部件中的每个文本实例。 但您也可以使用语言选项选择一种语言,如下所示:

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”。 奇怪的是,这不会更改按钮标签,因此您将不得不手动执行此操作。

此处的数据表文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM