r - R/Shiny - 如何让工作表名称在上传后自动填写字段?
问题描述
通过输出,我可以上传电子表格并将文件名用作应用程序中某些功能的参考。但是,我不能将此名称“传输”到“checkboxGroupInput”函数(其中显示“Sheet_Name”)。上传后如何使工作表名称自动填写在字段中?谢谢。
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(readxl)
library(tidyverse)
library(readxl)
library(stringr)
ui <- fluidPage(
br(),
fileInput("archive", "Upload file", accept = c(
".xlsx")),
textOutput("my_archive"),
# Itens Selection
checkboxGroupInput("additem", "Select Items", names(BancoEA))
)
server <- function(input, output) {
# Upload Data Sheet
csv <- reactive({
inFile <- input$archive
if (is.null(inFile))
return(NULL)
df<- read.xlsx(inFile$datapath, header=T)
return(df)
})
# Remove Extension Name
output$my_archive <- renderText({
# Test if file is selected
if (!is.null(input$x$datapath)) {
return(sub(".xlsx$", "", basename(input$archive$name)))
} else {
return(NULL)
}
})
}
# App
shinyApp(ui, server)
解决方案
正如@arashHaratian 指出的那样,您可以updateChecGroupInput()
在观察者内部使用根据上传的文件更新选择。
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(readxl)
library(tidyverse)
library(readxl)
library(stringr)
ui <- fluidPage(
br(),
fileInput("archive", "Upload file", accept = c(
".xlsx")),
#textOutput("my_archive"),
# Itens Selection
checkboxGroupInput("additem", "Select Items", choices = NULL)
)
server <- function(input, output, session) {
# Upload Data Sheet
csv <- reactive({
req(input$archive)
inFile <- input$archive
df <- read_xlsx(inFile$datapath)
return(df)
})
#reactive value that will hold the name of the file
reactive_my_path <- reactive({
# Test if file is selected
req(input$archive)
return(sub(".xlsx$", "", basename(input$archive$name)))
})
# output$my_archive <- renderText({
# reactive_my_path()
# })
observe({
input$archive
#update the choices in input$additem
updateCheckboxGroupButtons(session,
"additem",
paste('Column names in:', reactive_my_path()),
choices = names(csv()))
})
}
# App
shinyApp(ui, server)
另一种解决方案是直接renderUI
在服务器uiOutput
内部和 ui 内部呈现输入。
推荐阅读
- ios - 下载音频文件并保存在照片库中,最后播放该音频
- ios - 在滚动视图中滚动时如何更新 SCIChart 的实时数据?
- python - 如何在python中修剪文本和删除额外信息
- python-3.x - 根据另一列设置新列,3 个选项
- webpack - 获取 NextJS withSass() 生成的块的文件名
- c++ - 如何使用 std::vector 返回 WCHAR ** 参数?
- java - 未记录的 API 声纳规则在 sonarqube 6.5 中不起作用?
- types - DAML 是否有 Hoogle 等价物?
- python - 为什么不在我的所有列表上运行这个循环?
- c++ - 如何将二叉树附加到另一个