簡體   English   中英

如何在 Shiny 中保存電抗值?

[英]How to save reactive values in Shiny?

我想從用戶輸入生成一個字符串,然后將其用於另一個 function。 我嘗試使用reactiveValues但它不起作用。 我應該在某處添加一個observeEvent以使其工作,還是應該做其他事情? 我想不通。

具體來說,我想從這個最終的 function 生成一個字符串,然后在另一個 function 中使用它

output$out <- renderPrint({
    invisible(lapply(handler(), function(handle) {
      cat(paste0(paste0(handle(), collapse = " "), "\n"))
    }))
  })

我試過用這個

  values1 <- reactiveValues(invisible(lapply(handler(), function(handle) {
    cat(paste0(paste0(handle(), collapse = " "), "\n"))
    })))

但這沒有用。


我希望我的最終字符串看起來像,例如"LV1 ~ x1+x2+ x3\nLV2 ~ x4+x5+x6"

更具體地說,我想將 output 存儲為字符串(我認為現在它正在將其保存為列表):

在此處輸入圖像描述


這是代碼

library(shiny)
library(lavaan)

newlist <- as.list(c("LV1", "LV2", "x1", "x2", "x3", "x4", "x5", "x6"))

symbol <- as.list(c("=~", "~"))

row_ui <- function(id) {
  ns <- NS(id)
  fluidRow(
    
    column(2,
           uiOutput(ns("ui_placeholder"))),
    column(2,
           uiOutput(ns("ui_placeholder3"))),
    
    column(2, 
           uiOutput(ns("ui_placeholder2")))
  )
} 


row_server <- function(input, output, session) {
 
  return_value <- reactive({paste(input$variable1, input$symbol1, paste(input$variable2, collapse = "+"))})
  ns <- session$ns
  output$ui_placeholder <- renderUI({
   
    selectInput(ns("variable1"), "LV:", choices = c(' ', newlist), selected = NULL)

  })
  
  output$ui_placeholder2 <- renderUI({
    selectInput(ns("variable2"), "Ind:", choices = c(' ', names(HolzingerSwineford1939)), selected = NULL, multiple = TRUE)
  })
  
  output$ui_placeholder3 <- renderUI({
    selectInput(ns("symbol1"), "Type", choices = c(' ', symbol), selected = NULL)
  })
  
  list(return_value = return_value) 
}

ui <- fluidPage(  
  div(id="placeholder"),
  actionButton("addLine", "+ LV"),
  verbatimTextOutput("out"),
  verbatimTextOutput("listout5")
)



server <- function(input, output, session) {
  
  handler <- reactiveVal(list())
  observeEvent(input$addLine, {
    new_id <- paste("row", input$addLine, sep = "_")
    insertUI(
      selector = "#placeholder",
      where = "beforeBegin",
      ui = row_ui(new_id)
    )
    
handler_list <- isolate(handler())
    new_handler <- callModule(row_server, new_id)
    handler_list <- c(handler_list, new_handler)
    names(handler_list)[length(handler_list)] <- new_id
    handler(handler_list)
  })
  
  output$out <- renderPrint({
    invisible(lapply(handler(), function(handle) {
      cat(paste0(paste0(handle(), collapse = " "), "\n"))
    }))
  })
  
}

shinyApp(ui, server)

制作一個單獨的反應式 object ,您可以在您的renderPrint()中使用它。 例如

  outformula <- reactive({
    paste(sapply(handler(), function(handle) {
      paste0(handle(), collapse = " ")
    }), collapse="\n")
  })
  
  output$out <- renderPrint({
    cat(outformula())
  })

然后,您可以在任何需要的地方使用outformula()的值作為字符值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM