Nick
Nick

Reputation: 863

Find a file when running shiny

I have a file that I am having trouble reading in shiny. My user-interface is working fine, but I think my issue is that it's not reading the data when running the app. I am setting my work directory to Desktop. To open the csv file being placed into the code, it is opened by:

publishers <- read.csv("App-1/data/syndicationshiny.csv")

Then after running the code, I am struggling with running the app:

runApp("App-1")

"Warning in file(file, "rt") :
  cannot open file 'data/syndicationshiny.csv': No such file or directory
Error in file(file, "rt") : cannot open the connection"

So I am able to open it in R, I've already tested that but when I try to run it in the app it can't seem to find it. Any help would be greatly appreciated

Code:

# server.R
library(shiny)
library(ggplot2)
publishers <- read.csv("App-1/data/syndicationshiny.csv")
source("helpers.R")
head(publishers)
publishers$Date_Delivered<-as.Date(publishers$Date_Delivered,'%m/%d/%Y')


shinyServer(
  function(input, output) {
    output$map <- renderPlot({
      data <- switch(input$var, 
                     "A" = publishers[ which(publishers$Publisher=='A'),],
                     "B" = publishers[ which(publishers$Publisher=='B'),],
                     "C" = publishers[ which(publishers$Publisher=='C'),],
                     "D" = publishers[ which(publishers$Publisher=='D'),],
                     "E" = publishers[which(publishers$Publisher=='E'),],
                     "F" = publishers[  which(publishers$Publisher=='F')])

  color <- switch(input$var, 
                  "A" = "darkgreen",
                  "B" = "black",
                  "C" = "darkorange",
                  "D" = "darkviolet",
                  "E" = "darkred",
                  "F" ="darkblue")

  legend <- switch(input$var, 
                   "A" = "A",
                   "B" = "B",
                   "C" = "C",
                   "D" = "D",
                   "E" = "E",
                   "F" ="F")


      g<-ggplot(data,aes(data[,Date_Delivered],data[,impressions], 
                     color = color, 
                       legend.title = legend))+geom_line()
      print(g)
})})

##ui
library(shiny)
shinyUI(fluidPage(
  titlePanel("Syndication"),

  sidebarLayout(
    sidebarPanel(
      helpText("Create Graphs on Syndication Publishers"),

      selectInput("var", 
                  label = "Choose a variable to display",
                  choices = c("A", "B",
                              "C", "D","E","F"),
                  selected = "A")),

    mainPanel(plotOutput("map"))
  )
)
)

Upvotes: 0

Views: 2048

Answers (1)

Paulo MiraMor
Paulo MiraMor

Reputation: 1610

The path to the file is not correct. Consider that the app is running in the App-1 directory. So use:

publishers <- read.csv("data/syndicationshiny.csv")

Another problem in your code is the call to ggplot. You don't have to use subsetting, just enter the column names.

Also, the way you are using the color and legend arguments is wrong. From what I could understand, you want each publisher to have a different color legend label. The colour argument is used to enter the column by wich the lines will be colored and the legend is produced accordingly.

You can use scale_color_manual to use default line colors. This way you can get rid of your color and legend arguments.

By the way, I suggest not creating objects using function names, such as data or legend. This can lead to some confusion.

Finally, the code (ui.R is as you posted):

# server.R
library(shiny)
library(ggplot2)
publishers <- read.csv("data/syndicationshiny.csv")
#source("helpers.R")
head(publishers)
publishers$Date_Delivered<-as.Date(publishers$Date_Delivered,'%m/%d/%Y')


shinyServer(
  function(input, output) {
    output$map <- renderPlot({
      dat <- switch(input$var, 
                    "A" = publishers[which(publishers$Publisher=='A'),],
                    "B" = publishers[which(publishers$Publisher=='B'),],
                    "C" = publishers[which(publishers$Publisher=='C'),],
                    "D" = publishers[which(publishers$Publisher=='D'),],
                    "E" = publishers[which(publishers$Publisher=='E'),],
                    "F" = publishers[which(publishers$Publisher=='F'),])

      g<-ggplot(dat,aes(Date_Delivered,impressions, 
                        colour = Publisher))+geom_line()+ 
        scale_color_manual(values = c("A" = "darkgreen", 
                                      "B" = "black", 
                                      "C" = "darkorange",
                                      "D" = "darkviolet",
                                      "E" = "darkred",
                                      "F" ="darkblue"
        ))
      g
    })})

Upvotes: 4

Related Questions