Shiny, the web application framework for R, allows you to create interactive applications easily. One of the most common functionalities that you may want to add to your Shiny app is the ability to select a directory. This can be useful in many scenarios, such as uploading files from a directory, processing files in a directory, or saving output to a directory. In this article, we will discuss how to add directory selection functionality in R Shiny app.
Prerequisites
Before you start, make sure that you have the following:
- R installed
- RStudio installed
- Shiny package installed
You can install the Shiny package from the R console by running the following command:
install.packages("shiny")
Creating a Basic Shiny App
Before we add the directory selection functionality, let's first create a basic Shiny app. A Shiny app consists of two parts: the user interface (UI) and the server. The UI is created using the fluidPage() function, and the server is created using the function(input, output) {...} function. Here is an example of a basic Shiny app:
library(shiny)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
actionButton("button", "Click me!")
),
mainPanel(
textOutput("text")
)
)
)
server <- function(input, output) {
output$text <- renderText({
paste("You have clicked the button", input$button, "times.")
})
}
shinyApp(ui, server)
In this example, we have a button in the sidebar that says "Click me!". When you click the button, the number of times you have clicked the button is displayed in the main panel. Now, let's add the directory selection functionality to this app.
Adding Directory Selection Functionality
To add directory selection functionality to a Shiny app, we can use the shinyFiles package. The shinyFiles package provides a set of functions to add file selection and directory selection functionality to Shiny apps. Here is an example of how to add directory selection functionality to the basic Shiny app:
library(shiny)
library(shinyFiles)
ui <- fluidPage(
titlePanel("Directory Selection in Shiny"),
sidebarLayout(
sidebarPanel(
shinyFilesButton("dir", "Select Directory", "Select a directory")
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
volumes <- c("", "/")
on.exit(setSessionVolumes(volumes))
sessionVolumes() <- volumes
shinyFileChoose(input, "dir", roots = c(wd = "."))
output$table <- renderTable({
if (is.null(input$dir))
return()
files <- list.files(input$dir, full.names = TRUE)
head(files, 10)
})
}
shinyApp(ui, server)
In this example, we have added a directory selection button to the sidebar. When you click the button, a file selection dialog will be displayed. You can select a directory from the file selection dialog. The selected directory will be displayed in the main panel. The shinyFilesButton() function is used to add the directory selection button to the UI. The shinyFileChoose() function is used to display the file selection dialog. The list.files() function is used to display the files in the selected directory. The input$dir variable contains the path to the selected directory. The is.null(input$dir) condition is used to check if a directory has been selected. If no directory has been selected, the function returns an empty table. If a directory has been selected, the function displays the files in the selected directory.
In this article, we have discussed how to add directory selection functionality to R Shiny apps. We have discussed the prerequisites for adding directory selection functionality, and we have provided an example of a basic Shiny app and an example of a Shiny app with directory selection functionality. We have also discussed the functions used to add directory selection functionality to Shiny apps. With this knowledge, you can now add directory selection functionality to your Shiny apps.
References
| Reference | Link |
|---|---|
| Shiny | https://shiny.rstudio.com/ |
| shinyFiles | https://rdrr.io/cran/shinyFiles/man/shinyFiles.html |