如何在 Shiny 模块中使用 ShinyFiles 包 - 命名空间问题? [英] How to use shinyFiles package within Shiny Modules - Namespace Issue?

查看:43
本文介绍了如何在 Shiny 模块中使用 ShinyFiles 包 - 命名空间问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 闪亮的模块中使用shinyFilesButton()"和shinyFilesChoose()"功能时遇到问题.

I'm having trouble using the "shinyFilesButton()" and "shinyFilesChoose()" functionality within modules in R shiny.

我相信我的问题与命名空间函数(ns()")有关,这些函数在模块中有效地创建了新的、唯一的 id.

I believe my issue is related to the namespace functions ("ns()") that effectively create new, unique ids within the modules.

我应该将 ns() 调用放在 ShinyFiles 函数中的什么位置?我如何在服务器端处理这个问题?

我模拟了一个示例,代码如下所示.该应用程序只选择一个文件并告诉您有关您选择的内容的信息.请注意,目前没有 ns() 调用与任何 ShinyFiles 函数一起使用.(我试过在 ns() 中包装 ShinyFilesButton() 的 id,但它与 ShinyFileChoose 不匹配.)

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.)

目前,下面的这个应用程序会显示文件,但只在根目录中.我无法深入研究其他目录.此外,选择按钮将突出显示,但使用时不会发生任何事情.

我已经根据评论中的建议更新了代码,尝试使用命名空间.我在 ShinyFilesButton() 调用(ui 端)中使用 ns() 函数,而在服务器端没有使用.

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.

有什么帮助吗?

以下是我的 app.r 文件代码:

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)

对于模块:

#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)
  })
}

推荐答案

把你的模块改成这个,你的程序就可以工作了:

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)
  })
}

这篇关于如何在 Shiny 模块中使用 ShinyFiles 包 - 命名空间问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