在Shiny中获取上传文件的名称作为变量 [英] Get the name of uploaded file as a variable in Shiny

查看:285
本文介绍了在Shiny中获取上传文件的名称作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Shiny应用程序,其中维恩图的一个部分将在上传的文件(由用户完成)后命名。例如,如果有人上传文件ClientXYZ.csv,Venn图中的一部分将被命名为ClientXYZ。



是否可以在Shiny中执行此操作?它没有一个可重复的例子,但你可以抓住输入和名称的文件的名称

$。 b
$ b

  library(shiny)

ui< - fluidPage(

titlePanel文件名),

sidebarLayout(
sidebarPanel(
fileInput('file1','选择文件',
accept = c(
' text / csv',
'text / comma-separated-values',
'.csv'


),
mainPanel(
textOutput(myFileName)




服务器< - 函数(输入,输出){

file_name< - reactive({
inFile< - input $ file 1

if(is.null(inFile))
return(NULL)

return(stringi :: stri_extract_first(str = inFile $ name,regex = 。*(?= \\。)))
})

输出$ myFileName< - renderText({file_name()})

}

运行应用程序
shinyApp(ui = ui,server = server)


I am creating a Shiny App where one of the sections of a Venn Diagram will be named after the uploaded file(done by user). For example, if someone uploads a file ClientXYZ.csv, one section of the Venn diagram will be named "ClientXYZ"

Is it possible to do this in Shiny?

解决方案

Its not clear without a reproducible example, but you can grab the name of the file with input and name.

library(shiny)

ui <- fluidPage(

   titlePanel("Grabbing my file name"),

   sidebarLayout(
      sidebarPanel(
        fileInput('file1', 'Select your file',
                  accept = c(
                    'text/csv',
                    'text/comma-separated-values',
                    '.csv'
                  )
        )
      ),
      mainPanel(
         textOutput("myFileName")
      )
   )
)

server <- function(input, output) {

  file_name <- reactive({
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    return (stringi::stri_extract_first(str = inFile$name, regex = ".*(?=\\.)"))
  })

  output$myFileName <- renderText({ file_name() })

}

# Run the application 
shinyApp(ui = ui, server = server)

这篇关于在Shiny中获取上传文件的名称作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