r - 从 Shiny 的 DT 行选择中添加 react 性排名列表标签

标签 r shiny dt sortablejs

我正在尝试创建一个 bucket_list,其中参数 label 根据 DT 中的行选择而变化。

这是到目前为止的代码:

library(shiny)
library(DT)
library(sortable)
library(stringr)

nr <- c("1","2","3")
name <- c("John Doe One","John Doe Two","John Doe Three")


shedule <- data.frame(nr,name)

ui <- navbarPage("Hello world!",
                 tabPanel("Drive-thru",

                          DTOutput('shedule'),  # datatable
                          textOutput("selection"), # print label selection 

                          # bucket list #
                          bucket_list(   
                              header = "Drag and drop seleted rows to the correct location",
                              group_name = "bucket_list_group",
                              orientation = "horizontal",

                              add_rank_list(text = "Driver",
                                            labels = textOutput("selection"), # labels from row selection
                                            input_id = "driver"),
                              add_rank_list(text = "Passenger 1",
                                            labels = NULL,
                                            input_id = "passenger_1"),
                              add_rank_list(text = "Passenger 2",
                                            labels = NULL,
                                            input_id = "passenger_2"),
                              add_rank_list(text = "Passenger 3",
                                            labels = NULL,
                                            input_id = "passenger_3")) 
                 ),
                 inverse = TRUE
                 )

server = function(input, output) {

    # Render DT -------------------------------------------
    output$shedule <- DT::renderDataTable(shedule)


    output$selection  <- renderText({
        s <- input$shedule_rows_selected # Selected rows

        # Create label from selected rows ---------------------
        label =  NULL # Where labels will be stored
        for (i in s)
            label <- c(label, paste(shedule$nr[i], word(shedule$name[i],1,2), sep = " - ")) # Create label with code number and first two names of the person

        label})

}

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

感谢您的帮助!

最佳答案

要使您的愿望 list 动态化,您可以创建一个响应式(Reactive)表达式,以便在选择行时创建/存储标签。然后,您可以在您的愿望 list 中引用此reactive。为此,您需要将存储桶列表移至服务器,并在ui 中包含htmlOutput

根据所需的行为,您可能希望根据所选行更改响应式的工作方式。

library(shiny)
library(DT)
library(sortable)
library(stringr)

nr <- c("1","2","3")
name <- c("John Doe One","John Doe Two","John Doe Three")

shedule <- data.frame(nr,name)

ui <- navbarPage("Hello world!",
                 tabPanel("Drive-thru",
                          DTOutput('shedule'),  # datatable
                          textOutput("selection"), # print label selection 
                          htmlOutput("bucketlist")
                 ),
                 inverse = TRUE
)

server = function(input, output) {

  # Render DT -------------------------------------------
  output$shedule <- DT::renderDataTable(shedule)

  # Render bucket list
  output$bucketlist <- renderUI({
    bucket_list(   
      header = "Drag and drop seleted rows to the correct location",
      group_name = "bucket_list_group",
      orientation = "horizontal",
      add_rank_list(text = "Driver",
                    labels = bucketlistlabels(), # labels from row selection
                    input_id = "driver"),
      add_rank_list(text = "Passenger 1",
                    labels = NULL,
                    input_id = "passenger_1"),
      add_rank_list(text = "Passenger 2",
                    labels = NULL,
                    input_id = "passenger_2"),
      add_rank_list(text = "Passenger 3",
                    labels = NULL,
                    input_id = "passenger_3")) 
  })

  # Reactive expression to create labels from rows selected
  bucketlistlabels <- reactive({
    s <- input$shedule_rows_selected # Selected rows

    # Create label from selected rows ---------------------
    label =  NULL # Where labels will be stored
    for (i in s)
      label <- c(label, paste(shedule$nr[i], word(shedule$name[i],1,2), sep = " - ")) # Create label with code number and first two names of the person

    label
  })

  output$selection  <- renderText({
    bucketlistlabels()
  })

}

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

关于r - 从 Shiny 的 DT 行选择中添加 react 性排名列表标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62291389/

相关文章:

java - 安装 R 包时出错

R Shiny Leaflet 标记缩放问题

css - 使用 DT 包根据列的分位数为列的单元格着色,并对任何列执行此操作

r - 如何减少 DT 数据表中的行高

list - 可以给自己添加一个 data.frame 吗?

r - if() 和 ifelse() 函数之间的区别

r - 如何使用 r 中重复的另一个数据帧中的特定列更新数据帧中的新列?

r - R Shiny 应用程序中的传单 map 图例不显示颜色

r - 在 R 中的 Shiny 应用程序中动态绘制 Plotly 图形

jquery - 防止列名在 Shiny 的数据表中换行