"下载"按钮不适用于我闪亮的应用程序

时间:2016-08-17 19:57:59

标签: shiny shiny-server

我差不多完成了我的shinyapp,最后一部分是在我的server.R中添加一个下载按钮。我按照"如何下载"的说明进行操作。在Rshiny但它在我的案例中并没有真正起作用。

这是我的ui.R

library(shiny)
library(hurricaneexposure)
library(hurricaneexposuredata)
library(ggplot2)


data("hurr_tracks")

storms <- unique(hurr_tracks$storm_id)
storm_years <- as.numeric(gsub(".+-", "", storms))
storms <- storms[storm_years <= 2011]

years <- unique(storm_years)
years <- years[years <= 2011]


shinyUI(fluidPage(

  # Application title
  titlePanel("County-level exposure to tropical storms"),

  sidebarLayout(
    sidebarPanel(
      selectInput("year", label = "Storm year", years,
              selected = "1988"),

  # This outputs the dynamic UI component
  uiOutput("ui"),
  selectInput("metric", label="Storm exposure metric:",
              choices =  c("distance", "rainfall", "wind"),
              selected = "distance"),
  numericInput("limit", 
               label = "Limit range", 
               value = 100),
  downloadButton('downloadData', 'Download The Table')

),
mainPanel(plotOutput("map"))
  ),
  fluidRow(
    DT::dataTableOutput("table")
  )

))

这是我的服务器.R

library(shiny)
library(devtools)
library(ggplot2)
library(hurricaneexposuredata)
library(hurricaneexposure)
library(choroplethrMaps)
library(dplyr)

data("hurr_tracks")
data("county_centers")

storms <- unique(hurr_tracks$storm_id)
storm_years <- as.numeric(gsub(".+-", "", storms))
storms <- storms[storm_years <= 2011]

years <- unique(storm_years)
years <- years[years <= 2011]


all_fips <- unique(county_centers$fips)

## Split storm_id based on same year
stm <- split(storms, gsub(".+-", "", storms))
stm <- lapply(stm, function (x) gsub("-.+", "", x))

shinyServer(function(input, output, session) {

  output$ui <- renderUI({

    selectInput("storm_name", label = "Storm name", stm[input$year],
                selected = "Alberto")

  }) 

  output$map <-renderPlot({
    storm_id <- paste(input$storm_name, input$year, sep = "-")
    a <- map_counties(storm = storm_id, metric = input$metric)
    map_tracks(storms = storm_id, plot_object = a, plot_points = FALSE) + 
      ggtitle(paste(input$storm_name, input$year, input$metric, sep = ", "))

  })


  output$table <- DT::renderDataTable(DT::datatable({
   if(input$metric == "distance"){
     tab_out <- county_distance(counties = all_fips, start_year = input$year, 
                     end_year = input$year, dist_limit = input$limit) %>%
       dplyr::filter(storm_id == paste(input$storm_name,
                                       input$year, sep = "-")) %>%
      dplyr::left_join(county_centers, by = "fips") %>%
       dplyr::mutate(county = paste(county_name, state_name, sep =  ", ")) %>%
       dplyr::select(county, fips, closest_date, storm_dist) %>%
       arrange(storm_dist)
   } else if (input$metric == "rain"){
     tab_out <- county_rain(counties = all_fips, start_year = input$year, 
                            end_year = input$year, rain_limit = input$limit) %>%
       dplyr::filter(storm_id == paste(input$storm_name,
                                       input$year, sep = "-")) %>%
       dplyr::left_join(county_centers, by = "fips") %>%
       dplyr::mutate(county = paste(county_name, state_name, sep =  ", ")) %>%
       dplyr::select(county, fips, closest_date, tot_precip) %>%
       dplyr::rename(rainfall_mm = tot_precip) %>%
       arrange(desc(rainfall_mm))
   } else if(input$metric == "wind"){
     tab_out <- county_wind(counties = all_fips, start_year = input$year, 
                                end_year = input$year, wind_limit = input$limit) %>%
       dplyr::filter(storm_id == paste(input$storm_name,
                                       input$year, sep = "-")) %>%
       dplyr::left_join(county_centers, by = "fips") %>%
       dplyr::mutate(county = paste(county_name, state_name, sep =  ", ")) %>%
       dplyr::select(county, fips, max_sust) %>%
       dplyr::rename(wind_mps = max_sust) %>%
       arrange(desc(wind_mps))
   }
  })
  )
  output$downloadData <- downloadHandler(
    #filename = function() { paste(input$storm_name, input$year, input$metric,'.csv', sep='_') },
    filename = "ex.csv",
    content = function(file) {
      write.csv(tab_out, file)
    })   ### if I can recall the table

})

代码看起来很长但我希望有人能成功运行它。这个应用程序的想法很简单,我只是想知道如何将我的输出(只是那个表)下载为csv格式

0 个答案:

没有答案