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

Print a number based on the selection or not of datatable row in a shiny app

我有一个闪亮的应用程序,其中有一个 table 有 10 行。我想要实现的是根据简单逻辑下载一个包含一行 0 和 1 的 .txt 文件——如果我选择该行,我得到 1,否则我得到 O-。选择的所有行的示例是:

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

  )
}

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

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)