Bergam0t/nhs_ptd_power_bi

Fix issues preventing use on PowerBI Service

Closed this issue · 4 comments

At present use on PowerBI service fails due to the function any_of from tidyselect.

Error in any_of(value_field) : could not find function "any_of"

Please try again later or contact support.

This is either just plain not supported in PBI service or comes from a newer version of the package than the service supports.

Determine what package can be used to do the same thing, or copy in any_of from the source of tidyselect if not.

Microsoft seem very slow at updating R packages or approving new packages so think a workaround is going to be necessary.

May be other issues to resolve too once this one is sorted! But this is the only one that it's possible to see at the moment.

This is turning into quite a big job as the version of dplyr and tidyselect offered on the PowerBI service are absolutely ancient (~ July 2019 for dplyr) and are missing a lot of important helper functions - across() and any_of() have been the main issue so far.

This is also highlighting how difficult this sort of visual is to debug - I've reached a point where it is still working on the PowerBI desktop version but failing on the service with the error message 'point_type not found' - but no further traceback is available. point_type appears 45 times across 8 files!

I suspect this is going to cause problems with the method used to add the images to the plot too (as at the moment they pull from the repo online, not locally, due to the way plotly expects images to be passed in - only web support is mentioned)

The Power BI service applies a sandbox technology to protect users and the service from security risks.

This sandbox approach imposes some restrictions on the R scripts running in the Power BI service, such as accessing the Internet, or accessing to other resources that aren't required to create the R visual.

If the icon web access issue does turn out to be a problem (and if it isn't for general visual use, then it may be if we wish to pursue visual certification), then this helper function looks promising: https://github.com/plotly/plotly.R/blob/0469eab8afed182d789c79af4def894fbac3807c/R/helpers.R#L13

#' Encode a raster object as a data URI
#' 
#' Encode a raster object as a data URI, which is suitable for 
#' use with `layout()` \href{https://plotly.com/r/reference/#layout-images}{images}.
#' This is especially convenient for embedding raster images on a plot in 
#' a self-contained fashion (i.e., so they don't depend on external URL links).
#' 
#' @param r an object coercable to a raster object via [as.raster()]
#' @param ... arguments passed onto [as.raster()].
#' @author Carson Sievert
#' @export
#' @references <https://plotly-r.com/embedding-images.html>
#' @examplesIf interactive() || !identical(.Platform$OS.type, "windows")
#' 
#' # a red gradient (from ?as.raster)
#' r <- as.raster(matrix(hcl(0, 80, seq(50, 80, 10)), nrow = 4, ncol = 5))
#' plot(r)
#' 
#' # embed the raster as an image
#' plot_ly(x = 1, y = 1) %>% 
#'   layout(
#'     images = list(list(
#'      source = raster2uri(r),
#'      xref = "paper", 
#'      yref = "paper", 
#'      x = 0, y = 0, 
#'      sizex = 0.5, sizey = 0.5, 
#'      xanchor = "left", yanchor = "bottom"
#'   ))
#'  ) 

raster2uri <- function(r, ...) {
  try_library("png", "raster2uri")
  # should be 4 x n matrix
  if (inherits(r, "nativeRaster")) {
    # png::writePNG directly supports nativeRaster objects
    png <- r
  } else {
    r <- grDevices::as.raster(r, ...)
    rgbs <- col2rgb(c(r), alpha = T) / 255
    nr <- dim(r)[1]
    nc <- dim(r)[2]
    reds <- matrix(rgbs[1, ], nrow = nr, ncol = nc, byrow = TRUE)
    greens <- matrix(rgbs[2, ], nrow = nr, ncol = nc, byrow = TRUE)
    blues <- matrix(rgbs[3, ], nrow = nr, ncol = nc, byrow = TRUE)
    alphas <- matrix(rgbs[4, ], nrow = nr, ncol = nc, byrow = TRUE)
    png <- array(c(reds, greens, blues, alphas), dim = c(dim(r), 4))
  }
  base64enc::dataURI(png::writePNG(png), mime = "image/png")
}

Would require changing the images from SVG to a raster format (probably png) - bit of a shame for scalability. But worth considering as an option.

Fixed in #18