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
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)