加载,编辑和下载闪亮的csv文件

时间:2016-11-02 04:09:46

标签: r shiny yui-datatable

我正在尝试将csv文件加载到闪亮的程序中,对其进行编辑并下载已编辑的文件。我发现包DT提供了相同的功能。我修改了example here以从文件中读取csv并example here来编写输出。我还尝试使用DT的实验性功能here来编辑加载的CSV。我的下面的代码加载了csv,并保存了CSV,但在尝试编辑它时,它崩溃并出现错误:

Warning: Error in <<-: invalid (NULL) left side of assignment
Stack trace (innermost first):
65: observeEventHandler [/home/user/ShinyApp/EditingValues/server.R#40]

基本上,我的问题我认为我无法通过example here中的读取csv为x提供值。

我的ui.R

fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose file to upload',
                accept = c(
                  'text/csv',
                  'text/comma-separated-values',
                  'text/tab-separated-values',
                  'text/plain',
                  '.csv',
                  '.tsv'
                )
      ),
      tags$hr(),
      checkboxInput('header', 'Header', TRUE),
      radioButtons('sep', 'Separator',
                   c(Comma=',',
                     Semicolon=';',
                     Tab='\t'),
                   ','),
      radioButtons('quote', 'Quote',
                   c(None='',
                     'Double Quote'='"',
                     'Single Quote'="'"),
                   '"'),
      tags$hr(),
      p('Once you are done download the  data'),
      downloadButton('x3', 'Download Data')
    ),
    mainPanel(
        DT::dataTableOutput('x1')
    )
  )
)

我的server.R

# By default, the file size limit is 5MB. It can be changed by
# setting this option. Here we'll raise limit to 9MB.

# Install DT, experimental version may not work well 
# devtools::install_github('rstudio/DT@feature/editor')

library(shiny) # The main program
library(DT) # Edit the output
library(data.table) # Faster read and write

options(shiny.maxRequestSize = 9*1024^2)

function(input, output, session) {
   dataframe<-reactive({
        if (is.null(input$file1))
            return(NULL)  
       dta_types = fread(input$file1$datapath, header = input$header,
                         sep = input$sep, quote = input$quote, nrows = 0,
                         strip.white = TRUE, data.table = FALSE)
        data<-fread(input$file1$datapath, header = input$header,
                    sep = input$sep, quote = input$quote,
                    stringsAsFactors = FALSE, colClasses= rep("character", ncol(dta_types)))
        data
    })

  output$x3 = downloadHandler('mtcars-filtered.csv', content = function(file) {
      s = input$x1_rows_all
      write.csv(dataframe()[s, , drop = FALSE], file)
  })
  output$x1 = DT::renderDataTable(dataframe(), selection = 'none')

  proxy = dataTableProxy('x1')

  observeEvent(input$x1_cell_edit, {
      info = input$x1_cell_edit
      str(info)
      i = info$row
      j = info$col
      v = info$value
      dataframe()[i, j] <<- DT:::coerceValue(v, dataframe()[i, j])
      replaceData(proxy, dataframe(), resetPaging = FALSE)
  })
}

如何实现这一目标?我确信我错过了某处的简单任务。我试过了

x=dataframe() 

之前

输出$ x1 = DT :: renderDataTable(dataframe(),selection ='none')

并使用x而不是dataframe()到处但是在加载之前崩溃了应用程序。感谢您的投入。 使用rhandsontable的{​​{3}}问题也没有答案。

0 个答案:

没有答案