我可以复制输出结果并在 R/Shiny 函数中使用它吗?

Can I copy the result of an output and use it in an R/Shiny function?

我想复制输出结果“我的存档”(不带扩展名的上传文件名)并在函数 ( name( ) ) 中使用它来从电子表格中读取列标签。有可能吗?

感谢您的帮助。

代码:


    library(shiny)
ui <- fluidPage(
    
    fileInput("archive", "upload file", accept = c(
        ".xlsx")),

    textOutput("my_archive"),
    textOutput("top"))


server <- function(input, output) {
    
    
    csv <- reactive({
        inFile <- input$archive
        if (is.null(inFile))
            return(NULL)
        df<- read.xlsx(inFile$datapath, header=T)
        return(df)
    })
    
    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)
        }
    })
    
    output$top <- 
        renderPrint({
            names("output$my_archive")
        })
    
}


shinyApp(ui, server)

这将读取一个 xlsx 文件并输出不带扩展名的文件名和列名。

library(shiny)
library(tidyverse)
library(readxl)
library(stringr)

ui <- fluidPage(
    fileInput("archive", "upload file", accept = c(
        ".xlsx")),
    
    textOutput("my_archive"),
    textOutput("top"))
)

server <- function(input, output, session) {
    #read the file
    csv <- reactive({
        req(input$archive)
        inFile <- input$archive
        df<- read_xlsx(inFile$datapath)
        print(df)
        return(df)
    })
    
    #name of the file without extension
    output$my_archive <- renderText({
        # Test if file is selected
        if (!is.null(input$archive)) {
            return(str_replace(input$archive$name, '\.xlsx', ' ') )
        } else {
            return(NULL)
        }
    })
    
    #column names
    output$top <- 
        renderText({
            names(csv())
        })
}

shinyApp(ui, server)