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

print.htmlTable ignores useViewer = TRUE #27

Open
lemna opened this issue Feb 6, 2017 · 19 comments
Open

print.htmlTable ignores useViewer = TRUE #27

lemna opened this issue Feb 6, 2017 · 19 comments

Comments

@lemna
Copy link
Collaborator

lemna commented Feb 6, 2017

Hi Max,

I can't get htmlTable to use the RStudio viewer pane anymore (version 1.9). The class of a htmlTable object is now "html" "htmlTable" "character" Everything works fine after manually setting the class to "htmlTable" "character". Is it possible that the fix for issue #26 caused this?

thanks
Peter.

@gforge
Copy link
Owner

gforge commented Feb 6, 2017

Hi Peter,

yes, that was the fix for issue #26. I like when the table appears inside the notebook and adding the html-class seemed to do the job. This was a bummer though. Any suggestions on how to address this?

/Max

@lemna
Copy link
Collaborator Author

lemna commented Feb 6, 2017

Hi Max,

I've got a clumsy workaround using the rstudioapi package, but I think the long-term solution should be provided by an environment parameter in rstudio or knitr (as you've been asking for, apparently).

I can do a PR tonight if you're still interested in the workaround.

Peter.

@gforge
Copy link
Owner

gforge commented Feb 6, 2017

Awesome. It seems like it may take a while before the rstudio team has time to fix this. I should probably post an rstudio issue

@gforge
Copy link
Owner

gforge commented Feb 8, 2017

Thanks, excellent work as always. I'll post it to CRAN in a few days. I don't think any unit tests are possible here and the previous viewer code was untested.

@gforge gforge closed this as completed Feb 8, 2017
@gforge
Copy link
Owner

gforge commented Feb 11, 2017

Hi Peter,

Just noticed I often don't use the html_notebook but frequently the Gmisc::docx_document, my current thoughts are:

  • For new files we retain the current structure
  • Detect the path and look if it's has an Rmd file-ending: grepl("\\.Rmd$", rstudioapi::getActiveDocumentContext()$path)
  • Add an option for escaping notebook in the yaml header + a general option options(htmlTable.skip_notebook = TRUE)

What are your thoughts on this?

@gforge gforge reopened this Feb 11, 2017
@lemna
Copy link
Collaborator Author

lemna commented Feb 12, 2017

Hi Max,

I avoided parsing the filename as path is NULL for files that haven't been saved yet. That is common in my workflow: create a file, try out the tables interactively, save after a while if everything works as expected. What do you want docx_document to do? It's a wrapper around html_document, so is the current behavior (use viewer in interactive use instead of inline display) not what you want it to do?

The problem with a general option is that a user might switch between different types of markdown files during a session. You would need to be able to reset that option when focus changes, but AFAIK RStudio does not provide hooks. In the absence of a way to detect where code is submitted from you will always need to parse the active document to detect what the user intended, I'm afraid.

edit: Totally forgot that this is also influenced by the global RStudio option - "show output inline for all markdown documents." htmlTable should respect this, of course. It seems that it is readable through an undocumented method.

Peter.

@gforge
Copy link
Owner

gforge commented Feb 13, 2017

Thanks Peter, I've updated the package and fixed a few minor bugs and moved the Rmd detection to a private function since your PR:

prIsNotebook <- function() {
  if (!rstudioapi::isAvailable()) {
    return(FALSE)
  }

  ctxt <- rstudioapi::getActiveDocumentContext()
  if (grepl("\\.Rmd$", ctxt$path)) {
    return(TRUE)
  }

  # Look for html_notebook within the header if the file hasn't been saved
  contents <- ctxt$contents
  header <- grep("^---$", contents)
  if (length(header) == 2) {
    return(any(grepl("html_notebook$",
                     contents[min(header) : max(header)])))
  }

  return(FALSE)
}

I think this is a reasonable compromise until further. The thought of having an Rcmd check note feels like less appealing as the folks at CRAN won't like it. Although I think one can add globalVariables(".rs.readUiPref") in order to avoid this error.

