浏览和传递文件名的闪亮方法
我正在尝试使用 Shiny 构建一个前端,将一些参数传递给 markdown 文档。我想要传递的一个参数是用户选择的文件名,包括路径。我想使用文件打开类型对话框供用户浏览目录并选择文件。我不需要/想要实际打开文件,这发生在 markdown 文件中,我只想将所选文件的路径和文件名从闪亮的应用程序中传递出来。我使用 fileInput() 进行了设置,但是当然这会打开文件并在临时目录中创建一个副本,并且关联的路径是临时目录而不是原始目录。对此存在一些相关问题,唯一的答案是这是一个与闪亮的基于服务器性质相关的安全问题。同样,我不想打开该文件,只需获取原始路径和名称。关于如何实现这一目标有什么想法吗?这是精简为这个问题的代码......
library(shiny)
ui <- fluidPage(
titlePanel("Input"),
mainPanel(
fileInput(inputId = "rtffile", "Choose RTF File", accept = ".rtf", ),
)
server <- function(input, output, session) {
observe({
filename <<- input$rtffile
})
}
shinyApp(ui = ui, server = server)
I am trying to build a front end using Shiny to pass some parameters to a markdown document. One parameter I want to pass is a user selected file name, including the path. I would like to use a file open type dialogue box for the user to navigate through directories and select the file. I do not need/want to actually open the file, that happens in the markdown file, I just want to pass the path and file name of the selected file out of the shiny app. I have this set up using fileInput() but of course this opens the file and makes a copy in a temp directory and the associated path is to the temp directory not the original directory. There have been some related questions about this and the only answers are that this is a security issue related to the server-based nature of shiny. Again, I don't want to open the file, just grab the original path and name. Any thoughts on how to achieve this? Here's the code stripped down to just this issue ...
library(shiny)
ui <- fluidPage(
titlePanel("Input"),
mainPanel(
fileInput(inputId = "rtffile", "Choose RTF File", accept = ".rtf", ),
)
server <- function(input, output, session) {
observe({
filename <<- input$rtffile
})
}
shinyApp(ui = ui, server = server)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,您无法让网络浏览器为您提供指向文件的路径
用户的本地计算机。
但是,可以获得服务器上文件的路径。
如果服务器和本地机器恰好是同一台,则可以使用例如
shinyFiles
至选择一条路径:
In general, you can’t get a web browser to give you the path to a file on the
user’s local machine.
However, it’s possible to get a path to a file on the server.
If the server and the local machine happen to be the same, you can use e.g.
shinyFiles
topick a path: