【问题标题】:Can I copy the result of an output and use it in an R/Shiny function?我可以复制输出结果并在 R/Shiny 函数中使用它吗?
【发布时间】:2021-06-16 01:53:42
【问题描述】:

我想复制输出结果“我的存档”(上传不带扩展名的文件名)并在函数 (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)

【问题讨论】:

    标签: r shiny output


    【解决方案1】:

    这将读取一个 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)
    

    【讨论】:

      猜你喜欢
      • 2015-10-12
      • 2021-12-01
      • 2013-11-17
      • 2013-07-07
      • 2018-10-04
      • 2013-03-08
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多