Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I save the modifications ? #5

Open
pachevalier opened this issue Apr 13, 2015 · 9 comments
Open

How can I save the modifications ? #5

pachevalier opened this issue Apr 13, 2015 · 9 comments

Comments

@pachevalier
Copy link

That's a very nice editor but I don't understand how I can save the modification in an R object.

@timelyportfolio
Copy link
Owner

Hopefully I can get save implemented in the next couple of days. I'll update as I progress.

For reference, are you using in Shiny, or do you want to save to file?

@pachevalier
Copy link
Author

I think it would be nice to save the output as an R object. I would imagine a syntax like :

toto <- jsonedit(list())

where toto is the name of the output.

@jangorecki
Copy link

what is the status of that?

@timelyportfolio
Copy link
Owner

@jangorecki, I think this would be a perfect application of Shiny gadgets, but I hesitate to spend a lot of time on it given this discussion in r-lib/lobstr#2. It is relatively easy to get the return value from jsoneditor.

@webdevdufrain
Copy link

webdevdufrain commented Jun 16, 2016

I've just discovered this lovely package but my use case cannot be fulfilled without being able to save the output after modifications.

I'll confess and say that I don't understand the lobstr thread timelyportfolio has linked to. This issue currently stands open but the indication is that it isn't going to be solved. Would it be possible to get a little more clarity on whether this will be addressed or not?

@timelyportfolio
Copy link
Owner

Just after I submitted to CRAN I added this https://github.com/timelyportfolio/listviewer/blob/master/R/jsonedit_gadget.R. See if it works for you.

@webdevdufrain
Copy link

I've just had a little play with the example at listviewer/inst/examples/examples_gadget.R and yes, so far I seem to be able to capture an output.

It would be interesting if there were a way to "continuously" update a destination object, so each change that was made immediately writes to an object, rather than having to click "Done" and ending the mini shiny session. But for now I think I can do what I need to do.

Thank you very much for this nice development, it'll definitely be useful for me!

@colearendt
Copy link

colearendt commented Aug 9, 2017

Posting a JavaScript workaround I am using in Shiny. Ideally, I think there would be a way to specify an "inputId" on jsoneditOutput that provides an input$jseddata object back to the server side. I would like to hope that there is a cleaner method to implement than what I have. Using a bit of the jsonedit_gadget.R as inspiration, I have come up with the following (which is safe with multiple jsonedit outputs based on the shiny ID).

Note - this example returns the JSON object back to the server at every change, thereby allowing for notifications, warnings, etc. if comparing to a Schema object, etc.

library(shiny)
library(listviewer)
library(shinyjs)

jsCode <- "shinyjs.updatejsed = function(){
var txt = HTMLWidgets.findAll('.jsonedit').filter(function(item){return item.editor.container.id === 'jsed'})[0].editor.getText();
console.log(txt);
Shiny.onInputChange('jseddata',txt)
}"

ui <- shinyUI(
  fluidPage(
    useShinyjs()
    , actionButton('update','Update Data')
    , extendShinyjs(text=jsCode)
    , jsoneditOutput( "jsed" )
    , verbatimTextOutput('jsedtext')
    , jsoneditOutput( 'jsed2')
    , verbatimTextOutput('jsed2text')
  )
)

server <- function(input,output){
  
  observe(print(input$jsed2data))
  output$jsed <- renderJsonedit({
    jsonedit(
      '{"a":[]}'
      ,"onChange" = htmlwidgets::JS('shinyjs.updatejsed'))
  })
  
  observeEvent(input$update, {shinyjs::js$updatejsed()})
  
  output$jsedtext <- renderText(input$jseddata)
  
  output$jsed2 <- renderJsonedit({
    jsonedit(
      '{"b":[]}'
      ,"onChange" = htmlwidgets::JS("() => {
var txt = HTMLWidgets.findAll('.jsonedit').filter(function(item){return item.editor.container.id === 'jsed2'})[0].editor.getText()
console.log(txt)
Shiny.onInputChange('jsed2data',txt)
  }")
    )
    
})
  
  output$jsed2text <- renderText(input$jsed2data)
}

runApp( list( ui = ui, server = server ) )

Happy to help contribute to the resolution of this issue, if there is still interest and a desire to maintain this package (which I also find incredibly useful). Thanks!

EDIT: If using within a shiny module, it looks like using ns may need to be done on generation of the string. Full disclosure: I have not tested

EDIT2: the "onChange" handler unfortunately does not set the initial value of the data (notice that clicking the "Update Button" is required). I have worked around this using shinyjs, although this is far less than ideal. Ideal would be somehow binding the "getText()" results and the "input$jseddata" object.

@timelyportfolio
Copy link
Owner

The new reactjson provides a much better event stream. See lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants