简体   繁体   中英

How to save reactive values in Shiny?

I would like to generate a string from user inputs and then use it for another function. I tried using reactiveValues but it isn't working. Should I add an observeEvent somewhere to make it work, or should I do something else? 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

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"

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):

在此处输入图像描述


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() . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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