【问题标题】:executing a function in R shiny after user text input not working在用户文本输入不起作用后在 R 中执行一个函数
【发布时间】:2021-07-01 01:44:14
【问题描述】:

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

    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)

【问题讨论】:

    标签: r shiny shinyapps shiny-reactivity


    【解决方案1】:

    您可能正在寻找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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多