r - 在用户文本输入不起作用后在 R 中执行一个函数
问题描述
我正在尝试调用一个函数来处理用户的请求,然后生成一个 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)
解决方案
您可能正在寻找 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)
推荐阅读
- json - SwiftUI 如何从响应 JSON API 中分离单个数据
- powerbi - 电源查询(powerbi)数据结构:创建横截面-时间数据结构(面板数据)
- python - 在 Python 中绘制 3D 矢量场
- spring - 18+) 错误:405 不支持请求方法“GET”
- mysql - 在比较运算符中更改操作数顺序时出现 MySQL 错误
- android - Android Studio Arctic Fox 更新后构建失败,gradlew 签名报告有错误
- android - 与 com.google.android.youtube.player.YouTubePlayerSupportFragment 的另一个片段重复的 ID、标签 null 或父 ID
- sql - 选择每组列中的最大值
- python - 如何在python中找到函数的傅里叶变换幅度?
- hadoop - 创建表时将字符串转换为配置单元中的时间戳