เข้าถึงค่าที่ส่งคืนของโมดูล Shiny ที่สร้างแบบไดนามิก

Aug 18 2020

ฉันกำลังมองหาการสร้างแอปพลิเคชันเงางามที่สร้างโมดูลแบบไดนามิก (ผ่าน callmodule) ที่ส่งคืนรูปแบบง่ายๆ ฉันมีปลายหลวม 2 อันซึ่งฉันขอขอบคุณคำแนะนำบางประการเกี่ยวกับโปรด

ประการแรกเมื่อนำหลายรูปแบบมาสู่ผู้ใช้ (ผ่านการคลิกปุ่ม) ค่าของฟอร์มที่แสดงผลก่อนหน้านี้จะเปลี่ยนกลับเป็นค่าเริ่มต้น ฉันจะหยุดพฤติกรรมนี้ได้อย่างไรเพื่อให้ค่ายังคงอยู่กับการเลือกของผู้ใช้

และ 2 ฉันจะเข้าถึงและนำเสนอ 'ทั้งหมด' ค่าจากการเลือกลงใน tibble เดียวที่สามารถแสดงใน tableOutput ได้อย่างไร ฉันได้ใส่ตัวอย่างง่ายๆไว้ด้วยกันด้านล่างโดยใช้การสังเกตเหตุการณ์ ฉันยังลองใช้รูปแบบต่างๆกับ eventReactive แต่ดูเหมือนว่าฉันจะไม่สามารถเข้าถึงเอาต์พุตโมดูลการโทรได้

ขอบคุณล่วงหน้า!

library(shiny)
library(stringr)


gen_r_8_formUI <- function(id){
  
  ns <- NS(id)
  
  tagList(fluidRow(column(width = 4, selectInput(ns("slt_forename"), 'forename', choices = unique(c("john", "paul", "george", "ringo")))),
          column(width = 4, selectInput(ns("slt_surname") , 'surname' , choices = unique(c("lennon", "mccartney", "harrison", "starr"))))))
}

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

  select_values <- reactiveValues(forename = NULL, surname = NULL)  
  observeEvent(input$slt_forename,{select_values$forename <- input$slt_forename}) observeEvent(input$slt_surname, {select_values$surname <- input$slt_surname})
  select_values_all <- reactive({tibble(forename  = select_values$forename, surname = select_values$surname)})
  
  return(list(select_values_all = reactive({select_values_all()})))
}


ui <- fluidPage(
  column(width = 2, actionButton("btn_gen_r_8_form", "GEN R 8 a FORM")),
  column(width = 6, uiOutput("all_ui_forms")),
  column(width = 4, tableOutput("all_form_values_table")))

server <- function(input, output) {
  
  rctv_uis                     <- reactiveValues(all_ui          = list())
  gen_forms                    <- reactiveValues(all_form_values = list())
  output$all_ui_forms <- renderUI({tagList(rctv_uis$all_ui)})
  output$all_form_values_table <- renderTable({all_form_values_rctv()}) observeEvent(input$btn_gen_r_8_form, {
    
    x_id  <- paste( "ns_", str_replace_all(paste(Sys.time()), "-| |:", '') , sep = '')
    gen_forms$all_form_values[[x_id]] <- callModule(module = gen_r_8_form, id = x_id) rctv_uis$all_ui[[x_id]] <- gen_r_8_formUI(id = x_id)

  })
  
  
  all_form_values_rctv <- reactive({
    
    # Question - how to make a tibble with all form values?
    
    # tibble(
    #   forenames = 'all gen_forms$all_form_values forenames', # surnames = 'all gen_forms$all_form_values surnames'
    # )
    
  })
}

shinyApp(ui = ui, server = server)

คำตอบ

1 starja Aug 18 2020 at 04:37

นี่คือวิธีแก้ปัญหาที่ใช้insertUI. มีข้อดีคือองค์ประกอบ UI ที่มีอยู่ยังคงเหมือนเดิม (ไม่มีการรีเซ็ตโมดูลก่อนหน้า) และเพิ่มเฉพาะโมดูลใหม่เท่านั้น ในการกำหนดตำแหน่งที่เพิ่ม UI ให้กำหนด a tags$div(id = "tag_that_determines_the_position")ในUIฟังก์ชัน จากนั้นinsertUIใช้สิ่งนี้เป็นอาร์กิวเมนต์ นอกจากนี้ฉันได้เปลี่ยนแปลงบางสิ่ง:

  • ทำให้โค้ดง่ายขึ้นสำหรับฟังก์ชั่นเซิร์ฟเวอร์โมดูลโดยทั่วไปคุณต้องใช้ไฟล์ reactive
  • การใช้อินเทอร์เฟซโมดูลใหม่ที่มาพร้อมกับ 1.5.0 มันวาว
  • ใช้โครงสร้างข้อมูลปฏิกิริยาที่ง่ายกว่าเล็กน้อย (การซ้อนน้อยลง)
library(shiny)
library(stringr)


gen_r_8_formUI <- function(id){
  
  ns <- NS(id)
  
  tagList(fluidRow(column(width = 4, selectInput(ns("slt_forename"), 'forename', choices = unique(c("john", "paul", "george", "ringo")))),
                   column(width = 4, selectInput(ns("slt_surname") , 'surname' , choices = unique(c("lennon", "mccartney", "harrison", "starr"))))))
}

gen_r_8_form <- function(id){
  moduleServer(
    id,
    function(input, output, session) {
      select_values_all <- reactive({tibble(forename  = input$slt_forename,
                                            surname  = input$slt_surname)}) return(list(select_values_all = reactive({select_values_all()}))) } ) } ui <- fluidPage( column(width = 2, actionButton("btn_gen_r_8_form", "GEN R 8 a FORM")), column(width = 6, tags$div(id = "add_UI_here")),
  column(width = 4, tableOutput("all_form_values_table")))

server <- function(input, output) {
  gen_forms                    <- reactiveValues()
  current_id <- 1
  
  observeEvent(input$btn_gen_r_8_form, { x_id <- paste0("module_", current_id) gen_forms[[x_id]] <- gen_r_8_form(id = x_id) insertUI(selector = "#add_UI_here", ui = gen_r_8_formUI(x_id)) current_id <<- current_id + 1 }) all_form_values_rctv <- reactive({ res <- lapply(reactiveValuesToList(gen_forms), function(current_module_output) { current_module_output$select_values_all()
    })
    
    # prevent to show an error message when the first module is added
    if (length(res) != 0 && !is.null(res[[1]]$forename)) { dplyr::bind_rows(res) } else { NULL } }) output$all_form_values_table <- renderTable({
    all_form_values_rctv()
  })
}

shinyApp(ui = ui, server = server)
MrFlick Aug 18 2020 at 03:56

ฉันคิดว่าคุณต้องการอะไรแบบนี้

  all_form_values_rctv <- reactive({
    dplyr::bind_rows(lapply(gen_forms$all_form_values, function(x) { x$select_values_all()
    }))
  })

คุณได้รวบรวมองค์ประกอบปฏิกิริยาของโมเดลทั้งหมดแล้วgen_forms$all_form_valuesดังนั้นคุณจึงวนซ้ำองค์ประกอบเหล่านั้นและรับค่าปฏิกิริยาจากนั้นรวมตารางทั้งหมดเหล่านั้นเข้าด้วยกัน