-
Notifications
You must be signed in to change notification settings - Fork 205
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
Easier specification of external file dependencies #83
base: master
Are you sure you want to change the base?
Conversation
R/fileDependency.R
Outdated
#' Mark a string as an attachment | ||
#' @export | ||
attachment <- function(x){ | ||
structure(normalizePath(x), class = unique(c("ATTACHMENT", oldClass(x)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why oldClass
vs. class
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just used the same code as used in htmlwidgets:::JS
. I should admit I did not understand why oldClass
was used there in the first place 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should also probably add a file.exists(x)
check in the attachment
function to ensure that incorrect paths are caught early on.
R/fileDependency.R
Outdated
#' @export | ||
attachment <- function(x){ | ||
if (!file.exists(x)){ | ||
stop("The attachment ", x, " does not exist") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about just leaving it to normalizePath(mustWork = TRUE)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion! I will make the change.
This would be a most-welcome feature. I have been looking at this issue in my attempt to attach a file dependency that gets created in one of my functions before |
@nsh87 Does this data need to be packaged as a separate file? Could it perhaps be included as part of the createWidget's |
@jcheng5 It's an XML file, so I don't think it can be included with radial_phylo <- function(user-data, width=NULL, height=NULL) {
# Do some stuff to save an XML file to xmlFilePath
phyloxml <- htmltools::htmlDependency(
name = 'phyloxmls',
version = '1.0',
src = c(file=xmlFilePath)
attachment = list(xml = xmlFilePath) # Pretty sure this isn't right
)
# create widget
htmlwidgets::createWidget(
name = 'radial_phylo',
x,
width = width,
height = height,
htmlwidgets::sizingPolicy(
viewer.padding = 0,
viewer.suppress = suppressViewer,
browser.fill = TRUE
),
package = 'receptormarker',
dependencies = phyloxml
)
} |
I think that's pretty close--in the JavaScript side you should then be able to call |
That JS call works, but I'm not sure if the reference is right. If I inspect the attachment in the HTML and click on the link it's just a blank XML file (and var phyloxmlfile = HTMLWidgets.getAttachmentUrl('phyloxmls', 'xml');
// phyloxmlfile = "lib/phyloxml-1.0//Users/Nikhil/dev/receptormarker/receptormarker/phyloxml-69647efd9cfd.xml" |
Plus I don't have a |
This chunk: phyloxml <- htmltools::htmlDependency(
name = 'phyloxmls',
version = '1.0',
src = c(file=xmlFilePath)
attachment = list(xml = xmlFilePath) # Pretty sure this isn't right
) Should be this: phyloxml <- htmltools::htmlDependency(
name = 'phyloxmls',
version = '1.0',
src = c(file=dirname(xmlFilePath))
attachment = list(xml = basename(xmlFilePath)) # Pretty sure this isn't right
) Note that the entire contents of |
Awesome, that did the trick. Lines 7 and 8 in |
OK, great, glad to hear it. :) |
@jcheng5 Should we revisit this PR (i need some cleanup so it is mergeable)? All this PR does is provide a wrapper around the dependency API that you have put in. So maybe one way to go about this is to merge this in, acknowledging the limitations you have pointed out, and then once we have a more complete way of doing this, update the mechanism. Let me know what you think. |
👍 +1 |
I think it might be a simple tweak to get it working for dynamic values. Give me a minute... |
@jcheng5 I look forward to your magic 👍 |
Without this change, the attachment() feature might return stale data, since it identified file dependencies only by basename.
OK, I pushed some changes. Some notes:
|
Thanks @jcheng5. I will play with your PR and do some testing. I am okay with the dependencies on |
This PR makes it easy for package developers and users to specify file dependencies for their widgets. Here is an example:
It uses a similar mechanism to the one used by the
JS
function to mark objects as javascript literals. The resolution of attachment urls is carried out using theHTMLWidgets.getAttachmentUrl
function.@jcheng5 can you take a look at this whenever you have time. @jjallaire @timelyportfolio @yihui if you can test this functionality with any of your widgets and provide me with feedback/comments, that would be very useful.