Using values from reactive expression R Shiny Files on data frames

I am using Shiny Files to analyse the content from folders and this is the expression I use to obtain the folder path, the list of files in the folder and the length of that list.

folderInput1<- reactive({
    shinyDirChoose(input, 'directory', roots= volumes, session=session,     restrictions=system.file(package='base'))
return(parseDirPath(volumes, input$directory))})

files1 <- reactive({
list.files(path = folderInput1(), pattern = "*.csv", full.names = T)
})

nFiles1 <- reactive({ length(files1() ) })

I'm having trouble while trying to to use this information in data frames. This is what I am trying to do:

folder_df = data.frame(matrix(0,ncol = 4, nrow = nFiles1())) 

I'm getting this error message:

Error in matrix(0, ncol = 4, nrow = nFiles1()) : non-numeric matrix extent

Upvotes: 1

Views: 526

Answers (1)

mRcSchwering
mRcSchwering

Reputation: 894

Ok, so first shinyDirChoose is a reactive. So you do not need to wrap it in a reactive. Then, you obtain the value of this shinyDirChoose via input, input$directory in your case.

Next, extracting the path is a bit weird because it is stored in a list. Here is an example, I hope it makes things clear.

So the value that you were getting for nFiles1() was probably NULL.

ui

library(shiny)
library(shinyFiles)

shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      shinyDirButton("dir", "dirButton", "title", buttonType = "default", class = NULL)
    ),

    mainPanel(
      h3("input$dir"),
      verbatimTextOutput("view"), br(),
      h3("files"),
      verbatimTextOutput("info")
    )
  )
))

server

library(shiny)
library(shinyFiles)

shinyServer(function(input, output) {

  shinyDirChoose(input, "dir", roots = c(home = '~'))
  output$view <- renderPrint(input$dir)

  info <- reactive({
    dash <- .Platform$file.sep
    list.files(path = paste("~", paste(unlist(input$dir$path)[-1], collapse = dash), sep = dash), 
               pattern = "*.csv", full.names = TRUE)
  })
  output$info <- renderPrint(length(info()))

})

Upvotes: 0

Related Questions