Reputation: 8454
I have a shiny app in which I have a table with 10 rows. What I want to achieve is to download a .txt file with a line of 0 and 1 based on the simple logic-if I choose the row I get 1 and if not I get O-. An example of all the rows selected would be:
#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")
)
)
}
Upvotes: 0
Views: 114
Reputation: 84719
The only solution I can see is:
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)
Upvotes: 1