我对R / Rshiny / RMarkdown相当陌生,我已经采用了一些基本的用户输入模板来构建应用程序。我想要的是让用户输入文件路径,并能够在另一个函数/块中调用文件名和文件路径。
我发现反应式可能是实现此目的的关键功能,但我不确定如何使用它。
library(shinyFiles)
ui <- fluidPage(
shinyFilesButton("Btn_GetFile", "Choose a file" ,
title = "Please select a file:", multiple = TRUE,
buttonType = "default", class = NULL),
textOutput("filename"),
textOutput("txt_file")
)
filepath <- reactiveValues() #trying, but this isn't quite working
server <- function(input,output,session){
volumes = getVolumes()
observe({
shinyFileChoose(input, "Btn_GetFile", roots = volumes, session = session)
if(!is.null(input$Btn_GetFile)){
# browser()
file_selected<-parseFilePaths(volumes, input$Btn_GetFile)
output$txt_file <- renderText(as.character(file_selected$datapath))
output$filename <- renderText(as.character(file_selected$name))
filepath <- reactive({output$txt_file}) #an attempt, but it isn't doing what I want.
}
})
}
shinyApp(ui = ui, server = server)
isolate(filepath) #also tried print. Really just trying to see if filepath is populated
如果用户输入/Users/Jim/tmp.txt,我希望看到UI展示/Users/Jim/tmp.txt和tmp.txt(确实如此),并且还要看到/ Users / Jim / tmp.txt存储为我可以在其他地方访问的文件路径变量。
答案 0 :(得分:1)
看看?reactiveValues
。修改了您的代码,这应该可以工作:
编辑根据您的评论,您可以结合使用system()
软件包尝试glue
命令
library(shinyFiles)
library(glue)
ui <- fluidPage(
shinyFilesButton("Btn_GetFile", "Choose a file" ,
title = "Please select a file:", multiple = TRUE,
buttonType = "default", class = NULL),
textOutput("filename"),
textOutput("txt_file")
)
rvs <- reactiveValues() #renamed to rvs
server <- function(input,output,session){
volumes = getVolumes()
observe({
shinyFileChoose(input, "Btn_GetFile", roots = volumes, session = session)
req(input$Btn_GetFile)
file_selected <- parseFilePaths(volumes, input$Btn_GetFile)
output$txt_file <- renderText(as.character(file_selected$datapath))
output$filename <- renderText(as.character(file_selected$name))
rvs$filepath <- as.character(file_selected$datapath)
print(rvs$filepath) #Added this print to test
})
}
observe({
req(rvs$filepath)
system(glue("echo {rvs$filepath}"))
})
shinyApp(ui = ui, server = server)