Как редактировать таблицу с помощью DT и Shiny из загруженного файла? - PullRequest
0 голосов
/ 29 января 2019

Я полагаюсь на фрагмент кода, найденный здесь , чтобы создать приложение Shiny для загрузки таблицы, редактирования таблицы, а затем загрузки таблицы.Мне удалось отредактировать таблицу, которая уже загружена в память (радужную оболочку), но как мне отредактировать таблицу, которая должна быть загружена в Shiny? .

Я пробовал код впо ссылке выше и проверил, что работает.Я также попробовал код ниже, и это тоже работает.Чего мне не удалось добиться, так это преобразовать фрейм данных x в реактивный объект, назначенный загруженному файлу, и соответственно отредактировать все ссылки на x.

# This code works, but lacks a fileinput object 
# and needs to be amended for a reactive dataframe...
library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    fluidRow(
    # ~~ add fileInput("file1", "Choose file") here ~~
    downloadButton("download")
    ),
    fluidRow(
    DT::dataTableOutput('x1')
    )
  ),
  server = function(input, output, session) {
    # Do I make x reactive?
    x = iris
    x$Date = Sys.time() + seq_len(nrow(x))
    output$x1 = DT::renderDataTable(x, selection = 'none', rownames = FALSE, edit = TRUE)

    proxy = dataTableProxy('x1')

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

    output$download <- downloadHandler("example.csv", 
                                       content = function(file){
                                         write.csv(x, file)
                                       },
                                       contentType = "text/csv")

    }
)

Предыдущийпопытки этого привели к ошибкам, в основном из-за недопустимых операций без активного реактивного контекста.

Приведенный ниже код показывает, чего я хочу достичь, но выдает ошибку: "аргумент" expr "отсутствует по умолчанию "

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    fluidRow(
      fileInput("upload", "Choose CSV File",
                multiple = FALSE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),
    downloadButton("download")
    ),
    fluidRow(
    DT::dataTableOutput('x1')
    )
  ),
  server = function(input, output, session) {
    #x = iris

    # In this edited example x is now a reactive expression, dependent on input$upload
    x <- eventReactive({


      # input$file1 will be NULL initially. After the user selects
      # and uploads a file, head of that data file by default,
      # or all rows if selected, will be shown.

     req(input$upload)

      # when reading semicolon separated files,
      # having a comma separator causes `read.csv` to error
      tryCatch(
        {
          x <- read.csv(input$upload$datapath,
                         header = TRUE,
                         sep = ",",
                         stringsAsFactors = TRUE,
                         row.names = NULL)

        },
        error = function(e) {
          # return a safeError if a parsing error occurs
          stop(safeError(e))
         }
      )
    })

    #x$Date = Sys.time() + seq_len(nrow(x))
    output$x1 = DT::renderDataTable(x(), selection = 'none', rownames = FALSE, edit = TRUE)

    proxy = dataTableProxy('x1')

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

    output$download <- downloadHandler("example.csv", 
                                       content = function(file){
                                         write.csv(x(), file)
                                       },
                                       contentType = "text/csv")

    }
)

Ответы [ 2 ]

0 голосов
/ 30 января 2019

Благодаря Стефану и вдохновению на этот связанный вопрос , я думаю, у меня есть ответ.

Ключ должен использовать реактивные значения в качестве обходного пути к DT ::: coerceValue, не любящему реактивные выражения.Я включил verbatimTextOutput, чтобы проиллюстрировать сохраненные изменения в таблице после редактирования таблицы данных.Кнопка загрузки позволяет также загрузить отредактированную таблицу.

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    fluidRow(
      fileInput("upload", "Choose CSV File",
                multiple = FALSE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),
    downloadButton("download")
    ),
    fluidRow(
    DT::dataTableOutput('x1'),
    verbatimTextOutput("print")
    )
  ),
  server = function(input, output, session) {

    # In this edited example x is now a reactive expression, dependent on input$upload

    # Key to the solution is the use of reactiveValues, stored as vals
    vals <- reactiveValues(x = NULL)

    observe({


      # input$upload will be NULL initially. After the user selects
      # and uploads a file, head of that data file by default,
      # or all rows if selected, will be shown.

     req(input$upload)

      # when reading semicolon separated files,
      # having a comma separator causes `read.csv` to error
      tryCatch(
        {
          x <- read.csv(input$upload$datapath,
                         header = TRUE,
                         sep = ",",
                         stringsAsFactors = TRUE,
                         row.names = NULL)

        },
        error = function(e) {
          # return a safeError if a parsing error occurs
          stop(safeError(e))
         }
      )
      # Reactive values updated from x
      vals$x <- x
    })

    output$print <- renderPrint({
      vals$x
    })
    output$x1 = DT::renderDataTable(vals$x, selection = 'none', rownames = FALSE, edit = TRUE)

    proxy = dataTableProxy('x1')

    observeEvent(input$x1_cell_edit, {
      info = input$x1_cell_edit
      str(info)
      i = info$row
      j = info$col + 1
      v = info$value
      # Below is the crucial spot where the reactive value is used where a reactive expression cannot be used
      vals$x[i, j] <<- DT:::coerceValue(v, vals$x[i, j])
      replaceData(proxy, vals$x, resetPaging = FALSE, rownames = FALSE)
    })

    output$download <- downloadHandler("example.csv", 
                                       content = function(file){
                                         write.csv(vals$x, file, row.names = F)
                                       },
                                       contentType = "text/csv")

    }
)
0 голосов
/ 30 января 2019

См. ?eventReactive.

Вы должны сделать:

x <- eventReactive(input$upload, { # 'input$upload' was the "expr missing"
       ......

или

x <- reactive({
        req(input$upload)
        ......
...