如何在 R Shiny 中将无功值设置为默认值?
How to set reactive value to default in R Shiny?
我在更新闪亮的反应值时遇到问题。
所以我的应用基本上做的是保存用户的文本输入。
当用户决定上传所有文本输入时,我想重置 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("")
})
}
如果您尝试这个最小的示例,您将看到文本输入将被重置,
但只有 在 之后再次单击 "Save" 按钮。
但是我希望在单击 "submit_upload" 按钮时重置文本输入。
有人有想法吗?
您可能最好使用某种 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)
我在更新闪亮的反应值时遇到问题。
所以我的应用基本上做的是保存用户的文本输入。 当用户决定上传所有文本输入时,我想重置 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("")
})
}
如果您尝试这个最小的示例,您将看到文本输入将被重置, 但只有 在 之后再次单击 "Save" 按钮。
但是我希望在单击 "submit_upload" 按钮时重置文本输入。
有人有想法吗?
您可能最好使用某种 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)