How to refer to reactive element in ui.R in Shiny












1















I am making an app with drag and drop feature using the ShinyDND package. I would like to pass a list from input as a parameter of dragSetUI, a function that needs to be run in ui.R. I tried renderUI and uiOutput, and it almost works, but the dragged elements fail to be dropped in the drop area. As you can see in the example below, nonreactive choices work like charm. I tried creating a reactive object as well as text output, but I could not find documentation on how to refer to these objects in ui.R. Normally one would refer to output$x as "x" in Output, but here, if I add anything between quotes, it reads as string. I am really stuck with this.



library(shiny)
library(shinyDND)

nonreactive_choices<-as.list(c("a","b","c"))

ui <- shinyUI(
mainPanel(
textInput("choices","Put here a,b,c:"),
dragSetUI("drag", textval = "reactive_choices"),
dragSetUI("drag", textval = "choice_rv"),
textOutput("reactive_choices"),
dragSetUI("drag", textval = nonreactive_choices),
dropUI("drop")
)
)

server = shinyServer(function(input, output,session) {

output$reactive_choices<-reactive({
strsplit(input$choices,",")
})

observe({
chlist<-strsplit(input$choices,",")
choice_rv<-reactiveVal(chlist)
})

})

# Run the application
shinyApp(ui = ui, server = server)









share|improve this question























  • I would think you would need an update function that includes session information in order to push reactive choices to the ui. This is common for many shiny widgets but does not seem to have been created for the shinyDND package.

    – Chabo
    Nov 13 '18 at 19:13













  • I once implemeted a similar approach using orderInput() from library(shinyjqui). You might want to have a look at it.

    – ismirsehregal
    Nov 13 '18 at 19:23
















1















I am making an app with drag and drop feature using the ShinyDND package. I would like to pass a list from input as a parameter of dragSetUI, a function that needs to be run in ui.R. I tried renderUI and uiOutput, and it almost works, but the dragged elements fail to be dropped in the drop area. As you can see in the example below, nonreactive choices work like charm. I tried creating a reactive object as well as text output, but I could not find documentation on how to refer to these objects in ui.R. Normally one would refer to output$x as "x" in Output, but here, if I add anything between quotes, it reads as string. I am really stuck with this.



library(shiny)
library(shinyDND)

nonreactive_choices<-as.list(c("a","b","c"))

ui <- shinyUI(
mainPanel(
textInput("choices","Put here a,b,c:"),
dragSetUI("drag", textval = "reactive_choices"),
dragSetUI("drag", textval = "choice_rv"),
textOutput("reactive_choices"),
dragSetUI("drag", textval = nonreactive_choices),
dropUI("drop")
)
)

server = shinyServer(function(input, output,session) {

output$reactive_choices<-reactive({
strsplit(input$choices,",")
})

observe({
chlist<-strsplit(input$choices,",")
choice_rv<-reactiveVal(chlist)
})

})

# Run the application
shinyApp(ui = ui, server = server)









share|improve this question























  • I would think you would need an update function that includes session information in order to push reactive choices to the ui. This is common for many shiny widgets but does not seem to have been created for the shinyDND package.

    – Chabo
    Nov 13 '18 at 19:13













  • I once implemeted a similar approach using orderInput() from library(shinyjqui). You might want to have a look at it.

    – ismirsehregal
    Nov 13 '18 at 19:23














1












1








1








I am making an app with drag and drop feature using the ShinyDND package. I would like to pass a list from input as a parameter of dragSetUI, a function that needs to be run in ui.R. I tried renderUI and uiOutput, and it almost works, but the dragged elements fail to be dropped in the drop area. As you can see in the example below, nonreactive choices work like charm. I tried creating a reactive object as well as text output, but I could not find documentation on how to refer to these objects in ui.R. Normally one would refer to output$x as "x" in Output, but here, if I add anything between quotes, it reads as string. I am really stuck with this.



library(shiny)
library(shinyDND)

nonreactive_choices<-as.list(c("a","b","c"))

