Increase the width of matrix printout - r

When loading a matrix consisting of 12 columns into R, and then printing it, the terminal window in OS X cuts the matrix in half, sort to speak, first showing all the rows with the initial 7 columns and then showing all the rows again with the remaining 5 columns. However, I would like it to display ll the columns, rather than dividing it up. How can I accomplish this?

Andrie's answer is good, though sometimes one uses a super duper monitor and 9999 is not enough. ;-)
Here's my function for setting the display width:
setWidth <- function (width = NULL)
{
if (is.null(width)) {
columns <- as.numeric(Sys.getenv("COLUMNS"))
if (!is.na(columns)) {
options(width = columns)
}
else {
options(width = 100)
}
}
else {
options(width = width)
}
}
This has been addressed previously, though.
So, to improve on just the changing of width, another trick that I recommend: change the number of digits used in numeric output - set options(digits = ...) to a smaller value. See ?options for more info.

Related

ifc.js hiding example: hover highlights elements in non-displayed subsets

The issue is with the Hiding example (subset example with checkboxes):
helloworld/main/examples/web-ifc-viewer/hiding/bundle.js
When a subset is not-displayed, hover still highlights the element in the non-displayed subset. The solution as proposed on Discord was to remove the subset model from the array: ifcviewer.context.items.pickableIfcModels,
but it's not clear to me how or where to do this. Thanks for any help.
most probably because " ifcviewer.context.items.pickableIfcModels" is still thinks that you're dealing with the model as a whole not with the subsets,
you need to add this function:
function togglePickable(mesh, isPickable)
pickable = viewer.context.items.pickableIfcModels
if(isPickable) {
pickable.push(mesh)
} else {
const index = pickable.indexof(mesh)
pickable.splice(index, 1)
}
and on setupCheckbox function add this line under
if(checkbox.checked){
..
togglePickable(subset, true)
}else{
...
togglePickable(subset, false)
}
and on setupCategory function add the following:
togglePickable(subset[category], true)
and on your load model function you also need to add:
togglePickable("your model", true)
and it shall work ^^

iText7 large table autoLayout()

According to the docs
public Table(int numColumns,
boolean largeTable)
Constructs a Table with specified number of columns. The final column widths depend on selected table layout. Since 7.0.2 table layout algorithms were introduced. Auto layout is default, except large tables. For large table fixed layout set implicitly. Since 7.1 table will have undefined column widths, that will be determined during layout. In oder to set equal percent width as column width, use UnitValue.createPercentArray(int)
I render a large table using https://developers.itextpdf.com/examples/tables/clone-large-tables
Is there a way to define autoLayout? Maybe after adding the first row, get the cell widths and set them on the table, but that doesn't seem possible, because the column widths are null because I am using the constructor with number of columns.
Or adding some sort of autoLayout when end page is reached.
I don't want to define the widths for the columns because we have lots of tables.
First of all I would like to mention that auto layout requires the content of the whole table. The content is used when calculating the column widths. But you are using large table, which probably means you have a lot of data and you don't want to keep everything in memory (if that's not the case, just don't use large tables).
Thus, all you can do is calculate an approximation of the automatic column widths given some initial cells. Basically, it is possible to implement your first idea, however, it takes some code to be written. But if you have very different content in cells across different rows (e.g. images vs inner tables vs some text), then this method might not work very well because as I said, to estimate column widths well you need all the content.
Please also bear in mind that this approach is quite dirty and might not work for some corner cases. But it does solve the goal and frees you of the necessity to define column widths.
To describe the solution in a few words, we take cells of several initial rows, add them to a temporary table and layout it (estimate positions etc), without actually drawing it anywhere. Then we extract the cell widths from the layout step information and can use them for the large table constructor.
The method estimating column widths looks like this:
private UnitValue[] estimateWidths(Document document, Cell[][] cells) {
int numOfColumns = cells[0].length;
Table table = new Table(numOfColumns);
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
table.addCell(cells[i][j]);
}
}
LayoutContext context = new LayoutContext(document.getRenderer().getCurrentArea().clone());
TableRenderer tableRenderer = (TableRenderer)table.createRendererSubTree();
LayoutResult result = tableRenderer.setParent(document.getRenderer()).layout(context);
if (result.getStatus() == LayoutResult.PARTIAL) {
tableRenderer = (TableRenderer) result.getSplitRenderer();
}
UnitValue[] widths = new UnitValue[numOfColumns];
List<IRenderer> subList = tableRenderer.getChildRenderers().subList(0, numOfColumns);
for (int i = 0; i < subList.size(); i++) {
IRenderer cell = subList.get(i);
widths[i] = UnitValue.createPointValue(cell.getOccupiedArea().getBBox().getWidth());
}
return widths;
}
So assuming you have a Cell[][] cells array of cells for initial couple of rows (can be one row as well, but the more the better), where cells[i][j] refers to the cell at row i and column j, you can create your large table like this:
Table table = new Table(estimateWidths(doc, cells), true);
But don't forget to explicitly add cells from cells array to the large table before adding new content.

