DillonHammill / DataEditR

An Interactive R Package for Viewing, Entering Filtering and Editing Data
https://dillonhammill.github.io/DataEditR/
384 stars 40 forks source link

I want use it on my shiny project, but shiny-ini-shiny impossible.. #5

Closed SatCat closed 3 years ago

owbezick commented 4 years ago

@DillonHammill, I second the above idea. The author of the editData package (which provides similar functions) provides a module based approach, which could prove useful for those wanting to implement your GUI within their own shiny applications.

owbezick commented 4 years ago

Further, I have forked a repo to try to add the module functions. Please do let me know if you care to help!

DillonHammill commented 4 years ago

@owbezick, I would be happy to help add support this. In fact, I have a version where I have separated out the shiny components, so it may be easier to modify this version. I will push this to the master branch later today. I will also take a look at editData to see how they do things.

owbezick commented 4 years ago

@DillonHammill Just checking if that version is pushed to the master branch. Thanks.

DillonHammill commented 4 years ago

@owbezick, I figured I would add some new features while making these changes, I should have a working version ready soon for you to test. I will let you know when it is ready.

leungi commented 4 years ago

Thanks for project @DillonHammill 🙏

This is a great light alternative to {DTedit} that I've used regularly, in case you'd like to avoid converting everything over to shiny modules for the short-term, but still make it compatible with shiny.

owbezick commented 4 years ago

@DillonHammill Any updates on the new release?

DillonHammill commented 4 years ago

Sorry @owbezick, I was really busy with other things last week. I should have more time to work on this now. I will try have it ready by the end of the week.

owbezick commented 4 years ago

@DillonHammill Awesome! Thanks so much.

owbezick commented 4 years ago

@DillonHammill Sorry to ask again, I would just like to use it in a presentation I have on the 21st, so the longer I have to work with it the better!

DillonHammill commented 4 years ago

Thanks for the suggestions everyone! I now have a working version of DataEditR that uses a modular shiny design, has a revamped user interface and ships with an RStudio addin. I am still in the process of documenting and testing everything, but a working version is now available on the devel branch if you would like to take it for a spin. I would like to submit this update to CRAN in the coming weeks, any feedback is welcome.

devtools::install_github("DillonHammill/DataEditR@devel")

Basically, the old data_edit() code has been split into three distinct modules for data input (dataInput), data editing (dataEdit) and data output (dataOutput). All these modules are exported by DataEditR so that you can use whichever modules you would like in your own shiny applications. I would recommend consulting the documents for each of the modules before using them in your applications (?dataInput, ?dataEdit and ?dataOutput). Below is an example of a custom shiny application that uses all of these modules (this is basically what data_edit() looks like under the hood now):

library(DataEditR)
library(shiny)

ui <- fluidPage(
  splitLayout(
    dataInputUI("input1"),
    dataOutputUI("output1"),
    actionButton("done", "Done!"),
    cellWidths = c("60%", "20%", "20%")
  ),
  dataEditUI("edit1")
)

server <- function(input, output, session) {

  # DATA INPUT
  data_input <- dataInputServer("input1",
                                read_fun = "read.csv",
                                read_args = list(header = TRUE))

  # DATA EDIT
  data_update <- dataEditServer("edit1",
                                data = data_input)

  # DATA OUTPUT
  dataOutputServer("output1",
                   data = data_update,
                   write_fun = "write.csv",
                   write_args = list(row.names = FALSE))

  # DATA RETURN
  observeEvent(input$done, {
    stopApp(data_update())
  })

}

shinyApp(ui, server)

There is a lot more new functionality coming as well, but I will document this in the new vignettes. I haven't had much time to test the new version as yet so there are likely to be some issues - please report any major issues that you encounter. I will work to resolve these over the coming days.

andresrcs commented 4 years ago

Hi, How can I programmatically update the values on the dataEditUI()? I want to use it as an input template to add records to a database, so after adding a record I would like to get back to the original template, Is there something like a updateDataEditUI() function?

DillonHammill commented 4 years ago

@andresrcs, I am working on this as we speak. I am playing around with some data filtering UIs and tackling synchronisation issues. Can you elaborate a bit more on what you hope to achieve? You can pass data through the data argument in dataEditServer...

andresrcs commented 4 years ago

I want to use a Shiny app as a GUI for adding records to a database, so I want to show an empty template with dataEditUI() for the user to fill, then after saving the new record to the database I want to return to an empty template so the user can add the next record.

Maybe with some code to exemplify this will make more sense:

library(shiny)
library(DataEditR)
library(tidyverse)
library(odbc)

con <- dbConnect(odbc::odbc(), .connection_string = connection_string)

input_template <- data.frame(
    Variable = c("Variable 1", "Variable 2"),
    Value = NA_integer_
)

ui <- fluidPage(
    dataEditUI("edit"),
    actionButton("save", "Save")
)

server <- function(input, output) {

    data_update <- dataEditServer("edit",
                                  data = input_template)

    observeEvent(input$save, {

        DBI::dbAppendTable(conn = con,
                           name = 'some_table',
                           value = data_update() %>%
                               spread(Variable, Value)
        )
        # I would like to do something like this (like you can with other input elements on Shiny).
        updateDataEditUi("edit", data = input_template)
    })
}

shinyApp(ui = ui, server = server)
DillonHammill commented 4 years ago

Thanks, looks like this is pretty straightforward to implement. I will add support for this later today.

DillonHammill commented 4 years ago

@andresrcs, just wanted to point out that you can also achieve this using reactiveValues as below:

library(shiny)
library(DataEditR)
library(tidyverse)
library(odbc)

con <- dbConnect(odbc::odbc(), .connection_string = connection_string)

ui <- fluidPage(
  dataEditUI("edit"),
  actionButton("save", "Save")
)

server <- function(input, output) {

  # DATA TEMPLATE
  data_template <- data.frame(
    Variable = c("Variable 1", "Variable 2"),
    Value = NA_integer_
  )

  # DATA STORAGE
  values <- reactiveValues(x = data_template)

  # DATA EDIT
  data_update <- dataEditServer("edit",
                                data = reactive(values$x))

  # DATA SAVE & RELOAD
  observeEvent(input$save, {

    DBI::dbAppendTable(conn = con,
                       name = 'some_table',
                       value = data_update() %>%
                         spread(Variable, Value)
    )

    values$x <- NULL
    values$x <- data_template

  })
}

shinyApp(ui = ui, server = server)
andresrcs commented 4 years ago

Thanks for the walk-around (and for a great package!), although, I still think that an "update" function would be more intuitive since it would match the pattern of other input elements, I hope you are still considering implementing it.

DillonHammill commented 4 years ago

@andresrcs, after looking into this, I am not sure that the update* (using sendInputMessage() or sendCustomMessage()) approach will work here. I haven't seen anything cases where inputs to modules can be altered without using the reactiveValues approach that described above. Not sure if anyone else has tried to do this before? If there is a way, I would be happy to implement it...

DillonHammill commented 3 years ago

Closing since the original issue has been addressed in the new version of DataEditR. Feel free to open a new issue if you encounter any problems.