@gforge
Copy link
Owner

gforge commented Feb 13, 2017

Oh forgot, the docx_document is a wrapper around html_document and creates output that has the same color, font etc as a standard Word-docx document. I'm stuck with using Word for my manuscripts and then I want to just copy-paste the document without any hassle.

I think our workflow is rather similar. I just found that trying out tables within the document is even easier than viewing them through the Viewer since many tables relate to the information in the previous table.

I think that a single option is enough for now. Having a document-specific options seems like overdoing it although one could add a vector of filenames that are searched.

@lemna
Copy link
Collaborator Author

lemna commented Feb 13, 2017

It doesn't work for me though. If code is submitted from a .Rmd file the function returns TRUE without running the notebook detection part.
Also, this ignores the user's preference setting - would you reconsider the globalVariables(".rs.readUIPref") approach?

@gforge
Copy link
Owner

gforge commented Feb 13, 2017

In my case I sometimes have child documents that are of Rmd type but don't have a yaml intro. It seems that this requires relying on the file path for it to work since I can't deduce it from the context I'm in.

We can use .rs.readUiPref("rmd_chunk_output_inline") but currently it seems to always be NULL when I try it in regular docs and in notebook - are you sure the setting name is rmd_chunk_output_inline?

@lemna
Copy link
Collaborator Author

lemna commented Feb 13, 2017

I hadn't considered child documents, but as you say these will always have a non-NULL path. The current develop version does not work for html_document - it returns TRUE before evaluating the notebook detection code.

I am using RStudio version 1.0.136 - I can read the preference from the console, from within a .Rmd html_document and from a html_notebook.

@lemna
Copy link
Collaborator Author

lemna commented Feb 13, 2017

Another complication to consider: according to the Rmd specification it is possible to specify multiple output formats in a single yaml header. I think the only correct way forward is to find a way to determine which hook knitr will use to print something. All other solutions will make somebody unhappy.

@gforge
Copy link
Owner

gforge commented Feb 13, 2017

Found these similar issues:

I use 1.0.136 but neither the Windows nor the Linux version provide any useful output.

@lemna
Copy link
Collaborator Author

lemna commented Feb 13, 2017

> .rs.readUiPref("rmd_chunk_output_inline")
[1] TRUE

when the checkbox "Tools > Global Options... > R Markdown > Show output inline for all R Markdown documents" is checked.

> .rs.readUiPref("rmd_chunk_output_inline")
[1] FALSE

when the checkbox is unchecked. Tested under windows on three different installations.
Anyway, I don't think this is the way forward. the knitr print functions are better I think - RStudio will do the right thing when the correct one is selected.

@robsalasco
Copy link

any news on this?

@gforge
Copy link
Owner

gforge commented May 25, 2017

I haven't worked on this for a while. From looking at the rmarkdown bugs the activity has been low over there as well.

@dannyparsons
Copy link

I get this issue when using htmlTable function but I noticed that when I use concatHtmlTables I don't get this issue, i.e. print works correctly with useViewer = TRUE or useViewer = FALSE without needing to change the class manually.
Probably something you already know about but just thought I'd mention it in case. Do you know the reason it seems to behave correctly in this case?

@gforge
Copy link
Owner

gforge commented Jun 7, 2017

@dannyparsons I don't think I've adapted concatHtmlTables so it relies on the pre-notebook approach. I haven't heard back from any of the RStudio team and I guess they are busy on more pressing issues. Unfortunately I've been busy with other projects lately an I haven't had the time to dive into this issue again, any help is appreciated and you're welcome checking how concatHtmlTables work:

  • In plain .R file
  • In a regular Rmd file (both when just running the block + knitting the entire thing)
  • In a notebook (both above + with the different option settings specific to the notebook)

Thanks!

@dannyparsons
Copy link

Thanks for the reply, I certainly understand being busy with multiple projects! I'll try to get time to look into your suggestions and get back if I find anything useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants