Shiny &引用;下载「;按钮不存在';在我闪亮的应用程序中无法工作

Shiny &引用;下载「;按钮不存在';在我闪亮的应用程序中无法工作,shiny,shiny-server,Shiny,Shiny Server,我的shinyapp快完成了,最后一部分是在我的服务器中添加一个下载按钮。我在Rshiny中遵循了“如何下载”的说明,但在我的情况下,它并没有真正起作用 这是我的ui.R library(shiny) library(hurricaneexposure) library(hurricaneexposuredata) library(ggplot2) data("hurr_tracks") storms <- unique(hurr_tracks$storm_id) storm_yea

我的shinyapp快完成了,最后一部分是在我的服务器中添加一个下载按钮。我在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")
  )

))
库(闪亮)
图书馆(飓风暴露)
图书馆(飓风暴露数据)
图书馆(GG2)
数据(“hurr_轨道”)

您需要将
tab\u out
定义为一个全局变量,并使用
@warmoverflow tab\u out赋值为if else语句,因此根据不同的用户选择,tab\u out会有所不同。然后我想知道如何在shinyserver之前将其作为一个全局变量提取出来正如我提到的,只需将
tab\u out
定义为一个全局变量即可(在shinyserver外部,添加
tab\u out=NULL
,然后在您的if-else中添加
tab\u out@warmoverflow非常感谢!您需要将
tab\u out
定义为一个全局变量,并使用
@warmoverflow tab\u out指定了if-else语句,因此根据不同的用户选择,tab\u out会有所不同。然后我想知道是什么现在我可以在shinyserver之前将其作为一个全局变量提取出来,正如我所提到的,只需将
tab\u out
定义为一个全局变量(在shinyserver外部,添加
tab\u out=NULL
,然后在if-else中添加
tab\u out@warmoverflow非常感谢!
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

})