Shiny - Where to Place Code

I have an application organized by a file structure - global, library, ui, server, functions.

I found a really cool piece of code which allows me to drag-drop in the UI but it's unclear where I put or structure the functions provided. The following reprex works but I cannot integrate this into my code as I continue to get functions not found errors.

I can't make the labels and rank_list_basic "functions" reactives, or move them to a separate functions for re-use, I'm curious what's going on here and the code below only works when copied to my ui file before the ui <- code. I'm not familiar with this structure represented in the reprex.

Example shiny app with rank list

library(shiny)
library(sortable)

labels <- list(
"one",
"two",
"three"
)

rank_list_basic <- rank_list(
text = "Drag the items in any desired order",
labels = labels,
input_id = "rank_list_basic"
)

ui <- fluidPage(
fluidRow(
column(
width = 12,
tags$h2("Default, multi-drag and swapping behaviour"),
tabsetPanel(
type = "tabs",
tabPanel(
"Default",
tags$b("Exercise"),
rank_list_basic,
tags$b("Result"),
verbatimTextOutput("results_basic")
)
)
)
)
)

server <- function(input, output) {
output$results_basic <- renderPrint({
input$rank_list_basic # This matches the input_id of the rank list
})

}

shinyApp(ui, server)

Hello,

I'm not sure what exactly you are trying to do, but I guess you like to make your reorder list reactive? Here is an example where I use the uiOutput() function to dynamically add the ranked list and fill it with values from a reactive variable that changes by clicking the newly added button. This all just to show you what is possible...

library(shiny)
library(sortable)

ui <- fluidPage(
  fluidRow(
    column(
      width = 12,
      tags$h2("Default, multi-drag and swapping behaviour"),
      tabsetPanel(
        type = "tabs",
        tabPanel(
          "Default",
          tags$b("Exercise"),
          actionButton("addLabel", 'Add label'),
          uiOutput("rankedList"),
          tags$b("Result"),
          verbatimTextOutput("results_basic")
        )
      )
    )
  )
)

server <- function(input, output) {
  
  labels <- reactiveVal(LETTERS[1:3])
  
  observeEvent(input$addLabel, {
    labels(c(labels(), LETTERS[!LETTERS %in% labels()][1]))
  })
  
  output$results_basic <- renderPrint({
    input$rank_list_basic # This matches the input_id of the rank list
  })
  
  output$rankedList = renderUI({
    rank_list(
      text = "Drag the items in any desired order",
      labels = labels(),
      input_id = "rank_list_basic"
    )
  })
  
}

shinyApp(ui, server)

Hope this helps,
PJ

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.