0

I am very new to R Dashboard (literally started using it yesterday). Essentially, the purpose of the code is to go into a set of folder and subfolders on my C: drive and then based on the selection of dropdown menu, it presents the ".png" files in that directory. When I write the whole thing using "ui.R" and "server.R" in the same page and run them using

shinyapp(ui, server)

it works just fine, but when I want to deploy the app using

deployApp(appName = "Name of the app")

and for this purpose create the "ui.R" and "server.R" function separately, while it does build the app, it does not show the files from the dropdown menu or the .png files. My "ui.R" code is as follows:

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")
  )
)

and the "server.R" file is as follows:

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)
}

What am I doing wrong?

Thanks in advance!

Carl
  • 301
  • 2
  • 10

1 Answers1

0

It seems like the server part is not running. Try putting the line

shinyapp(ui, server)

(with the correct file names) at the end of the server file after the final closing brackets, then running "deployApp()" as before.

Hallie
  • 1
  • 2