【问题标题】:How to set reactive value to default in R Shiny?如何在 R Shiny 中将反应值设置为默认值?
【发布时间】:2020-06-24 06:06:51
【问题描述】:

我在更新闪亮的反应值时遇到问题。

所以我的应用程序基本上所做的就是保存来自用户的 textInputs。 当用户决定上传所有文本输入时,我想重置 textInputs。

以下示例代码:

ui.R

ui <- fluidPage(
  sidebarPanel(
    textInput("words", "Please enter a word"), 

    actionButton("submit_new_word", "Save"), # this submits each single word
    textOutput("submitted_new_words"), # this show all submitted words

    actionButton("submit_upload", "Upload my Results") # this uploads all submitted words
  )
)

server.R

server <- function(input, output, session) {
  words_submitted <- paste("") # initial value 

  w_submitted <- eventReactive(input$submit_new_word, {
    words_submitted <- paste(words_submitted, " ", input$words)
    words_submitted <<- words_submitted

    updateTextInput(session, 
                    inputId = "words",
                    value = "") 

    return(words_submitted)
  }, ignoreNULL=FALSE)

  output$submitted_new_words <- renderText({
    w_submitted()
  })

  observeEvent(input$submit_upload, {
    # saveData(data_final) # upload, not needed for example here

    words_submitted <<- paste("")
  })
}

如果您尝试这个最小的示例,您会看到文本输入将被重置, 但只有在之后再次点击“保存”按钮。

但是,我希望在单击“submit_upload”按钮时重置文本输入。

有人有想法吗?

【问题讨论】:

    标签: r shiny shiny-reactivity


    【解决方案1】:

    您可能最好使用某种reactive。闪亮的工作方式是,如果没有附加反应性,它不会使客户端上的任何内容失效(刷新),例如renderText

    library(shiny)
    
    ui <- fluidPage(
            sidebarPanel(
                    textInput("words", "Please enter a word"), 
    
                    actionButton("submit_new_word", "Save"), # this submits each single word
                    textOutput("submitted_new_words"), # this show all submitted words
    
                    actionButton("submit_upload", "Upload my Results") # this uploads all submitted words
            )
    )
    
    v <- reactiveValues()
    
    server <- function(input, output, session) {
            v$words_submitted <- paste("") # initial value 
    
            observeEvent(input$submit_new_word, {
                    v$words_submitted <- paste(v$words_submitted, " ", input$words)
                    updateTextInput(session, inputId = "words",value = "") 
            }, ignoreNULL=FALSE)
    
            output$submitted_new_words <- renderText({
                    v$words_submitted
            })
    
            observeEvent(input$submit_upload, {
                    # saveData(data_final) # upload, not needed for example here
                    v$words_submitted <- paste("")
            })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2019-06-23
      • 2016-04-11
      • 2021-09-27
      • 2021-06-26
      • 2017-05-20
      • 2016-02-13
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多