0

I have been reviewing all sorts of documents all day but I do not see a clear solution to my problem. The issue I am having is that I want to bring in a file locally (.xlsx) into the shiny rmarkdown file. Once the app is hosted, users will be able to go in and edit the dataframe and upon exiting the app, the updated dataframe will reflect the changes made my the user.

---
title: "Component Health Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
library(flexdashboard)
library(tidyverse)
library(readxl)
```

Dashboard
=======================================================================

## Table
```{r cars, echo= F}
cars<-read_xlsx("cars.xlsx")

DT::renderDataTable(cars, editable = T,rownames = F)
```

I have referenced this question r shiny: updating rhandsontable from another rhandsontable & Using Shiny to update dataframe values and access it in the local environment after Shiny session ends, but it only seems to work when a DF is in the global environment for a UI/Server. I feel like there is a much better way of doing this. Any help in pushing me into the correct direction or other idea is greatly appreciated.

Phil
  • 7,287
  • 3
  • 36
  • 66

1 Answers1

0

Instead of using flex_dashboard, let me suggest you use shiny, something like the following.

library(shiny)
library(DT)

cars <- data.frame(rbind(c(1,2,3), c(4,5,6), c(7,8,9)))

ui <- fluidPage(
        mainPanel(
          HTML("<br><br><b>Double click on a cell in this table. Enter a number.</b><br><br>"),
          DTOutput("table1"), 
          HTML("<br><br><b>See the change reflected in this table.</b><br><br>"),
          DTOutput("table2")
        )
)

server <- function(input, output) {

  RV <- reactiveValues(data = NULL)
  RV$data <- cars
  
  observeEvent(input$table1_cell_edit, {
    row  <- input$table1_cell_edit$row
    clmn <- input$table1_cell_edit$col + 1
    val  <- input$table1_cell_edit$value
    RV$data[row, clmn] <- as.numeric(val)
  })
  
  output$table1 <- renderDT({ 
    datatable(RV$data, options = list(dom = 't'), editable = TRUE, rownames = F)
  })
  
  output$table2 <- renderDT({ 
    datatable(RV$data,  options = list(dom = 't'), editable = FALSE, rownames = F)
  })
  
}

shinyApp(ui = ui, server = server)

YLC
  • 104
  • 1
  • 5
  • When the user closes the app however, the numbers will reset. I want to be able to close the app and the numbers are stored. – Bills_Mafia_17 Feb 06 '23 at 20:35
  • You have a couple of ways to do this: 1) a button to save RV$data before closing the app. 2) add a line inside observeEvent to save RV$data to a location of your choice whenever RV$data is updated. – YLC Feb 06 '23 at 21:55
  • A third way to accomplish your quest ... https://stackoverflow.com/questions/33235473/shiny-server-how-to-use-sessiononsessionended – YLC Feb 07 '23 at 13:38