根据用户输入写入CSV

时间:2018-10-15 21:43:50

标签: r shiny dt

我想根据行中输入框的选择编写CSV文件。多次选择输入框后,用户只要点击"Write Data"按钮就可以写入CSV文件。

library(shiny)
library(DT)


mymtcars = mtcars
mymtcars$id = 1:nrow(mtcars)
runApp(
  list(ui = pageWithSidebar(
    headerPanel('Examples of Table'),
    sidebarPanel(
      textInput("collection_txt",label="Foo")
    ,
       br(),
   p("Include actionButton to prevent write occuring before user finalises selection"),  
   actionButton("generateButton","Write Data")    
),
    mainPanel(
      DT::dataTableOutput("mytable")
    )
  )
  , server = function(input, output, session) {

      shinyInput <- function(FUN,id,num,...) {
      inputs <- character(num)
      for (i in seq_len(num)) {
        inputs[i] <- as.character(FUN(paste0(id,i),label=NULL,...))
      }
      inputs
    }

    rowSelect <- reactive({

      rows=names(input)[grepl(pattern = "srows_",names(input))]
      paste(unlist(lapply(rows,function(i){
        if(input[[i]]==T){
          return(substr(i,gregexpr(pattern = "_",i)[[1]]+1,nchar(i)))
        }
      })))

    })

    observe({
      updateTextInput(session, "collection_txt", value = rowSelect() ,label = "Foo:" )
      if(input$generateButton == 0){return()}
      input$generateButton
      mtt = mymtcars[rowSelect(),]
      mtt

    showNotification("Done!")
    write.csv(mtt, file = "sol_Deep.csv" , row.names=F)
    })

    output$mytable = DT::renderDataTable({
    DT::datatable(cbind(Flag=shinyInput(checkboxInput,"srows_",nrow(mymtcars),value=NULL,width=1), 
                    mymtcars),   extensions = 'Buttons', options = list(orderClasses = TRUE,
                                   pageLength = 5, lengthChange = FALSE, dom = 'Bfrtip',
                                   buttons = c('copy', 'csv', 'excel'),
                                   drawCallback= JS(
                                     'function(settings) {
                                     Shiny.bindAll(this.api().table().node());}')
                                  ),escape=F)

      }
  )

    })
)

代码返回了NA。我该如何解决?

0 个答案:

没有答案