我有一个在R Studio中编写的R Shiny应用程序,里面有单独的用户界面.R、伺服器.R、全球 al.R,还有一个www文件夹,里面有一些图片.这款应用程序所做的就是显示其全球 al.R文件中指定的目录中的图像.这些图像具有允许用户 Select 图像的复选框.一旦用户 Select 他们的图片并按下名为"创建"的操作按钮,应用程序就会创建两个文本文件,列出应用程序目录中用户 Select 和未 Select 的图像的文件名.

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

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

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

因此,如果任何人有使用这些或其他人的 idea ,都会非常感谢他们的帮助.

为了帮助重现我的 case ,下面我包含了我当前在uI.R、伺服器.R和全球 al.R中拥有的代码.

用户界面

library(shiny)
library(shinydashboard)

dashboardPage(
  dashboardHeader(title = "Pictures List"),
  dashboardSidebar(
    actionButton("the_action_button", "Create", width = "100%")
  ),
  dashboardBody(
    fl用户界面dRow(
      box(
        title = "Picture Selection",
        width = 12,
        用户界面Output("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"

推荐答案

虽然我认为shinyOptions解决方案更优雅,但如果我们不想使用getShinyOption,我建议这样做:

  • 将路径作为参数传递,并将它们分配给全局环境,以便应用程序可以访问它们.
  • 删除函数执行完成后赋值为on.exit的变量
my_app <- function(image_folder = "~/Desktop/pictures_folder",
                   project_name = "land_project") {
    image_folder <<- image_folder
    project_name <<- project_name
    on.exit({
        rm(list = c("image_folder", "project_name"), envir = .GlobalEnv)
        message("clearing image_folder variable")
    })
    shiny::runApp()
}

my_app()

R相关问答推荐

使用格式化程序自定义hc_tooltip以添加textColor删除了我的标记并try 将它们带回失败

如果索引重复,聚合xts核心数据

从嵌套列表中智能提取线性模型系数

在(g)子中使用asserable字符

如何修复R码的置换部分?

如何从当前行上方找到符合特定条件的最接近值?

在数学中正确显示摄氏度、开氏度或华氏度

在R中为马赛克图中的每个字段着色

矩阵的堆叠条形图,条形图上有数字作为标签

迭代到DataFrame列并获得成对的值列表(col1->;col2、col2->;col3、col3->;col4等)的正确方法.

如何在ggplot2中绘制具有特定 colored颜色 的连续色轮

基于数据集属性将科分配给物种

在纵向数据集中创建新行

数据集上的R循环和存储模型系数

计算来自单独分组的分幅的值的百分位数

如何在刻面和翻转堆叠条形图中对齐geom_text()

将CSV转换为R中的自定义JSON格式

如何在R中的两列以上使用联合(&U)?

在不带max()的data.table中按组查找最后一个元素

R,将组ID分配给另一个观测ID变量中的值的组合