如何在R中的DT :: Datatable中更改这些按钮的标签并更改行的颜色?

时间:2018-07-20 14:37:23

标签: r shiny dt

我需要更改R中的按钮DT :: Datatable的标签。 感谢您的帮助! DataTble

DT:从表中获取数据的扩展性信息,主要用于文档的格式。

在美国扩展按钮。否,“搜索”或“发布”:语言=列表(搜索='采购商:')

Queria sabre sealguémjáusou esse recurso so no shine e se po poderia me ajudar。

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=",")
        }
      )

    })

2 个答案:

答案 0 :(得分:3)

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"))))

enter image description here

答案 1 :(得分:0)

@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)您可以使用分页选项更改语言,并覆盖小部件中的每个文本实例。但是您也可以使用language选项选择一种语言,如下所示:

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的数据表文档。