在R Shiny中清除(重置)上传文件输入

时间:2018-02-12 10:26:46

标签: r shiny leaflet shinydashboard shinyjs

我正在构建一个闪亮的应用程序,它基本上传多个形状文件(dbf,prj,shp,shx)并使用传单包创建地图。上传完成并创建地图后,有一个重置按钮,其功能是清除/重置文件输入的现有值,虽然我尝试使用shinyjs同样做但仍然看起来文件输入缓存值。下面是我试过的代码。寻找休息文件的提示输入如下。

## app.R ##
library(shinydashboard)
library(shinyjs)
library(leaflet)
library(sp)
library(rgdal)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(

    fileInput('uploadFile', 'Upload Files',
              accept=c('text/csv', 'text/comma-separated-values,text/plain'), multiple=T),

    fileInput('uploadFile2', 'Upload Files 2',
              accept=c('text/csv', 'text/comma-separated-values,text/plain'), multiple=T),

    actionButton('reset', 'Reset', icon = icon("minus"),style="color:rgb(57,156,8);border-color:rgb(57,156,8)"),
    tags$style(type='text/css', "#reset {margin-left: 10px;}")

  ),
  dashboardBody(

    useShinyjs(),

    leafletOutput("GeoStates",width = "125%", height = 500)

  )
)

server <- function(input, output) {

  # Folder name and uploaded file names 
  values <- reactiveValues(
    folderName = "dataSource",
    fileName = ""
  )

  # Expression for reading the OGR and transforming it
  expr_q <- quote({ 

    if(values$fileName == "") 
      return (NULL)
    spTransform(readOGR(dsn = paste0(getwd(),"/",values$folderName), layer = values$fileName), 
                CRS("+proj=longlat +datum=WGS84"))

  })

  ORG0 <- reactive(expr_q, quoted = TRUE)

  # Upload file 
  observe({
    if (!is.null(input$uploadFile)){
      hide(id = "uploadMsgBox", anim = TRUE)
      if(!dir.exists(values$folderName))
        dir.create(values$folderName,recursive = TRUE)
      for(i in 1:nrow(input$uploadFile)){
        values$fileName <- substr(input$uploadFile[i,1],0,nchar(input$uploadFile[i,1])-4)
        file.copy(input$uploadFile[i,4],paste0(getwd(),"/",values$folderName,"/",input$uploadFile[i,1]))
      }
    }
  })

  # -------------------------------------------------------------------------------------------------------------------------------
  # -------------------------------------------------------------------------------------------------------------------------------

  # Folder name and uploaded file names

  values2 <- reactiveValues(
    folderName2 = "dataSource2",
    fileName2 = ""
  )

  expr_q2 <- quote({

    if(values2$fileName2 == "")
      return (NULL)
    spTransform(readOGR(dsn = paste0(getwd(),"/",values2$folderName2), layer = values2$fileName2),
                CRS("+proj=longlat +datum=WGS84"))
  })

  ORG2 <- reactive(expr_q2, quoted = TRUE)

  # Upload file 2
  observe({
    if (!is.null(input$uploadFile2)){
      hide(id = "uploadMsgBox2", anim = TRUE)
      if(!dir.exists(values2$folderName2))
        dir.create(values2$folderName2,recursive = TRUE)
      for(i in 1:nrow(input$uploadFile2)){
        values2$fileName2 <- substr(input$uploadFile2[i,1],0,nchar(input$uploadFile2[i,1])-4)
        file.copy(input$uploadFile2[i,4],paste0(getwd(),"/",values2$folderName2,"/",input$uploadFile2[i,1]))
      }
    }
  })

  #------------------------------------------------------------------------------------------------------------------
  #------------------------------------------------------------------------------------------------------------------

  observeEvent(input$reset, {

    values$fileName == ""
    values2$fileName2 == ""

    ORG0 <- NULL
    ORG2 <- NULL

    shinyjs::reset('uploadFile')
    shinyjs::reset('uploadFile2')

    unlink(paste0(getwd(),"/dataSource"), recursive = TRUE)
    unlink(paste0(getwd(),"/dataSource2"), recursive = TRUE)

    output$GeoStates <- renderLeaflet({ })

  })

  #----------------------------------------------------------------------------------------------------------------
  #----------------------------------------------------------------------------------------------------------------

  observe({

   if (!is.null(input$uploadFile) & is.null(input$uploadFile2)){

    print("TEST1")

    output$GeoStates <- renderLeaflet({

      leaflet(data = ORG0()) %>% addTiles() %>% addPolygons(fill = FALSE, stroke = TRUE, color = "#0090C5") %>%
        addLegend("bottomright", colors = "#0090C5", labels = values$fileName) %>%
        addProviderTiles(providers$Esri.WorldImagery,
                         options = providerTileOptions(noWrap = TRUE))%>%
        addScaleBar(position = "topright", options = scaleBarOptions(metric = TRUE))

    })

  }


  if (!is.null(input$uploadFile) & !is.null(input$uploadFile2)){

    print("TEST2")

    output$GeoStates <- renderLeaflet({

      leaflet(data = ORG0()) %>% addTiles() %>% addPolygons(fill = FALSE, stroke = TRUE, color = "#0090C5") %>%
        addLegend("bottomright", colors = "#0090C5", labels = values$fileName) %>%
        addProviderTiles(providers$Esri.WorldImagery,
                         options = providerTileOptions(noWrap = TRUE))%>%
        addPolygons(fill = FALSE, stroke = TRUE, color = "#F39200", data = ORG2())%>%
        addScaleBar(position = "topright", options = scaleBarOptions(metric = TRUE))

    })

  }

  })

}

shinyApp(ui, server)

0 个答案:

没有答案