Combine into function and iterate

I am attempting to combine a series of loops/functions into one all-encompassing function to then be able to see the result for different input values. While the steps work properly when standalone (and when given just one input), I am having trouble getting the overall function to work. The answer I am getting back is a vector of 1s, which is incorrect.
The goal is to count the number of occurrences of consecutive zeroes in the randomly generated results, and then to see how the probability of consecutive zeroes occurring changes as I change the initial percentage input provided.
Does anyone have a tip for what I'm doing wrong? I have stared at this at several separate points now but cannot figure out where I'm going wrong. Thanks for your help.
### Example
pctgs_seq=seq(0.8,1,.01)
occurs=20
iterations=10
iterate_pctgs=function(x) {
probs=rep(0,length(pctgs_seq))
for (i in 1:length(pctgs_seq)) {
all_sims=lapply(1:iterations, function (x) ifelse(runif(occurs) <= i, 1, 0))
totals=sapply(all_sims,sum)
consec_zeroes=function (x) {
g=0
for (i in 1:(length(x)-1))
{ g= g+ifelse(x[i]+x[i+1]==0,1,0) }
return (g) }
consec_zeroes_sim=sapply(all_sims,consec_zeroes)
no_consec_prob=sum(consec_zeroes_sim==0)/length(consec_zeroes_sim)
probs[i]=no_consec_prob }
return (probs)
}
answer=iterate_pctgs(pctgs_seq)

Mathematically scale large elements lesser then small elements

10 elements with the class xxx have different widths and heights. Putting transform: scale(1.1) enlarges the big ones clearly but the small ones barely show difference. This is bad UX. The mathematical question is how to make the bigger elements scale less then the smaller ones:
width 10 should get scale 1.1
width 5 should get scale 1.2
How can i mathematically solve this?
The question lacks context and details, so it is hard to give a generally meaningful answer. However, the given examples indicate the following solution:
x_new = 1 + 1/x_old
Where x_old is the input value, i.e. 10 or 5.
Using logarithmic scaling instead of just 1/x_old might be another option, depending on the context.
To illustrate the scenarios i made these pens:
non logarithmic scale: http://codepen.io/anon/pen/bwRVpj
logarhitmic scale: http://codepen.io/anon/pen/VKWLJK
var inlineStyle = ''
var divs = document.getElementsByTagName('div')
var len = divs.length
while(len--) {
var elWidth = divs[len].offsetWidth
var scale = 1+9/elWidth
inlineStyle += `#${divs[len].id}:hover {
transform: scale(${scale})
}`
}
document.getElementById('lolStyle').innerHTML = inlineStyle

Writing the results of the for loop

We were trying to write the results from a for loop. We tried to use write.table, as.data.frame and other solutions, but with no success. We expect to have a data frame.
Currently we have only the loop, that shows year and values from a matrix which are bigger than 50. Looks like that:
for (i in 1:nrow(dobowe1)) {
if(dobowe1[i,4]>50) {
cat(dobowe1[i,"rok"],dobowe1[i,4], "\n")
}
}
Note: We don't do programming a lot, so it's hard to use other solutions from the questions that already beed asked.
Try to save each element to the vector, like here:
tabela <- numeric(nrow(dobowe1))
for (i in 1:nrow(dobowe1)) {
if(dobowe1[i,4]>50) {
tabela[i] <- paste(dobowe1[i,"rok"],dobowe1[i,4])
}
}
as.data.frame(tabela)
If you just want to visually inspect a subset of your matrix, you can just print out a filtered subset:
# create the filter:
> f <- dobowe1[,4] > 50
# use the filter to subset (index) your data.frame:
> dobowe1[f,c("rok", whatever-4th-var-is-called)]
This will automatically print it out. Or you can write it to a file with ?write.table

Resources