Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merged code for rstudio addin, updated DESCRIPTION imports and docs. #85

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ Imports:
mapview,
miniUI,
sf (>= 0.5-2),
shiny
shiny,
rstudioapi (>= 0.8),
shinyWidgets (>= 0.4.3)
Suggests:
crayon
Enhances:
geojsonio
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.0
RoxygenNote: 6.1.1
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@ export(editFeatures)
export(editMap)
export(editMod)
export(editModUI)
export(mapeditAddin)
export(selectFeatures)
export(selectMap)
export(selectMod)
export(selectModUI)
import(htmltools)
import(shiny)
importFrom(leaflet,setView)
importFrom(mapview,mapview)
importFrom(miniUI,gadgetTitleBar)
importFrom(miniUI,miniButtonBlock)
importFrom(miniUI,miniContentPanel)
importFrom(miniUI,miniPage)
importFrom(rstudioapi,getActiveDocumentContext)
importFrom(sf,write_sf)
importFrom(shinyWidgets,switchInput)
importFrom(shinyWidgets,updateSwitchInput)
87 changes: 87 additions & 0 deletions R/addin.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@



#' @title mapedit Addin
#' @description Create and save spatial objects within the Rstudio IDE
#'
#' @return sf object to .GlobalEnv and/or saved to file
#' @importFrom miniUI miniPage miniContentPanel gadgetTitleBar miniButtonBlock
#' @import shiny
#' @importFrom shinyWidgets switchInput updateSwitchInput
#' @importFrom mapview mapview
#' @importFrom sf write_sf
#' @importFrom leaflet setView
#' @importFrom rstudioapi getActiveDocumentContext
#' @export
#'
mapeditAddin <- function() {

ui <- miniPage(
gadgetTitleBar("Edit Map"),
miniContentPanel(
editModUI("editor"),
miniButtonBlock(
div(style="display: inline-block;padding-top:22px;padding-left:30px;width:180px;",
switchInput('savefile', 'Save', value = FALSE, onStatus = "success", offStatus = "danger")),
div(style="display: inline-block; width: 400px;",
textInput('filename', '', value = 'saved_geometry.geojson')),
div(style="display: inline-block;padding-top:18px;width: 400px;font-size: 10pt;color: #313844;",
'You can add folders and change output type.',
'Created geometry will always save to .GlobalEnv')
)
)
)

server <- function(input, output, session) {

# get values from rstudio
ct <- getActiveDocumentContext()

TEXT <- ct$selection[[1]]$text
OBJECTNAME <- ifelse(TEXT == '', 'geom', TEXT)
FILENAME <- ifelse(TEXT == '', 'saved_geometry.geojson', paste0(TEXT, '.geojson'))
SF_OBJECT <- NULL

# test selected text an sf object
try({
SF_OBJECT <- get(TEXT)
if (class(SF_OBJECT) != 'sf') {SF_OBJECT <- NULL}
})

# update UI based on inputs
updateTextInput(session, 'filename', value = FILENAME)
if (FILENAME != 'saved_geometry.geojson') {
updateSwitchInput(session, 'savefile', value = TRUE)
}


# load mapedit
if (class(SF_OBJECT) == 'sf') {
geo <- callModule(editMod, "editor", mapview(SF_OBJECT)@map)
} else {
geo <- callModule(editMod, "editor", setView(mapview()@map, 80, 0, 3))
}


# return geometry to file and object in .GlobalEnv
observeEvent(input$done, {
geom <- geo()$finished

if (!is.null(geom) & !is.null(SF_OBJECT)) geom <- rbind(SF_OBJECT, geom)

if (!is.null(geom)) {
assign(OBJECTNAME, geom, envir = .GlobalEnv)
if (input$savefile) {
sf::write_sf(geom, input$filename, delete_layer = TRUE, delete_dsn = TRUE)
}
}

stopApp()
})

}

viewer <- paneViewer(600)
runGadget(ui, server, viewer = viewer)

}
4 changes: 4 additions & 0 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Name: Create Spatial Data
Description: Create spatial data using mapedit
Binding: mapeditAddin
Interactive: true
14 changes: 14 additions & 0 deletions man/mapeditAddin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.