R闪亮的动态数据功能-如何根据日期范围输入更改访问哪个CSV?

时间:2018-09-05 22:40:32

标签: r shiny

我希望有一个仪表板,可以让您选择某些日期,并根据这些日期访问相应的CSV文件以更改输出图。为此,我的CSV是否需要在公共站点上并使用getSymbols函数?我已经有了正确的输入和输出结构,我只是想弄清楚动态数据集的访问。任何建议都是有帮助的!

1 个答案:

答案 0 :(得分:0)

无需将您的数据放在公共站点上。 shinyapps.io允许将数据存储在应用程序文件夹中。请参阅取决于数据输入自动加载csv文件的代码:

# csv file simulation
set.seed(123)
write.csv(data.frame(x = rnorm(100), y = rnorm(100)), "old.csv")
write.csv(data.frame(x = 100 * rnorm(100), y = 100 * rnorm(100)), "new.csv")



library(shiny)

# Define UI for application 
ui <- fluidPage(

   # Application title
   titlePanel("Dynamic loading of csv file"),

   sidebarLayout(
      sidebarPanel(
        radioButtons("rb", "Choose one:",
                     choiceNames = list(
                       "old", "new"),
                     choiceValues = list(
                       2, 1
                     ))
      ),

      mainPanel(
         plotOutput("distPlot")
      )
   )
)

server <- function(input, output) {
   output$distPlot <- renderPlot({
     if(input$rb == 1) {
       df <- read.csv("new.csv", header = TRUE)
       clr <- "red"
     } else {
       df <- read.csv("old.csv", header = TRUE)
       clr <- "blue"
     }

      plot(df$x, df$y, col = clr)
   })
}

# Run the application 
shinyApp(ui = ui, server = server)