Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kguidonimartins committed Dec 18, 2024
1 parent 7727875 commit e00fcce
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 15 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
^codecov\.yml$
^\.lintr$
^\.scratch$
^\.dev$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ tags
docs
docs/
todo.txt
/.dev/
7 changes: 0 additions & 7 deletions .lintr

This file was deleted.

11 changes: 7 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ Imports:
usethis,
crayon,
readr,
rlang
mapview,
sf,
rlang,
jsonlite,
writexl
Suggests:
clipr,
conflicted,
covr,
janitor,
lexicon,
magick,
naniar,
readxl,
rgeos,
rnaturalearth,
sf,
skimr,
stringr,
testthat,
Expand All @@ -46,4 +49,4 @@ Suggests:
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
RoxygenNote: 7.3.2
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export(save_temp_data)
export(setup_lintr)
export(tad_view)
export(trim_fig)
export(view_excel)
export(view_in)
export(view_mapview_from_path)
export(view_vd)
export(view_vd_nonint)
importFrom(crayon,blue)
importFrom(crayon,col_align)
importFrom(crayon,green)
Expand Down Expand Up @@ -53,14 +57,18 @@ importFrom(glue,glue)
importFrom(glue,glue_collapse)
importFrom(glue,identity_transformer)
importFrom(here,here)
importFrom(jsonlite,write_json)
importFrom(magrittr,"%$%")
importFrom(magrittr,"%>%")
importFrom(mapview,mapshot)
importFrom(mapview,mapview)
importFrom(purrr,map)
importFrom(purrr,map_df)
importFrom(purrr,set_names)
importFrom(readr,write_csv)
importFrom(remotes,install_github)
importFrom(rlang,is_empty)
importFrom(sf,read_sf)
importFrom(tidyr,everything)
importFrom(tidyr,pivot_longer)
importFrom(tools,file_path_sans_ext)
Expand Down
2 changes: 1 addition & 1 deletion R/setup_lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' @description
#' This function create a config file (`.lintr`) in root of the user's project
#' based on static code analysis performed by the `{lintr}.
#' based on static code analysis performed by the `{lintr}`.
#'
#' @details
#' `setup_lint` creates a file (`.lintr`) in the root of the user project,
Expand Down
221 changes: 221 additions & 0 deletions R/view_in.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ check_avail <- function(bin) {
#' @param data a data.frame/tibble data format.
#' @param viewer character app to open the csv file.
#' @importFrom readr write_csv
#' @importFrom jsonlite write_json
#' @importFrom sf read_sf
#' @importFrom mapview mapview mapshot
#' @importFrom fs file_exists dir_create
#' @return None
#'
#' @export
Expand All @@ -33,3 +37,220 @@ view_in <- function(data, viewer = c("libreoffice", "gnumeric", "tad")) {
readr::write_csv(data, tmp)
system(paste0(viewer, " ", tmp, " > /dev/null 2>&1 &"))
}

## #' @export
## view_vd <- function(data, title = NULL) {
## if (interactive()) {
## bin <- Sys.which("st")
## if (is.null(title)) {
## title <- "misc::view_vd"
## }
## if (bin == "") {
## message("INFO: `st` is not available; using the built-in `View()`.")
## data |> View()
## } else {
## tmp <- paste0(tempfile(), ".csv")
## readr::write_csv(data, tmp)
## system(glue::glue("st -g '200x65+320+50' -T '{title}' -e sh -c 'vd --default-width=500 {tmp}' > /dev/null 2>&1 &"))
## }
## }
## return(data)
## }

#' View data in VisiData
#'
#' @description
#' Opens data in VisiData through the Terminal application on macOS. If the input is
#' an sf object, the geometry column will be dropped before viewing.
#'
#' @param data A data.frame, tibble, or sf object to view
#' @param title Character string for the Terminal window title. Defaults to "misc::view_vd"
#' @param type Either "csv" or "json" format for writing the temporary file. Use "json" for
#' preserving list-columns.
#'
#' @details
#' This function only works in interactive sessions on macOS. It creates a temporary file
#' and opens it in VisiData through the Terminal application. The temporary filename includes
#' a timestamp for identification.
#'
#' @return Returns the input data invisibly
#' @export
#'
#' @examples
#' \dontrun{
#' # View a data frame
#' mtcars %>% view_vd()
#'
#' # View with custom title
#' mtcars %>% view_vd(title = "Car Data")
#'
#' # View with list columns preserved
#' nested_df %>% view_vd(type = "json")
#' }
view_vd <- function(data, title = NULL, type = "csv") {
if (interactive()) {

## bin <- Sys.which("st")

if (is.null(title)) {
title <- "misc::view_vd"
}

if (class(data)[1] == "sf") {
message("Removing sf geometry...")
data_clean <-
sf::st_drop_geometry(data)
} else {
data_clean <- data
}

if (type == "csv") {
extfile <- ".csv"
tmp <- paste0(tempfile(), "___", format(Sys.time(), "D%Y%m%dT%H%M%S"), "___misc___visidata", extfile)
readr::write_csv(data_clean, tmp)
}

if (type == "json") {
extfile <- "json"
tmp <- paste0(tempfile(), "___", format(Sys.time(), "D%Y%m%dT%H%M%S"), "___misc___visidata", extfile)
jsonlite::write_json(data_clean, tmp)
}

## NOTE 2024-10-15: Use jsonlite::write_json to get list-columns
## see: https://github.com/paulklemm/rvisidata/issues/5
system(
## glue::glue("osascript -e 'tell app \"Terminal\" to do script \"vd --default-width=500 {tmp}\"' -e 'tell app \"Terminal\" to activate'")
glue::glue(
"osascript -e 'tell application \"Terminal\"
if not application \"Terminal\" is running then launch
do script \"vd --default-width=500 {tmp}\"
activate
end tell'
"
)
)

}
return(data)
}

#' View data frame in VisiData (non-interactive version)
#'
#' Opens a data frame in VisiData terminal viewer, saving to a fixed location in Downloads.
#' Similar to view_vd() but without interactive mode check.
#'
#' @param data A data frame or sf object to view
#' @param title Optional title for the viewer window (default: "misc::view_vd")
#' @return Returns the input data frame unchanged
#' @export
view_vd_nonint <- function(data, title = NULL) {
## bin <- Sys.which("st")

if (is.null(title)) {
title <- "misc::view_vd"
}

if (class(data)[1] == "sf") {
message("Removing sf geometry...")
data_clean <-
sf::st_drop_geometry(data)
} else {
data_clean <- data
}

tmp <- paste0("/Users/karloguidoni/Downloads/", "___", format(Sys.time(), "D%Y%m%dT%H%M%S"), "___misc___visidata.csv")
readr::write_csv(data_clean, tmp)
system(
## glue::glue("osascript -e 'tell app \"Terminal\" to do script \"vd --default-width=500 {tmp}\"' -e 'tell app \"Terminal\" to activate'")
glue::glue(
"osascript -e 'tell application \"Terminal\"
if not application \"Terminal\" is running then launch
do script \"vd --default-width=500 {tmp}\"
activate
end tell'
"
)
)
return(data)
}


#' View data frame in Excel or other spreadsheet viewer
#'
#' Opens a data frame in Microsoft Excel or another spreadsheet viewer. Also copies the data
#' to the system clipboard.
#'
#' @param data A data frame to view
#' @param viewer The spreadsheet viewer to use. One of "libreoffice", "gnumeric", "tad", or "excel" (default)
#' @return Returns nothing
#' @export
view_excel <- function(data, viewer = c("libreoffice", "gnumeric", "tad", "excel")) {
viewer <- match.arg(viewer)
## check_avail(viewer)
tmp <- paste0(tempfile(), ".xlsx")
clipr::write_clip(data)
message("Data is also on system clipboard!")
writexl::write_xlsx(data, tmp)
system(paste0("open -a 'Microsoft Excel'", " tmp "))
}

#' View spatial data from file path with optional map preview
#'
#' Reads a spatial data file (.shp or .gpkg) and optionally displays it in an interactive map preview.
#' The data is also opened in VisiData for tabular viewing.
#'
#' @param path Path to the spatial data file (.shp or .gpkg)
#' @param preview Logical. If TRUE, opens an interactive map preview in the browser. Default is FALSE.
#'
#' @details
#' The function performs the following steps:
#' 1. Validates that the input file exists and has the correct extension (.shp or .gpkg)
#' 2. Creates a temporary HTML file for the map preview in ~/.local/share/mapview/
#' 3. Reads the spatial data using sf::read_sf()
#' 4. If preview=TRUE, creates an interactive map using mapview and opens it in the browser
#' 5. Opens the attribute data in VisiData
#'
#' @return Returns nothing, called for side effects
#' @export
view_mapview_from_path <- function(path, preview = FALSE) {

stopifnot(fs::file_exists(path))

stopifnot(tolower(tools::file_ext(path)) == "shp" | tolower(tools::file_ext(path)) == "gpkg")

path_string <- deparse(substitute(path))

basename_filename <- tools::file_path_sans_ext(basename(path))

userhome <- Sys.getenv("HOME")

userlocal <- paste0(userhome, "/.local/share/mapview/")

fs::dir_create(userlocal)

tempmapfile <-
paste0(userlocal, basename_filename, "___", format(Sys.time(), "D%Y%m%dT%H%M%S"), "__mapview.html")

df <-
path |>
sf::read_sf()

if (preview) {

dinamic_map <-
mapview::mapview(df, map.types = c("OpenStreetMap"))

mapview::mapshot(
dinamic_map,
url = tempmapfile,
remove_controls = NULL,
title = basename_filename
)

system(paste0("open ", tempmapfile))

}

misc::view_vd_nonint(df)

}
6 changes: 4 additions & 2 deletions man/create_dirs.Rd

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

2 changes: 1 addition & 1 deletion man/setup_lintr.Rd

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

20 changes: 20 additions & 0 deletions man/view_excel.Rd

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

Loading

0 comments on commit e00fcce

Please sign in to comment.