Skip to content

Instantly share code, notes, and snippets.

@aurielfournier
Created May 31, 2018 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aurielfournier/09e5626a8424c192c53734193a44800f to your computer and use it in GitHub Desktop.
Save aurielfournier/09e5626a8424c192c53734193a44800f to your computer and use it in GitHub Desktop.
edit_biblio <- function(filepath=
"./inst/metadata-tables/biblio.csv",
outdir=getwd(),
outfilename="biblio"){
ui <- shinyUI(fluidPage(
wellPanel(
div(class='row',
div(class="col-sm-6",
actionButton("save", "Save"))
)
),
wellPanel(
h3("Plot Bounding Box [After all 4 bounds have been entered]"),
div(class='row',
div(class="col-sm-6",
actionButton("makebb", "Plot Bounding Box"))
)
),
plotOutput("bbmap")
),
mainPanel(
rHandsontableOutput("hot"),
)
)
))
server <- shinyServer(function(input, output) {
values <- reactiveValues()
dat <- readr::read_csv(file = filepath,
col_types = "ccccccccccccccc")
output$hot <- rhandsontable::renderRHandsontable({
rows_to_add <- as.data.frame(matrix(nrow=1,
ncol=ncol(dat)))
colnames(rows_to_add) <- colnames(dat)
DF <- dplyr::bind_rows(dat, rows_to_add)
rhandsontable::rhandsontable(DF,
useTypes = TRUE,
stretchH = "all")
})
## Save
observeEvent(input$save, {
finalDF <- hot_to_r(input$hot)
utils::write.csv(finalDF,
file=file.path(outdir,
sprintf("%s.csv",
outfilename)),
row.names = FALSE)
})
observeEvent(input$makebb, {
finalDF <- hot_to_r(input$hot) %>%
dplyr::mutate(northBoundCoord =
as.numeric(northBoundCoord),
eastBoundCoord =
as.numeric(eastBoundCoord),
southBoundCoord =
as.numeric(southBoundCoord),
westBoundCoord =
as.numeric(westBoundCoord))
bb <- renderDataTable(data.frame(north=
finalDF$northBoundCoord,
south=
finalDF$southBoundCoord,
east=
finalDF$eastBoundCoord,
west=
finalDF$westBoundCoord))
})
## bounding box map
output$bbmap <- renderPlot({
world <- ggplot2::map_data("world")
if(is.numeric(bb$westBoundCoord)){
ggplot2::ggplot()+
ggplot2::geom_map(data=world, map=world,
aes(x=long, y=lat, map_id=region),
color="black",fill="#7f7f7f")+
ggplot2::geom_rect(data=cc,
aes(xmin=east, xmax=west,ymin=south,ymax=north), fill=NA, color="orange")}
if(!is.numeric(bb$westBoundCoord)){
ggplot2::ggplot()+
ggplot2::geom_map(data=world, map=world,
aes(x=long, y=lat, map_id=region),
color="black",fill="#7f7f7f")
}
})
## Message
output$message <- renderUI({
if(input$save==0){
helpText(sprintf("This table will be saved in folder \"%s\" once you press the Save button.", outdir))
}else{
outfile <- "biblio.csv"
fun <- 'read.csv'
list(helpText(sprintf("File saved: \"%s\".",
file.path(outdir, outfile))),
helpText(sprintf("Type %s(\"%s\") to get it.",
fun, outfile)))
}
})
})
## run app
runApp(list(ui=ui, server=server))
return(invisible())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment