我有一个在R Studio中编写的R Shiny应用程序,里面有单独的用户界面.R、伺服器.R、全球 al.R,还有一个www文件夹,里面有一些图片.这款应用程序所做的就是显示其全球 al.R文件中指定的目录中的图像.这些图像具有允许用户 Select 图像的复选框.一旦用户 Select 他们的图片并按下名为"创建"的操作按钮,应用程序就会创建两个文本文件,列出应用程序目录中用户 Select 和未 Select 的图像的文件名.
现在,我真正需要帮助的是两件事:
-
我不是在全球 al.R文件中指定图像目录和项目名称,而是让我的应用程序接受来自用户的参数,即,我希望用户在运行应用程序时以某种方式指定图像目录和项目名称(稍后在创建文本文件时使用的名称).理想情况下,我希望将全球 al.R文件设置为空,其中不包含任何代码.这有可能吗?我的一位朋友建议我可以使用名为ShinyOptions和getShinyOtion的东西,但我和他都不熟悉这一点.
-
如果我要从我的计算机(而不是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"