【问题标题】:Find a file when running shiny运行闪亮时查找文件
【发布时间】:2015-08-13 21:35:50
【问题描述】:

我有一个文件,我无法在 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"))
  )
)
)

【问题讨论】:

  • 您的应用程序的文件夹/文件结构是什么?
  • 你能发布你闪亮的代码吗?
  • @PauloMiraMor 刚刚发布了代码。
  • 你确定路径正确吗?
  • 找到文件的路径? @PauloMiraMor

标签: r directory shiny


【解决方案1】:

文件路径不正确。考虑该应用程序正在 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
    })})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2018-07-22
    • 2021-12-02
    • 2013-07-08
    • 1970-01-01
    • 2017-10-12
    • 2019-07-09
    相关资源
    最近更新 更多