xtable with latex math symbols in the table - latex

I'm trying to write this table with checkboxes using the xtable package. It would seem that the checkboxes that I've chosen are what is throwing the error. I'm just at a loss how to fix it.
library(xtable)
## I want to create a table with the names of some people in two columns
nStu = 10
## Create fake names
names = character(nStu)
for(i in 1:nStu){
names[i] = paste(LETTERS[i],rep(letters[i],5),sep='',collapse='')
}
## put check boxes behind each of the names
squares = rep('$ \\square $',nStu)
## Build the table
rosterTab = data.frame('Name'=names,'Mostly'=squares,'Sometimes'=squares, stringsAsFactors = FALSE)
## Now chop it in half and paste the halves together. (Yes, if nStu is odd, this will have to be fixed)
lTab = nStu%/%2
aTab = rosterTab[1:lTab, ]
bTab = rosterTab[(lTab+1):nStu, ]
outTab = cbind(aTab,bTab)
## Everything before this point runs fine.
outTab.tab = xtable(outTab,label=FALSE)
align(outTab.tab) = 'llcc||lcc'
print(outTab.tab, include.rownames=FALSE, sanitize.text.function = function(x){x})
The error message that I'm getting is:
Error in as.string(y) : Cannot coerce argument to a string
This error goes away if I use:
squares = rep('aaa',nStu)
Ideally, I want to get the names from a csv file (which I can do quite easily), and will use knitr to write this into a LaTeX document. (I want to do this for a bunch of input files, so automating this task seems useful to me.)
Here are some other ideas that I've considered:
A $\LaTeX$ only solution. Note, that there is one other difficulty (other than the squares not being in the input file), and that is that I would need to do some text manipulation on the strings in the input file.
Replacing the \square symbol with some other object that looks like a checkbox, that (through knitr) I can send to $\LaTeX$.

Related

