我是 R Dashboard 的新手(昨天才真正开始使用它)。本质上,代码的目的是进入我的 C: 驱动器上的一组文件夹和子文件夹,然后根据下拉菜单的选择,它在该目录中显示“.png”文件。当我在同一页面中使用“ui.R”和“server.R”编写整个内容并使用
运行它们时shinyapp(ui, server)
它工作得很好,但是当我想使用
部署应用程序时deployApp(appName = "Name of the app")
为此目的分别创建“ui.R”和“server.R”功能,虽然它确实构建了应用程序,但它不显示下拉菜单中的文件或 .png 文件。我的“ui.R”代码如下:
library(shiny)
library(png)
library(shinyFiles)
library(dplyr)
library(ggplot2)
library(rsconnect)
# Define the directory paths
main_dir <- "C:/Users/username/Dropbox/user/Academic Paper/Data Files/Data/Plots"
annual_dir <- file.path(main_dir, "Annual")
quarterly_dir <- file.path(main_dir, "Quarterly")
# Define the country names (i.e., the three-letter folder names)
country_names <- list.files(annual_dir, recursive = FALSE)
# Define the plot types
plot_types <- c("Difference from base", "Level")
ui <- fluidPage(
column(width = 3,
selectInput("country", "Select Country", choices = country_names),
selectInput("freq", "Select Frequency", choices = c("Annual", "Quarterly")),
selectInput("folder", "Select Plot Type", choices = plot_types),
uiOutput("file_selector")
),
column(width = 9,
imageOutput(outputId = "plot")
)
)
和“server.R”文件如下:
library(shiny)
library(png)
library(shinyFiles)
library(dplyr)
library(ggplot2)
library(rsconnect)
server <- function(input, output, session) {
# Initialize variable name
previous_country <- ""
file_list <- reactive({
# Change variable name only if country changes
if (input$country != previous_country) {
previous_country <<- input$country
message("Updating variable name...")
}
chosen_path <- file.path("C:/Users/username/Dropbox/user/Academic Paper/Data Files/Data/Plots", input$freq, input$country, input$folder)
png_files <- list.files(chosen_path)
png_files <- png_files[!grepl("^(asl|asd|ql|qd)", png_files)]
png_files <- gsub("\\.png$", "", png_files) # remove ".png" extension
return(png_files)
})
output$file_selector <- renderUI({
selectInput(inputId = "file", label = "Select a file",
choices = file_list())
})
output$plot <- renderPlot({
# Get the selected directory path based on the input values
chosen_file <- file.path("C:/Users/username/Dropbox/user/Academic Paper/Data Files/Data/Plots", input$freq, input$country, input$folder, paste0(input$file, ".png"))
# Display the PNG file as a plot
#png(chosen_file)
img <- readPNG(chosen_file)
grid::grid.raster(img)
#dev.off()
}, height = 600, width = 800)
}
我做错了什么?
提前致谢!