问题
I'm having trouble using the "shinyFilesButton()" and "shinyFilesChoose()" functionality within modules in R shiny.
I believe my issue is related to the namespace functions ("ns()") that effectively create new, unique ids within the modules.
Where do I put the ns() call within the shinyFiles functions? How do I handle this issue on the server side?
I've mocked up an example, with code shown below. The app just selects a file and tells you the info on what you selected. Note that currently no ns() calls are used with any shinyFiles functions. (I've tried wrapping the shinyFilesButton()'s id in the ns(), but then it doesn't match with the shinyFileChoose.)
Currently, this app below will show me files, but only in the root directory. I can't delve deeper into other directories. Additionally, the select button will highlight, but nothing will happen when used.
Edit: I've update the code with an attempt at using the namespaces, per suggestions in the comments. I'm using the ns() function in the shinyFilesButton() call (ui side) and nothing in the server side.
Now I can't see any files when using the file chooser.
Any help?
Below is my code for the app.r file:
#App.R
#Demonstrate issues with ShinyFiles and namesspaces in modules
library(shiny)
library(shinyFiles)
source("shinyFiles_module.R")
server <- function(input, output, session) {
#module Way
callModule(sample,
id="testid",
root_dirs=c(root_on_mac="/Users/Ryan/Desktop/"))
}
ui <- fluidPage(
tagList(
h2("Module Way"),
sample_UI(id = "testid",
label = "shiny file test")
)
)
shinyApp(ui = ui, server = server)
And for the module:
#Sample shinyFiles Module
#trying to get File path using ShinyFiles within a Module
library(shiny)
library(shinyFiles)
#Settings UI function:
# Module UI function
sample_UI <- function(id, label = "Shiny file test") {
# Create a namespace function using the provided id
ns <- NS(id)
#begin UI (wrap all input/ouput in ns() call)
tagList(
strong("Selected Location: "), verbatimTextOutput(ns("file_path")),
shinyFilesButton(
id=ns("get_file_path"),
label="Click Here to Select",
title="Select a file",
multiple= FALSE,
buttonType = "default",
class = NULL)
)
}
# Module server function
sample <- function(input,
output,
session,
root_dirs,
id_value) {
shinyFileChoose(input, id="get_file_path", roots=root_dirs, session=session)
output$file_path <- renderPrint({
parseFilePaths(roots=root_dirs, input$get_file_path)
})
}
回答1:
Change your module to this and your program works:
library(shiny)
library(shinyFiles)
#Settings UI function:
# Module UI function
sample_UI <- function(id, label = "Shiny file test") {
# Create a namespace function using the provided id
ns <- NS(id)
#begin UI (wrap all input/ouput in ns() call)
tagList(
strong("Selected Location: "), verbatimTextOutput(ns("file_path")),
shinyFilesButton(
id=ns("get_file_path"),
label="Click Here to Select",
title="Select a file",
multiple= FALSE,
buttonType = "default",
class = NULL)
)
}
# Module server function
sample <- function(input,
output,
session,
root_dirs) {
ns <- session$ns
shinyFileChoose(input, id=ns("get_file_path"), roots=root_dirs, session=session)
output$file_path <- renderPrint({
parseFilePaths(roots=root_dirs, input$get_file_path)
})
}
回答2:
I have the same issue and I resolve it by following Carl's suggestion:
- use ns() in ui
- remove ns() in server
I am using R3.6.1 and it works. In R3.4 it has problem navigate into the subfolders.
library(shiny)
library(shinyFiles)
#Settings UI function:
# Module UI function
sample_UI <- function(id, label = "Shiny file test") {
# Create a namespace function using the provided id
ns <- NS(id)
#begin UI (wrap all input/ouput in ns() call)
tagList(
strong("Selected Location: "), verbatimTextOutput(ns("file_path")),
shinyFilesButton(
id=ns("get_file_path"),
label="Click Here to Select",
title="Select a file",
multiple= FALSE,
buttonType = "default",
class = NULL)
)
}
# Module server function
sample <- function(input,
output,
session,
root_dirs,
id_value) {
shinyFileChoose(input, id="get_file_path", roots=root_dirs, session=session)
output$file_path <- renderPrint({
parseFilePaths(roots=root_dirs, input$get_file_path)
})
}
来源:https://stackoverflow.com/questions/38747129/how-to-use-shinyfiles-package-within-shiny-modules-namespace-issue