【问题标题】:How to save reactive values in Shiny?如何在 Shiny 中保存反应值?
【发布时间】:2020-08-13 10:57:04
【问题描述】:

我想从用户输入生成一个字符串,然后将它用于另一个功能。我尝试使用reactiveValues,但它不起作用。我应该在某处添加observeEvent 以使其工作,还是应该做其他事情?我想不通。

具体来说,我想从这个最终函数生成一个字符串,然后在另一个函数中使用它

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"

更具体地说,我想将输出存储为字符串(我认为现在它正在将其保存为列表):


这是代码

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)

【问题讨论】:

  • 我不明白在问什么。 “保存值”究竟是什么意思,“其他功能”需要这些值?您想以何种方式更改此示例的行为 - 即我们如何测试我们是否制作了“正确”版本?
  • @MrFlick,我添加了一个截图来澄清我的意思。
  • 还在我原来的问题中添加了更多信息。
  • 我再次编辑了我的问题。我想生成一个字符串而不是保存值。我希望我的最终字符串看起来像,例如"LV1 ~ x1+x2+ x3\nLV2 ~ x4+x5+x6"

标签: r shiny reactive


【解决方案1】:

创建一个单独的反应对象,您可以在 renderPrint() 中使用它。例如

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

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

【讨论】:

  • 非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2020-06-24
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
相关资源
最近更新 更多