R:在闪亮的服务器文件中使用命名空间?

R: using namespace in server file in shiny?

提问人:Wang 提问时间:12/26/2022 最后编辑:Wang 更新时间:12/28/2022 访问量:125

问:

在我的 shinyApp 中,我需要根据用户的输入(服务器)更新一些输入(在 UI 中)。

如果我不在服务器文件中使用命名空间,则代码不起作用。当我引入命名空间时,它按预期工作。

以下是我的最小示例:

该应用程序允许用户删除或撤消其上传数据的某些列。为了使示例更简单,我在这里使用了默认数据集。

注意:我在这段代码中添加了行号来帮助解释代码。
您可以复制另一个没有行号的相同代码以在您的 PC 中运行。
请参阅第 18 行和第 33 行,我在其中添加了命名空间。
server file

我的问题是:在服务器文件中使用命名空间可以吗?我的方式是否正确,是否有其他建议/正确的方法?

1. choice_ui <- function(id) {
2.   ns <- NS(id)
3.   tagList(
4.     sidebarPanel(
5.     uiOutput(outputId = ns("selectColumn"), label = "Select Columns to delete"),
6.     actionButton(inputId = ns("removeCol"), label = "Remove"),
7.     actionButton(inputId = ns("undoCol"), label = "Undo")
8.     ),
9.     mainPanel(
10.     dataTableOutput(outputId = ns("rawView")),
11.     dataTableOutput(outputId = ns("processView")),
12.     textOutput(outputId = ns("count"))
13.     )
14.   )
15. }
16. 
17. choice_server <- function(id, r) {
18.   ns <- NS(id) ## use namespace here
19.   moduleServer(id, function(input, output, session){
20.       inputData <- reactive({
21.         r$data <- head(mtcars)
22.         return(head(mtcars))
23.         })
24.       
25.       output$rawView <- renderDataTable({inputData()})
26.       output$processView <- renderDataTable({r$data})
27.       output$count <- renderText(ncol(r$data))
28. 
29.       # remove or undo
30.       removecolumn <- function(df, nameofthecolumn){dplyr::select(df, -all_of(nameofthecolumn))}
31. 
32.       output$selectColumn <- renderUI({
33.         selectInput(inputId = ns("selectColumn"), ## namespace
34.                     label = "Select sample(s) to remove",
35.                     choices = colnames(r$data)
36.                     )
37.         })
38.       observeEvent(input$removeCol, {
39.         r$data <- removecolumn(r$data, input$selectColumn)
40.         })
41.       observeEvent(input$undoCol, {
42.         r$data <- inputData()
43.         })
44.     }
45.   )
46. }
47. 
48. 
49. # Application
50. library(shiny)
51. app_ui <- function() {
52.   fluidPage(
53.     choice_ui("choice_ui_1")
54.   )
55. }
56. 
57. app_server <- function(input, output, session) {
58.   r <- reactiveValues()
59.   choice_server("choice_ui_1", r = r)
60. }
61. 
62. shinyApp(app_ui, app_server)

如果您想在 PC 中进行测试,请复制以下代码。

choice_ui <- function(id) {
  ns <- NS(id)
  tagList(
    sidebarPanel(
      uiOutput(outputId = ns("selectColumn"), label = "Select Columns to delete"),
      actionButton(inputId = ns("removeCol"), label = "Remove"),
      actionButton(inputId = ns("undoCol"), label = "Undo")
    ),
    mainPanel(
      dataTableOutput(outputId = ns("rawView")),
      dataTableOutput(outputId = ns("processView")),
      textOutput(outputId = ns("count"))
    )
  )
}

choice_server <- function(id, r) {
  ns <- NS(id) ## use namespace here
  moduleServer(id, function(input, output, session){
    inputData <- reactive({
      r$data <- head(mtcars)
      return(head(mtcars))
    })
    
    output$rawView <- renderDataTable({inputData()})
    output$processView <- renderDataTable({r$data})
    output$count <- renderText(ncol(r$data))
    
    # remove or undo
    removecolumn <- function(df, nameofthecolumn){dplyr::select(df, -all_of(nameofthecolumn))}
    
    output$selectColumn <- renderUI({
      selectInput(inputId = ns("selectColumn"), ## namespace
                  label = "Select sample(s) to remove",
                  choices = colnames(r$data)
      )
    })
    observeEvent(input$removeCol, {
      r$data <- removecolumn(r$data, input$selectColumn)
    })
    observeEvent(input$undoCol, {
      r$data <- inputData()
    })
  }
  )
}


# Application
library(shiny)
app_ui <- function() {
  fluidPage(
    choice_ui("choice_ui_1")
  )
}

app_server <- function(input, output, session) {
  r <- reactiveValues()
  choice_server("choice_ui_1", r = r)
}

shinyApp(app_ui, app_server)
r 闪亮 的命名空间

评论

1赞 Jan 12/28/2022
我想说这正是它应该的样子。

答: 暂无答案