根据是否在闪亮的应用程序中选择数据表行来打印数字

时间:2018-07-30 22:25:58

标签: r shiny dt

我有一个闪亮的应用程序,其中有一个包含10行的表格。我要实现的是根据简单的逻辑下载带有0和1行的.txt文件-如果我选择获得1的行,否则选择O-。所有选定行的示例为: enter image description here

#ui.r
library(shiny)
library(DT)
library(tidyverse)
navbarPage(
  "Application",
  tabPanel("General",
           sidebarLayout(

             sidebarPanel(
               downloadButton("downloadData2", "Download")
             ),
             mainPanel(

               DT::dataTableOutput("hot3")

             )
           )))
#server.r
  server <- function(input, output,session) {

  hott<-reactive({
    if(is.null(input$hot5_rows_selected)|| is.na(input$hot5_rows_selected)){

      paste("0",collapse = " ")

    }
    else{
      paste("1",collapse = " ")
    }
  })

  output$downloadData2 <- downloadHandler(
    filename = function(){
      paste(input$file, ".txt", sep = "")
    },
    content = function(file) {
      writeLines(paste(hott()
      ), file)
    }
  )

  rt5<-reactive({
    DF=data.frame(
      Id=  1:10,
      stringsAsFactors = FALSE
    )
  })
  output$hot3 <-DT::renderDataTable(

    rt5()%>% 
      rowid_to_column("Row") %>% 
      mutate(Row = ""),
    rownames = FALSE,
    extensions = "Select",
    options = list(
      columnDefs = list(list(className = "select-checkbox", targets = 0, orderable = FALSE)),
      select = list(style = "multi", selector = "td:first-child")
    )

  )
}

1 个答案:

答案 0 :(得分:1)

我唯一看到的解决方案是:

library(shiny)
library(DT)

dat <- data.frame(X = LETTERS[1:10])


ui <- fluidPage(DTOutput("dt"))


server <- function(input, output){

  rowSelected <- reactive({
    x <- numeric(nrow(dat))
    x[input$dt_rows_selected] <- 1
    x
  })

  output$dt <- renderDT(datatable(cbind(id=rowSelected(), dat), 
                        selection = list(mode = "multiple",
                                         selected = (1:nrow(dat))[as.logical(rowSelected())],
                                         target = "row")))

}

shinyApp(ui, server)