在用户文本输入不起作用后在 R shiny 中执行一个函数

executing a function in R shiny after user text input not working

我正在尝试调用一个函数来处理用户的请求,然后生成一个 excel 文件。出于调试目的简化了代码,但我的想法是我需要能够 运行 单击按钮后的功能,但单击 'replace' 按钮后没有任何反应。

    library(shiny)
    library(DT)
    library(readxl)
    library(writexl)
    library(openxlsx)
    library(plyr)
    library(tidyverse)
    
    ##################
    search_func <- function(user_input) {
    
      
      hfi <- read_excel("path/to/file", col_names = FALSE, sheet = "One")
      
      hfi_cut <- hfi[-c(1:13),]
      
      hfi_cut[2,1] <- user_input
      
      write_xlsx(hfi_cut, path = "path/to/file/hfi_cut.xlsx")
    }
    ########################################
    
    ui <- fluidPage(
      h2("hfi"),
      textInput("query", "Watcha wanna replace it with, homie?"),
      actionButton("action", "replace"),
      downloadButton("dl", "Download")
    )
    
    
    
    server <- function(input, output, session){
      
      
      storage <- eventReactive(input$action, {
        search_func(input$query)
      })
      
      
      output$dl <- downloadHandler(
        filename = function() {paste("Search_Output.xlsx")},
        content = function(file) {file.copy(""path/to/file/hfi_cut.xlsx"", file)}
      )
    }
    
    
    shinyApp(ui = ui, server = server)

您可能正在寻找 observeEvent :

observeEvent(input$action, {
    search_func(input$query)
})

完整的应用程序代码 -

library(shiny)

ui <- fluidPage(
  h2("hfi"),
  textInput("query", "Watcha wanna replace it with, homie?"),
  actionButton("action", "replace"),
  downloadButton("dl", "Download")
)



server <- function(input, output, session){

observeEvent(input$action, {
    search_func(input$query)
  })
  
  
  output$dl <- downloadHandler(
    filename = function() {paste("Search_Output.xlsx")},
    content = function(file) {file.copy("path/to/file/hfi_cut.xlsx", file)}
  )
}


shinyApp(ui = ui, server = server)