[英]how to use shinyFileChoose to create an reactive object to load a data.frame
我一直在我的閃亮應用程序中使用fileInput
來上傳文件,然后創建一個反應對象,允許我讀取data.frame並執行其他操作,如子集化或過濾。 但是,我需要獲取文件的絕對路徑以進行另一次計算,而fileUpload
似乎只存儲時間路徑。
這是有效的服務器部分
server = function(input, output) {
options(shiny.maxRequestSize=100*1024^2)
contents <- reactive({
inputFile <- input$fileUpload
if (is.null(inputFile))
return()
read.delim(inputFile$datapath, header = TRUE)
})
# Return filename as another object
file_name <- reactive({
inFile <- input$fileUpload
if (is.null(inFile))
return()
else { print(inFile$name); return(tools::file_path_sans_ext(inFile$name))}
})
output$tabla <- DT::renderDataTable({
if(is.null(contents()))
return()
DT::datatable(contents(),
filter = 'top')
})
但是,我想使用shinyFiles
選項,因為它存儲了真實路徑,我將需要該路徑來加載更多文件我在服務器中嘗試這部分代碼來模仿與fileUpload
相同的行為,但它不起作用
server = function(input, output, session) {
volumes = getVolumes()
volumes <- c(Home = fs::path_home(), "R Installation" = R.home(), getVolumes()())
file_selected <- reactive({
shinyFileChoose(input, "file", roots = volumes, session = session)
return(parseFilePaths(volumes, input$file))
})
contents <- reactive({
if (is.null(file_selected()))
return()
read.delim(file_selected(), header = TRUE)
})
# Reactive function creating the DT output object
output$tabla <- DT::renderDataTable({
if(is.null(contents()))
return()
DT::datatable(contents(),
filter = 'top')
})
我得到一個關於file
必須是字符串或連接的錯誤,如果我使用read.delim(as.character(file_selected()), header = TRUE)
那么錯誤是關於無效的“描述”參數
錯誤有不同的可能原因。 請嘗試以下步驟:
req(input$file)
input$file
是否為NULL
parseFilePaths
函數的$datapath
parseFilePaths
。 根據這個,您的代碼應如下所示:
file_selected <- reactive({
shinyFileChoose(input, "file", roots = volumes, session = session)
req(input$file)
if (is.null(input$file))
return(NULL)
return(parseFilePaths(volumes, input$file)$datapath)
})
希望對你有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.