ui <- shinyUI(
mainPanel(
textInput("choices","Put here a,b,c:"),
dragSetUI("drag", textval = "reactive_choices"),
dragSetUI("drag", textval = "choice_rv"),
textOutput("reactive_choices"),
dragSetUI("drag", textval = nonreactive_choices),
dropUI("drop")
)
)

server = shinyServer(function(input, output,session) {

output$reactive_choices<-reactive({
strsplit(input$choices,",")
})

observe({
chlist<-strsplit(input$choices,",")
choice_rv<-reactiveVal(chlist)
})

})

# Run the application
shinyApp(ui = ui, server = server)









share|improve this question














I am making an app with drag and drop feature using the ShinyDND package. I would like to pass a list from input as a parameter of dragSetUI, a function that needs to be run in ui.R. I tried renderUI and uiOutput, and it almost works, but the dragged elements fail to be dropped in the drop area. As you can see in the example below, nonreactive choices work like charm. I tried creating a reactive object as well as text output, but I could not find documentation on how to refer to these objects in ui.R. Normally one would refer to output$x as "x" in Output, but here, if I add anything between quotes, it reads as string. I am really stuck with this.



library(shiny)
library(shinyDND)

nonreactive_choices<-as.list(c("a","b","c"))

ui <- shinyUI(
mainPanel(
textInput("choices","Put here a,b,c:"),
dragSetUI("drag", textval = "reactive_choices"),
dragSetUI("drag", textval = "choice_rv"),
textOutput("reactive_choices"),
dragSetUI("drag", textval = nonreactive_choices),
dropUI("drop")
)
)

server = shinyServer(function(input, output,session) {

output$reactive_choices<-reactive({
strsplit(input$choices,",")
})

observe({
chlist<-strsplit(input$choices,",")
choice_rv<-reactiveVal(chlist)
})

})

# Run the application
shinyApp(ui = ui, server = server)






r shiny






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 14:26









kristofkelemenkristofkelemen

154




154













  • I would think you would need an update function that includes session information in order to push reactive choices to the ui. This is common for many shiny widgets but does not seem to have been created for the shinyDND package.

    – Chabo
    Nov 13 '18 at 19:13













  • I once implemeted a similar approach using orderInput() from library(shinyjqui). You might want to have a look at it.

    – ismirsehregal
    Nov 13 '18 at 19:23



















  • I would think you would need an update function that includes session information in order to push reactive choices to the ui. This is common for many shiny widgets but does not seem to have been created for the shinyDND package.

    – Chabo
    Nov 13 '18 at 19:13













  • I once implemeted a similar approach using orderInput() from library(shinyjqui). You might want to have a look at it.

    – ismirsehregal
    Nov 13 '18 at 19:23

















I would think you would need an update function that includes session information in order to push reactive choices to the ui. This is common for many shiny widgets but does not seem to have been created for the shinyDND package.

– Chabo
Nov 13 '18 at 19:13







I would think you would need an update function that includes session information in order to push reactive choices to the ui. This is common for many shiny widgets but does not seem to have been created for the shinyDND package.

– Chabo
Nov 13 '18 at 19:13















I once implemeted a similar approach using orderInput() from library(shinyjqui). You might want to have a look at it.

– ismirsehregal
Nov 13 '18 at 19:23





I once implemeted a similar approach using orderInput() from library(shinyjqui). You might want to have a look at it.

– ismirsehregal
Nov 13 '18 at 19:23












2 Answers
2






active

oldest

votes


















0














Let's see why the renderUI approach does not work with shinyDND. An app using shinyDND is linked to the dragndrop.js file, which is in the shinyDND folder. In this file one can see:



$(document).ready(function(){
......
$(".dragelement").on("dragstart",function(e){
e.originalEvent.dataTransfer.setData("Text",e.target.id);
});
......
});


This defines the action to perform on elements having class dragelement when a drag is starting, and this is defined when the document is ready. dragSetUI creates such elements.



When you use a dragSetUI inside a renderUI, this creates new elements of class dragelement. But the action defined on such elements in $(document).ready is not effective for these new elements, because this action has been defined just after the document is ready, and then before the effect of renderUI.



A solution consists in defining the action of the event dragstart inside the renderUI. This works:



library(shiny)
library(shinyDND)

nonreactive_choices<-as.list(c("a","b","c"))

ui <- shinyUI(
mainPanel(
textInput("choices","Put here d,e,f:"),
dragSetUI("drag", textval = nonreactive_choices),
uiOutput("dragset"),
dropUI("drop")
)
)

server = shinyServer(function(input, output,session) {

reactive_choices <- reactive({
strsplit(input$choices,",")[[1]]
})

output$dragset <- renderUI({
tagList(
dragSetUI("drag2", textval = as.list(reactive_choices())),
tags$script('$(".dragelement").on("dragstart",function(e){
e.originalEvent.dataTransfer.setData("Text",e.target.id);
});'
)
)
})
})

# Run the application
shinyApp(ui = ui, server = server)





share|improve this answer































    0














    Comment by @ismirsehregal helped me find the solution: shinyjqui can be used for my purposes and it seems to work from inside renderUI. Here is the edited code that does exactly what I needed.



    library(shiny)
    library(shinyjqui)


    ui <- fluidPage(
    textInput("choices","Put here a,b,c:"),
    uiOutput("reactiveselect"),
    orderInput(inputId = 'drop', label = 'Reactive drop', items = NULL,placeholder = "drop here..."),
    verbatimTextOutput("droppedorder")
    )


    server <- function(input, output) {



    output$reactiveselect <- renderUI({
    req(input$choices)
    reactiveitems<- unlist(strsplit(input$choices,","))
    orderInput("groupstochoose", "groups to choose from:", connect='drop',items=reactiveitems)

    })

    output$droppedorder<-
    renderPrint({input$drop_order})

    }



    shinyApp(ui, server)





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283202%2fhow-to-refer-to-reactive-element-in-ui-r-in-shiny%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Let's see why the renderUI approach does not work with shinyDND. An app using shinyDND is linked to the dragndrop.js file, which is in the shinyDND folder. In this file one can see:



      $(document).ready(function(){
      ......
      $(".dragelement").on("dragstart",function(e){
      e.originalEvent.dataTransfer.setData("Text",e.target.id);
      });
      ......
      });


      This defines the action to perform on elements having class dragelement when a drag is starting, and this is defined when the document is ready. dragSetUI creates such elements.



      When you use a dragSetUI inside a renderUI, this creates new elements of class dragelement. But the action defined on such elements in $(document).ready is not effective for these new elements, because this action has been defined just after the document is ready, and then before the effect of renderUI.



      A solution consists in defining the action of the event dragstart inside the renderUI. This works:



      library(shiny)
      library(shinyDND)

      nonreactive_choices<-as.list(c("a","b","c"))

      ui <- shinyUI(
      mainPanel(
      textInput("choices","Put here d,e,f:"),
      dragSetUI("drag", textval = nonreactive_choices),
      uiOutput("dragset"),
      dropUI("drop")
      )
      )

      server = shinyServer(function(input, output,session) {

      reactive_choices <- reactive({
      strsplit(input$choices,",")[[1]]
      })

      output$dragset <- renderUI({
      tagList(
      dragSetUI("drag2", textval = as.list(reactive_choices())),
      tags$script('$(".dragelement").on("dragstart",function(e){
      e.originalEvent.dataTransfer.setData("Text",e.target.id);
      });'
      )
      )
      })
      })

      # Run the application
      shinyApp(ui = ui, server = server)





      share|improve this answer




























        0














        Let's see why the renderUI approach does not work with shinyDND. An app using shinyDND is linked to the dragndrop.js file, which is in the shinyDND folder. In this file one can see:



        $(document).ready(function(){
        ......
        $(".dragelement").on("dragstart",function(e){
        e.originalEvent.dataTransfer.setData("Text",e.target.id);
        });
        ......
        });


        This defines the action to perform on elements having class dragelement when a drag is starting, and this is defined when the document is ready. dragSetUI creates such elements.



        When you use a dragSetUI inside a renderUI, this creates new elements of class dragelement. But the action defined on such elements in $(document).ready is not effective for these new elements, because this action has been defined just after the document is ready, and then before the effect of renderUI.



        A solution consists in defining the action of the event dragstart inside the renderUI. This works:



        library(shiny)
        library(shinyDND)

        nonreactive_choices<-as.list(c("a","b","c"))

        ui <- shinyUI(
        mainPanel(
        textInput("choices","Put here d,e,f:"),
        dragSetUI("drag", textval = nonreactive_choices),
        uiOutput("dragset"),
        dropUI("drop")
        )
        )

        server = shinyServer(function(input, output,session) {

        reactive_choices <- reactive({
        strsplit(input$choices,",")[[1]]
        })

        output$dragset <- renderUI({
        tagList(
        dragSetUI("drag2", textval = as.list(reactive_choices())),
        tags$script('$(".dragelement").on("dragstart",function(e){
        e.originalEvent.dataTransfer.setData("Text",e.target.id);
        });'
        )
        )
        })
        })

        # Run the application
        shinyApp(ui = ui, server = server)





        share|improve this answer


























          0












          0








          0







          Let's see why the renderUI approach does not work with shinyDND. An app using shinyDND is linked to the dragndrop.js file, which is in the shinyDND folder. In this file one can see:



          $(document).ready(function(){
          ......
          $(".dragelement").on("dragstart",function(e){
          e.originalEvent.dataTransfer.setData("Text",e.target.id);
          });
          ......
          });


          This defines the action to perform on elements having class dragelement when a drag is starting, and this is defined when the document is ready. dragSetUI creates such elements.



          When you use a dragSetUI inside a renderUI, this creates new elements of class dragelement. But the action defined on such elements in $(document).ready is not effective for these new elements, because this action has been defined just after the document is ready, and then before the effect of renderUI.



          A solution consists in defining the action of the event dragstart inside the renderUI. This works:



          library(shiny)
          library(shinyDND)

          nonreactive_choices<-as.list(c("a","b","c"))

          ui <- shinyUI(
          mainPanel(
          textInput("choices","Put here d,e,f:"),
          dragSetUI("drag", textval = nonreactive_choices),
          uiOutput("dragset"),
          dropUI("drop")
          )
          )

          server = shinyServer(function(input, output,session) {

          reactive_choices <- reactive({
          strsplit(input$choices,",")[[1]]
          })

          output$dragset <- renderUI({
          tagList(
          dragSetUI("drag2", textval = as.list(reactive_choices())),
          tags$script('$(".dragelement").on("dragstart",function(e){
          e.originalEvent.dataTransfer.setData("Text",e.target.id);
          });'
          )
          )
          })
          })

          # Run the application
          shinyApp(ui = ui, server = server)





          share|improve this answer













          Let's see why the renderUI approach does not work with shinyDND. An app using shinyDND is linked to the dragndrop.js file, which is in the shinyDND folder. In this file one can see:



          $(document).ready(function(){
          ......
          $(".dragelement").on("dragstart",function(e){
          e.originalEvent.dataTransfer.setData("Text",e.target.id);
          });
          ......
          });


          This defines the action to perform on elements having class dragelement when a drag is starting, and this is defined when the document is ready. dragSetUI creates such elements.



          When you use a dragSetUI inside a renderUI, this creates new elements of class dragelement. But the action defined on such elements in $(document).ready is not effective for these new elements, because this action has been defined just after the document is ready, and then before the effect of renderUI.



          A solution consists in defining the action of the event dragstart inside the renderUI. This works:



          library(shiny)
          library(shinyDND)

          nonreactive_choices<-as.list(c("a","b","c"))

          ui <- shinyUI(
          mainPanel(
          textInput("choices","Put here d,e,f:"),
          dragSetUI("drag", textval = nonreactive_choices),
          uiOutput("dragset"),
          dropUI("drop")
          )
          )

          server = shinyServer(function(input, output,session) {

          reactive_choices <- reactive({
          strsplit(input$choices,",")[[1]]
          })

          output$dragset <- renderUI({
          tagList(
          dragSetUI("drag2", textval = as.list(reactive_choices())),
          tags$script('$(".dragelement").on("dragstart",function(e){
          e.originalEvent.dataTransfer.setData("Text",e.target.id);
          });'
          )
          )
          })
          })

          # Run the application
          shinyApp(ui = ui, server = server)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 13:09









          Stéphane LaurentStéphane Laurent

          12.7k65392




          12.7k65392

























              0














              Comment by @ismirsehregal helped me find the solution: shinyjqui can be used for my purposes and it seems to work from inside renderUI. Here is the edited code that does exactly what I needed.



              library(shiny)
              library(shinyjqui)


              ui <- fluidPage(
              textInput("choices","Put here a,b,c:"),
              uiOutput("reactiveselect"),
              orderInput(inputId = 'drop', label = 'Reactive drop', items = NULL,placeholder = "drop here..."),
              verbatimTextOutput("droppedorder")
              )


              server <- function(input, output) {



              output$reactiveselect <- renderUI({
              req(input$choices)
              reactiveitems<- unlist(strsplit(input$choices,","))
              orderInput("groupstochoose", "groups to choose from:", connect='drop',items=reactiveitems)

              })

              output$droppedorder<-
              renderPrint({input$drop_order})

              }



              shinyApp(ui, server)





              share|improve this answer




























                0














                Comment by @ismirsehregal helped me find the solution: shinyjqui can be used for my purposes and it seems to work from inside renderUI. Here is the edited code that does exactly what I needed.



                library(shiny)
                library(shinyjqui)


                ui <- fluidPage(
                textInput("choices","Put here a,b,c:"),
                uiOutput("reactiveselect"),
                orderInput(inputId = 'drop', label = 'Reactive drop', items = NULL,placeholder = "drop here..."),
                verbatimTextOutput("droppedorder")
                )


                server <- function(input, output) {



                output$reactiveselect <- renderUI({
                req(input$choices)
                reactiveitems<- unlist(strsplit(input$choices,","))
                orderInput("groupstochoose", "groups to choose from:", connect='drop',items=reactiveitems)

                })

                output$droppedorder<-
                renderPrint({input$drop_order})

                }



                shinyApp(ui, server)





                share|improve this answer


























                  0












                  0








                  0







                  Comment by @ismirsehregal helped me find the solution: shinyjqui can be used for my purposes and it seems to work from inside renderUI. Here is the edited code that does exactly what I needed.



                  library(shiny)
                  library(shinyjqui)


                  ui <- fluidPage(
                  textInput("choices","Put here a,b,c:"),
                  uiOutput("reactiveselect"),
                  orderInput(inputId = 'drop', label = 'Reactive drop', items = NULL,placeholder = "drop here..."),
                  verbatimTextOutput("droppedorder")
                  )


                  server <- function(input, output) {



                  output$reactiveselect <- renderUI({
                  req(input$choices)
                  reactiveitems<- unlist(strsplit(input$choices,","))
                  orderInput("groupstochoose", "groups to choose from:", connect='drop',items=reactiveitems)

                  })

                  output$droppedorder<-
                  renderPrint({input$drop_order})

                  }



                  shinyApp(ui, server)





                  share|improve this answer













                  Comment by @ismirsehregal helped me find the solution: shinyjqui can be used for my purposes and it seems to work from inside renderUI. Here is the edited code that does exactly what I needed.



                  library(shiny)
                  library(shinyjqui)


                  ui <- fluidPage(
                  textInput("choices","Put here a,b,c:"),
                  uiOutput("reactiveselect"),
                  orderInput(inputId = 'drop', label = 'Reactive drop', items = NULL,placeholder = "drop here..."),
                  verbatimTextOutput("droppedorder")
                  )


                  server <- function(input, output) {



                  output$reactiveselect <- renderUI({
                  req(input$choices)
                  reactiveitems<- unlist(strsplit(input$choices,","))
                  orderInput("groupstochoose", "groups to choose from:", connect='drop',items=reactiveitems)

                  })

                  output$droppedorder<-
                  renderPrint({input$drop_order})

                  }



                  shinyApp(ui, server)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 15:34









                  kristofkelemenkristofkelemen

                  154




                  154






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283202%2fhow-to-refer-to-reactive-element-in-ui-r-in-shiny%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python