Feature Request from Doug Martin and Tom Philips

Paul A. Rubin parubin73 at gmail.com
Mon Jan 18 23:17:36 UTC 2021


On 1/18/21 5:12 PM, Richard Kimberly Heck wrote:
> On 1/18/21 2:49 PM, Paul A. Rubin wrote:
>> On 1/18/21 2:03 PM, Doug Martin wrote:
>>>
>>>
>>> On Mon, Jan 18, 2021 at 10:41 AM Scott Kostyshak <skostysh at lyx.org 
>>> <mailto:skostysh at lyx.org>> wrote:
>>>
>>>     On Mon, Jan 18, 2021 at 07:25:42PM +0100, Jean-Marc Lasgouttes
>>>     wrote:
>>>     > Le 14/01/2021 à 05:34, Doug Martin a écrit :
>>>     > > JMarc and all,
>>>     > >
>>>     > > Tom and I use knitr extensively for R code chunks, and we
>>>     mostly use
>>>     > > kable with kableExtra to make tables.
>>>     > > The input to kable are R data frames, or data.tables, which
>>>     are the
>>>     > > result of model fitting and related calculations.
>>>     > > But we like to put mathematical expressions in selected
>>>     cells of tables,
>>>     > > which is so easy with LYX tables, and we currently
>>>     > > have to make the data entry into LYX by hand from data
>>>     tables and
>>>     > > data.tables in order to make use of that feature.
>>>     > > So it would be great if we could import R data tables and
>>>     data.tables
>>>     > > into LYX tables, rather than using the kable/kableExtra
>>>     > > solution for our tables  (maybe I didn't make that clear in
>>>     my earlier
>>>     > > email).  Then we would probably would drop use of
>>>     > > kable/kableExtra.
>>>     >
>>>     > So you want to import as .tex the result of R processing. This
>>>     can be done
>>>     > via "Paste from LaTeX". What would be missing for your
>>>     intended usage?
>>>
>>>     From what I understand, they would like to import a .Rds file
>>>     without
>>>     having to manually convert it to LaTeX.
>>>
>>>
>>> Scott,
>>>
>>> Definitely correct on the "without" part.  But we want to directly 
>>> import an R object
>>> of class data.frame or data.table into an LYX table.
>>>
>>> If we have to export such an object first, we would typically export 
>>> it to an .Rda object.
>>> But it would be far more convenient to not have to do that.
>>>
>>> Doug
>>>
>>>
>>>     Scott
>>>
>>>
>>>
>>> -- 
>>> R. Douglas Martin
>>> Professor Emeritus in Applied Mathematics and Statistics
>>> Founder and Former Director of MS-CFRM Program
>>> depts.washington.edu/compfin/ <http://depts.washington.edu/compfin/>
>>> University of Washington
>>>
>> Doug,
>>
>> I'm not sure that what you want (direct import without first 
>> exporting) is possible. Keep in mind that the source data frames / 
>> tables live inside an R session, to which LyX probably does not have 
>> access. So I'm pretty sure you will need to manually export the R 
>> objects, either by saving to .Rda files (and then importing them into 
>> a LyX document using a converter) or by running an R function/script 
>> that converts them to LaTeX or LyX source (or something else LyX can 
>> ingest).
>
> Presumably one could script the R session itself so that the needed 
> object was exported? I.e., can one do that from the command line? 
> That's the kind of thing that an external template would do.
>
> Riki
>
>
Riki,

What do you have in mind by "external template"? Definitely you can 
export objects from within the R session, whether it is done by entering 
a line of code at the command line, or adding code to a script or 
notebook, or doing something interactive from within a Shiny web 
interface or an IDE. It seems they are currently doing that now (with 
kable()). What I don't think is possible is to have both LyX and an R 
session running, go to LyX, and run a command sequence or script that 
would say "go into my R session, grab the data frame named 'results', 
and import it as a LyX table". Specifically, I don't think the "go into 
my R session" part is doable.

With the right stuff installed, I think a viable option is to use the 
clipboard. I'll use the (in)famous iris data frame (which comes 
preinstalled in R) as an example. Assume that I have both R and LyX 
open, and the 'clipr' and 'xtable' R packages loaded. The command 
'write_clip(capture.output(xtable(iris)))' formats the iris data frame 
as a LaTeX table and crams it into the system clipboard. In the LyX 
document, Edit > Paste Special > Paste from LaTeX imports it as a table 
(in a float inset, which can be dissolved if not wanted). The R command 
can be made into a function that can be loaded automatically when an R 
session starts, and I'm pretty sure I could turn it into an add-in with 
a shortcut in RStudio. Note that this would not require any changes to LyX.

Paul

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lyx.org/pipermail/lyx-devel/attachments/20210118/6fab3aaa/attachment-0001.html>


More information about the lyx-devel mailing list