简体   繁体   中英

How to store an old reactive input in R shiny after removing it

I have the following r shiny application. We first import the library

 library(shiny)

Now we generate the UI

 ui <- fluidPage(
 downloadButton("dl", "Download"),
 textInput("text","word inpput"),
 mainPanel(
 tableOutput("text1")
                      ))

Next we generate the server

  server <- function(input, output) {

    output$text1 <- renderTable({

      df1<-iris
      df1$text <- input$text
      return(df1)
      })
            }

Finally run the app

    shinyApp(ui, server)

We can now add a text to the textbox to dd to the column text in the dataframe output. However, if i remove the word, the dataframe output reverts to its original state owing to reactivity. Is there a way that the change in output can be made permanent so that even if the textinput box value is replaced, the old value reflects along with the new value

Assuming I understand correctly, please test the below and let me know if it solves your problem or what still needs to be achieved:

server <- function(input, output) {

    values <- reactiveValues(df1 = iris)

    observeEvent(req(input$text != ""), {
        values$df1[, paste0("text", ncol(values$df1)-ncol(iris))] <- input$text
    })

    output$text1 <- renderTable({
        return(values$df1)
    })
}

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