有没有办法让我闪亮的应用程序接受用户传递的参数?

问题描述 投票:0回答:1

我有 R 闪亮的应用程序,我在 R 工作室中编码,具有单独的 ui.R、server.R、global.R 和一个包含一些图片的 www 文件夹。该应用程序的作用是显示其 global.R 文件中指定的目录中的图像。这些图像具有允许用户选择图像的复选框。一旦用户选择他们的图片并按下名为“创建”的操作按钮,应用程序就会创建 2 个文本文件,列出用户在应用程序目录中选择的和未选择的图像的文件名。

现在,我真正需要帮助的是两件事:

  1. 我不想在 global.R 文件中指定图像目录和项目名称,而是想让我的应用程序接受用户的参数,即,我希望用户在运行应用程序时以某种方式指定图像目录以及项目名称(稍后在创建文本文件时使用的名称)。理想情况下,我想让我的 global.R 文件为空,其中不包含任何代码。那可能吗?我的一位朋友建议我可以使用名为 ShinyOptions 和 getShinyOtion 的东西,但我和他都不熟悉这一点。

  2. 如果我要从计算机的命令行(终端)(而不是 Rstudio 中的终端)运行应用程序,我需要做什么?

因此,如果有人有使用这些或其他人的想法,将非常感谢他们的帮助。

为了帮助重现我的案例,下面我包含了我当前在 ui.R、server.R 和 global.R 中的代码。

ui

library(shiny)
library(shinydashboard)

dashboardPage(
  dashboardHeader(title = "Pictures List"),
  dashboardSidebar(
    actionButton("the_action_button", "Create", width = "100%")
  ),
  dashboardBody(
    fluidRow(
      box(
        title = "Picture Selection",
        width = 12,
        uiOutput("image_selection")
      )
    )
  )
)

服务器

library(shiny)
library(shinydashboard)

function(input, output, session) {
  
  # Get a list of available image files from the specified folder
  
  image_files <- list.files(image_folder, pattern = "\\.png$", full.names = FALSE)
  
  # Initialize a list to store selected images
  selected_images <- reactiveVal(character(0))
  
  # Render UI for image selection
  output$image_selection <- renderUI({
    tagList(
      lapply(image_files, function(img) {
        img_tag <- tags$img(src = img, width = "100%")
        checkbox_tag <- checkboxInput(
          inputId = img,
          label = NULL,
          value = img %in% selected_images()
        )
        div(img_tag, checkbox_tag)
      })
    )
  })
  
  # Create and download text files based on selected images
  observeEvent(input$the_action_button, {
    # Create a timestamp for the filenames
    timestamp <- format(Sys.time(), format = "%Y%m%d")
    
    # Create text file for images not selected
    not_selected_images <- setdiff(image_files, selected_images())
    not_selected_filename <- paste0("not_selected_file_", project_name,
                                    "_", timestamp, ".txt")
    writeLines(not_selected_images, con = not_selected_filename)
    
    # Create text file for selected images
    selected_images <- selected_images()
    selected_filename <- paste0("selected_file_list_", project_name, 
                                "_", timestamp, ".txt")
    writeLines(selected_images, con = selected_filename)
  })
}

全球

image_folder <- "~/Desktop/pictures_folder"
project_name <- "land_project"
r shiny shinydashboard
1个回答
0
投票

使用Shiny的textInput和fileInput小部件允许用户在运行应用程序时指定图像目录和项目名称:

library(shiny)

图书馆(闪亮的仪表板)

仪表板页面( DashboardHeader(title = "图片列表"), 仪表板侧边栏( textInput("image_directory", "图片目录", placeholder = "输入目录路径"), textInput("project_name", "项目名称", placeholder = "输入项目名称"), actionButton("the_action_button", "创建", 宽度 = "100%") ), 仪表板主体( 流体行( 盒子( title = "图片选择", 宽度=12, uiOutput(“图像选择”) ) ) ) )

更新您的服务器。R以使用这些输入:

 library(shiny)
library(shinydashboard)

function(input, output, session) {
  observe({
    # Get the image folder and project name from user inputs
    image_folder <- input$image_directory
    project_name <- input$project_name
    
    # Get a list of available image files from the specified folder
    image_files <- list.files(image_folder, pattern = "\\.png$", full.names = FALSE)
    
    # ... rest of your server logic ...
  })

  # ... rest of your server logic ...
}
© www.soinside.com 2019 - 2024. All rights reserved.