简体   繁体   English

如何在 Shiny 中保存电抗值?

[英]How to save reactive values in Shiny?

I would like to generate a string from user inputs and then use it for another function.我想从用户输入生成一个字符串,然后将其用于另一个 function。 I tried using reactiveValues but it isn't working.我尝试使用reactiveValues但它不起作用。 Should I add an observeEvent somewhere to make it work, or should I do something else?我应该在某处添加一个observeEvent以使其工作,还是应该做其他事情? I couldn't figure it out.我想不通。

Specifically, I would like to generate a string from this final function and use it in another function later具体来说,我想从这个最终的 function 生成一个字符串,然后在另一个 function 中使用它

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

I tried using this我试过用这个

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

But this didn't work.但这没有用。


I would like my final string to look like, for example "LV1 ~ x1+x2+ x3\nLV2 ~ x4+x5+x6"我希望我的最终字符串看起来像,例如"LV1 ~ x1+x2+ x3\nLV2 ~ x4+x5+x6"

To be more specific, I would like to store the output as a string (right now it is saving it as a list, I think):更具体地说,我想将 output 存储为字符串(我认为现在它正在将其保存为列表):

在此处输入图像描述


Here's the code这是代码

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)

Make a separate reactive object that you can use inside your renderPrint() .制作一个单独的反应式 object ,您可以在您的renderPrint()中使用它。 For example例如

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

Then you can use the value of outformula() as a character value where ever you need it.然后,您可以在任何需要的地方使用outformula()的值作为字符值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM