运行闪亮时查找文件

时间:2015-08-13 21:35:50

标签: r directory shiny

我有一个文件,我无法阅读闪亮的文件。我的用户界面工作正常,但我认为我的问题是它在运行应用程序时没有读取数据。我将我的工作目录设置为桌面。要打开放入代码中的csv文件,它将通过以下方式打开:

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

然后在运行代码后,我正在努力运行应用程序:

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"

所以我能够在R中打开它,我已经测试了但是当我尝试在应用程序中运行它时,它似乎无法找到它。任何帮助将不胜感激

代码:

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

1 个答案:

答案 0 :(得分:4)

文件的路径不正确。请考虑该应用程序正在App-1目录中运行。所以使用:

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

代码中的另一个问题是对ggplot的调用。您不必使用子集,只需输入列名称即可。

此外,您使用colorlegend参数的方式是错误的。根据我的理解,您希望每个发布者都有不同的颜色图例标签。 colour参数用于输入列,线条将被着色,并相应地生成图例。

您可以使用scale_color_manual来使用默认线条颜色。这样您就可以摆脱colorlegend个参数。

顺便说一句,我建议不要使用函数名创建对象,例如datalegend。这可能会导致一些混乱。

最后,代码(ui.R与您发布的内容相同):

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