Working with a reactive() dataframe inside eventReactive()?

前端 未结 2 1014
忘掉有多难
忘掉有多难 2021-01-27 04:28

I was hoping someone could provide some guidance on resolving an issue I have. For background, I\'m trying to create an interface for users to upload a csv of news stories, whic

相关标签:
2条回答
  • 2021-01-27 04:55

    I think you should made several changes to make your reactive and the output works.

    • remove the return in the expression of reactive because reactive only captures the last output from the expression.
    • Move all the reactive outside of any function because you can always check if the input$goButton (in your case) is triggered inside reactive function.
    • Use ObserveEvent for the triggering of events (like input$goButton) instead of eventReactive because eventReactive only gives out a value (rather than renders for outputs and keeps them in the scope).
    • Use reactiveValues for any variable (df_sheet) that changes values in ObserveEvent
    • Change df to df_sheet

    Here is the modified server function:

    server <- function(input, output){
      v <- reactiveValues(df_sheet = NULL)
    
      sheet <- reactive({
        infile <- input$datafile
        if (is.null(infile)) {
          # User has not uploaded a file yet
          NULL
        } else {
          tbl = read.csv(infile$datapath, stringsAsFactors = FALSE)
    
          if(!is.null(selected_row_id())) {
            if (selected_row_id() > 0){  
              tbl[selected_row_id(),1] <- input$truefalse
            }
          } 
    
          tbl
        }
      })
    
    
      # selected row ID from data table
      selected_row_id <- reactive({
        input$sheet_rows_selected
      })
    
    
      # reactive dataframe object
      observeEvent(input$goButton, {
    
        v$df_sheet <- sheet()
    
        # selectable data table 
        output$sheet <- renderDataTable({
          v$df_sheet
        }, selection = "single")
    
      }, ignoreNULL = FALSE)
    
    
      # Save when click the button
      observeEvent(input$save, {
    
        # text of article
        output$text<-renderText({v$df_sheet[input$sheet_rows_selected, "lede"]})
    
        # download button
        output$download_data <- downloadHandler(
          filename = "updated_data.csv",
          content = function(file) {
            write.csv(v$df_sheet, file, row.names = FALSE)
          }
        )
    
        write.csv(v$df_sheet, input$save_file, row.names = FALSE)
      })
    
    }
    
    0 讨论(0)
  • 2021-01-27 05:08

    I managed to get this working by storing the data in a reactiveValues, replacing eventReactive() with observeEvent, then updating the data using isolate().

    Working server code

    server <- function(input, output){
      v = reactiveValues(df_sheet = NULL)
      sheet <- reactive({
        infile <- input$datafile
        if (is.null(infile)) {
          # User has not uploaded a file yet
          return(NULL)
        }
        tbl = read.csv(infile$datapath, stringsAsFactors = FALSE)
        v$df_sheet = tbl
        tbl
      })
    
    
    
      # reactive dataframe object
       observeEvent(input$goButton, {
         isolate({
           if(!is.null(selected_row_id())) {
             if (selected_row_id() > 0){  
               v$df_sheet[selected_row_id(),1] <- input$truefalse
             }
           } 
        })
      }, ignoreNULL = FALSE)
    
      # selectable data table 
      output$sheet <- renderDataTable({
        v$df_sheet[,c(1,2)]
      }, selection = "single")
    
      # selected row ID from data table
      selected_row_id <- reactive({
        input$sheet_rows_selected
      })
    
      # text of article
      output$text<-renderText({sheet()[input$sheet_rows_selected, "lede"]})
    
      # Save when click the button
      observeEvent(input$save, {
        write.csv(v$df_sheet, paste('data/',input$save_file, sep=''), row.names = FALSE)
      })
    
      # download button
      output$download_data <- downloadHandler(
        filename = "updated_data.csv",
        content = function(file) {
          write.csv(v$df_sheet, paste('data/', file, sep=''), row.names = FALSE)
        }
      )
    
    }
    
    shinyApp(ui = ui, server = server)
    
    0 讨论(0)
提交回复
热议问题