Domanda

I am trying to create a Shiny App.

I'd like it to download a CSV file from the web and store it on the local machine, then perform analysis.

My current approach is:

ui.R

library(shiny)

shinyUI(pageWithSidebar(

  # Application title
  headerPanel("TEST"),

  sidebarPanel(
    sliderInput("range", "Date Range:",
                min = 0, max = 15, value = c(0,15))
  ),

  # Show a tabset that includes a plot, summary, and table view
  # of the generated distribution
  mainPanel(
    tabsetPanel(
      tabPanel("Plot", plotOutput("plot"))
  )
))

server.R

library(shiny)

shinyServer(function(input, output) {

datasetInput <- function(){

  x1 <- strptime(Sys.time(), "%Y-%m-%d %H:%M:%S")
  x2 <- strptime(file.info("/srv/shiny-server/Data/current.csv")$mtime, "%Y-%m-%d %H:%M:%S")

if ( difftime(x1, x2, units='mins') > 20 ){
  str <- "wget http://www.web.com/file.csv -O /srv/shiny-server/Data/current.csv"
  system(str)
}
    data <- read.csv("/srv/shiny-server/Data/current.csv")
    return(data)
}

output$plot <- renderPlot({

data <- datasetInput()
plot(data)

})

So, everything works. The data plots perfectly. The problem is the wget script doesn't get called. Regardless of where I put it.

For simplicity my main goal is to download and save a CSV file when the app runs. Then read in that CSV file as my main dataframe.

The ultimate goal is for my app to do the time check (Check if file is older then 20 minutes) every time someone does anything with the app. If it is older, I want to download/save the file, and update my data frame.

* note * using the wget function is a work around for a problem accessing a password protected CSV file.

The problem/solution is discussed here: R Import - CSV file from password protected URL - in .BAT file

I don't know much about how Shiny works, the code used to generate the Shiny app is mostly from: http://rstudio.github.io/shiny/tutorial/#tabsets

È stato utile?

Soluzione

Try using the httr package instead of a raw wget. Here's an example of a Shiny application that also downloads a remote CSV file and parses it. https://github.com/trestletech/dallas-police/blob/master/shiny/server.R

Also you may find this tutorial valuable, as I think you could be using reactiveFunctions as the sources of your data input.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top