`knitr_out, `file_out` and `vis_drake_graph` usage in R:drake

I'm trying to understand how to use knitr_out, file_out and vis_drake_graph properly in drake.
I have two questions.
Q1: Usage of knitr_out and file_out to create markdown reports
While a code like this works correctly for one of my smaller projects:
make_hyp_data_aggregated_report <- function() {
render(
input = knitr_in("rmd/hyptest-is-data-being-aggregated.Rmd"),
output_file = file_out("~/projectname/reports/01-hyp-test.html"),
quiet = TRUE
)
}
plan <- drake_plan(
...
...
hyp_data_aggregated_report = make_hyp_data_aggregated_report()
...
...
)
Exactly similar code in my large project (with ~10+ reports) doesn't work exactly right. i.e., while the reports get built, the knitr_in objects don't get displayed as the blue squares in the graph using drake::vis_drake_graph() in my large project.
Both projects use the drake::loadd(....) within the markdown to get the objects from cache.
Is there some code in vis_drake_graph that removes these squares once the graph gets busy?
Q2: file_out objects in vis_drake_graph
Is there a way to display the file_out objects themselves as circles/squares in vis_drake_graph?
Q3: packages showing up in vis_drake_graph
Is there a way to avoid vis_drake_graph from printing the packages explicitly? (Basically anything with the ::)
Q1
Every literal file path needs its own knitr_in() or file_out(). If you have one function with one knitr_in(), even if you use the function multiple times, that still only counts as one file path. I recommend writing these keywords at the plan level, e.g.
plan <- drake_plan(
r1 = render(knitr_in("report1.Rmd"), output_file = file_out("report1.html")),
r2 = render(knitr_in("report2.Rmd"), output_file = file_out("report2.html")),
r3 = render(knitr_in("report3.Rmd"), output_file = file_out("report3.html"))
)
Q2
They should appear unless you set show_output_files = FALSE in vis_drake_graph().
Q3
No, but if it's any consolation, I do regret the decision to track namespaced functions and objects at all in drake. drake's approach is fundamentally suboptimal for tracking packages, and I plan to get rid of it if there ever comes time for a round of breaking changes. Otherwise, there is no way to get rid of it except vis_drake_graph(targets_only = TRUE), which also gets rid of all the imports in the graph.

HL7 Encoding/Separator Characters

In regards to HL7 pipe-delimited data, how exactly do the encoding characters (|^~\&) work?
Is the following example of fields, field repetitions, components and their sub-components correct when parsing raw HL7 data?
PID|1||||||||||||1234567890^somedata&moredata^TESTEMAIL#GMAIL.COM~0987654321
Field (|):
PID13 = 1234567890^somedata&moredata^TESTEMAIL#GMAIL.COM~0987654321
Field repetition (~):
PID13~1 = 1234567890^somedata&moredata^TESTEMAIL#GMAIL.COM
PID13~2 = 0987654321
Component (^):
PID13.1 = 1234567890
PID13.2 = somedata&moredata
PID13.3 = TESTEMAIL#GMAIL.COM
Sub-component (&):
PID13.2.1 = somedata
PID13.2.2 = moredata
PID13.3.1 = TESTEMAIL#GMAIL.COM
PID13.3.2 =
Without understanding the left-hand side structure you're trying to assign stuff to, it's impossible to tell you if you're doing it right.
There is however one right way to parse the segment/field in question.
Here's a link to the specs I reference here
From section 2.5.3 of the HL7v2.7 Standard:
Each field is assigned a data type that defines the value domain of the field – the possible values that it may
take.
If you pull up section 3.4.2.13 (PID-13) you'll see a breakdown of each component and subcomponent. Technically, the meaning of subcomponents and components can vary by field, but mostly they just vary by data type.
In your example, you don't treat the repetitions as separate instances of XTN data types. I would re-write using array syntax as so:
Field repetition (~):
PID13[0] = 1234567890^somedata&moredata^TESTEMAIL#GMAIL.COM
PID13[1] = 0987654321
Component (^):
PID13[0].1 = 1234567890
PID13[0].2 = somedata&moredata
PID13[0].3 = TESTEMAIL#GMAIL.COM
Sub-component (&):
PID13[0].2.1 = somedata
PID13[0].2.2 = moredata
The psuedo-code in the same specification section 2.6.1 may be helpful as well
foreach occurrence in ( occurrences_of( field ) ) {
construct_occurrence( occurrence );
if not last ( populated occurrence ) insert repetition_separator;
/* e.g., ~ */
}
It's important to remember that those different subcomponents have different meaning because PID-13 is a XTN type.
PID-13 is a problematic example because historically, the order of PID-13 mattered. The first repetition was "primary". Over time the field has also become the landing place for e-mail addresses, pager numbers, etc. So good luck trying to make sense out of real-world data.

mapping graduated symbols in arcpy for data with changing max value

I have a pyscript that I am working on and running into issues with the legend.
The goal of the script is to take a csv file with two columns (country, value) that I am joining to a world map based on country name. Once they are joined I turn it into a lyr file and then start to work on the mapping aspects of it.
I want to change the field being mapped to 'value' and then change the mapping to graduated symbols with 5 breaks (at 5,20,50,75,100 % of the max value in the map). The problem being that depending on what layer file is being used the max value changes. I have spent a lot of time searching for how to do this and have still come up short, there are probably some unnecessary bits to my code as well as I am now to python.
How can I get it to map based on graduated symbol based on 5 percent breaks with a changing max value?
Some of the code I have been working on below,
import os, sys, string, arcpy, arcpy.mapping, glob, arcgisscripting,time
from arcpy import env
call blank mxd file, set feature layer, Declare variables
mxd = arcpy.mapping.MapDocument(workspace/blank_4.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
'newname' is the layer file being used
sourceLayer = arcpy.mapping.Layer(newname)
Convert feature classes to layer objects
outLayer = str(newname)
curmap = workspace + filename + ".shp"
arcpy.MakeFeatureLayer_management(curmap, outLayer)
Save layer objects to layer files
LayerFile = str(outLayer) + ".lyr"
arcpy.SaveToLayerFile_management(outLayer, LayerFile)
Define variable for the new layer using LayerFile object
newlayer = arcpy.mapping.Layer(LayerFile)
Adds the new layer to top of TOC in the mxd doc map document
arcpy.mapping.AddLayer(df, newlayer, "TOP")
change the mapping field from country name to fert value
lyr = arcpy.mapping.ListLayers(mxd, newlayer, df)[0]
symbols = arcpy.mapping.Layer(C:\workspace\graduated_symbol2.lyr")
graduated symbols is a blank layer file where I created a legend with 5 breaks (5,20,50,75,100 %) but the range was created based on a max value of 22,000 and now when I run a file with a max value of 35,000 anything above 22,000 becomes blank.
How can I get it to purely work on % breaks, and have the 0/max range of the respective csv file be used??????
Update new layer with sybmology from the source layer
arcpy.ApplySymbologyFromLayer_management(lyr,symbols)
arcpy.mapping.UpdateLayer(df, lyr, symbols, True)
change symbology, colors, and breaks
if lyr.symbologyType == "GRADUATED_COLORS":
lyr.symbology.valueField = "value"
print lyr.symbology.numClasses #
Then I output this to a pdf, but that part is working.
Thanks!

randomly selection of images from File

I have a file that contains a 400 images. What I want is to separate this file into two files: train_images and test_images.
The train_images should contains 150 images selected randomly, and all these images must be different from each other. Then, the test_images should also contains 150 images selected randomly, and should be different from each other, even from the images selected in the file train_images.
I begin by writing a code that aims to select a random number of images from a Faces file and put them on train_images file. I need your help in order to respond to my behavior described above.
clear all;
close all;
clc;
Train_images='train_faces';
mkdir(Train_images);
ImageFiles = dir('Faces');
totalNumberOfImages = length(ImageFiles)-1;
scrambledList = randperm(totalNumberOfImages);
numberIWantToUse = 150;
loop_counter = 1;
for index = scrambledList(1:numberIWantToUse)
baseFileName = ImageFiles(index).name;
str = fullfile('faces', baseFileName); % Better than STRCAT
face = imread(str);
imwrite( face, fullfile(Train_images, ['hello' num2str(index) '.jpg']));
loop_counter = loop_counter + 1;
end
Any help will be very appreciated.
Your code looks good to me. When you implement the test, you can re-run the scrambledList = randperm(totalNumberOfImages); then select the first 150 elements in scrambledList as you did in training process.
You can also directly re-initialize the loop:
for index = scrambledList(numberIWantToUse+1 : 2*numberIWantToUse)
... % same thing you wrote in your training loop
end
with this approach, your test sample will be completely different from the training sample.
Supposing that you have the Bioinformatics Toolbox, you can use crossvalind using the parameter HoldOut:
This is an example. trainand test are logical arrays, so you can use findto get the actual indexes:
ImageFiles = dir('Faces');
ImageFilesIndexes = ones(1,length(ImageFiles )) %Use a numeric array instead the char array
proportion = 150/400; %Testing set
[train,test] = crossvalind('holdout',ImageFilesIndexes,proportion );
training_files = ImageFiles(train); %250 files: It is better to use more data to train
testing_files = ImageFiles(test); %150 files
%Then do whatever you like with the files
Other possibilities are dividerand ( Neural Network Toolbox) and cvpartition (Statistics Toolbox)

Can I define powerpoints or presentations in raw text?

Hi Guys : I wanted to create a power point presentation using a raw text file, so that i can rapidly edit the file and see the results, gauranteed with uniform formatting. Basically, I'm talking about separating the data content from the presentation.
How can I do this ? Im thinking that maybe Latex could be a choice. I've also seen that there are API's for Powerpoint and open office presentations.
Powerpoint exposes it's API via COM - which makes it possible to do (almost) anything that you can do in the GUI in any programming language that supports COM. The difficulty is that the API changes between releases. One way to scope out the API is to use the macro recording facility to manually do one slide, and then translate that to your target language.
I've got some old (not tested recently) python code for Powerpoint 2003 that should give you an idea of what the code might look like depending on your layout needs.
from win32com.client import Dispatch
ppLayoutTitle = 1
ppLayoutText = 2
def writePresentation( fname, data ):
pptApp = Dispatch('Powerpoint.Application')
pres = pptApp.ActivePresentation
aw = pptApp.ActiveWindow
slides = pres.Slides
for item in data:
t1 = item[0]
t2 = item[1]
stype = item[2]
assert(stype in [ppLayoutTitle,ppLayoutText])
s = slides.Add( slides.Count, stype )
aw.View.GotoSlide(s.SlideIndex)
s.Shapes.Item(1).Select()
aw.Selection.ShapeRange.TextFrame.TextRange.Select()
aw.Selection.ShapeRange.TextFrame.TextRange.Characters(Start=1, Length=0).Select
tr = aw.Selection.TextRange
tr.Text = t1
s.Shapes.Item(2).Select()
aw.Selection.ShapeRange.TextFrame.TextRange.Select()
if stype == ppLayoutText:
aw.Selection.ShapeRange.TextFrame.TextRange.ParagraphFormat.Bullet.Visible = 0
aw.Selection.ShapeRange.TextFrame.TextRange.Characters(Start=1, Length=0).Select
tr = aw.Selection.TextRange
tr.Text = t2
slides.Range(slides.Count).Delete()
pres.SaveAs(fname)
Edit:
Openoffice (that can export to powerpoint) also ships with it's scripting API that could be used to solve similar problems.
If all you need is slides with titles and bulleted text, it's quite simple. Create a txt file that looks like this (use the TAB key in place of below):
Slide 1 Title
<tab>Bullet Level One Text
<tab><tab>Bullet Level Two Text
<tab>Back to Bullet Leven One again
Slide 2 Title
Slide 3 Title
<tab>More Bulleted text
<tab><tab>Tufte hates us by now
<tab><tab>But we don't care, do we?
Slide 4 Title
And so on. Save the file, start PowerPoint, choose the file open command, choose Outline or files of all types in the file open dialog box and select your TXT file. Done.

Resources