What Leads the First Element of a Printed List to Be Enclosed with Backticks in R V3.5.1

What leads the first element of a printed list to be enclosed with backticks in R v3.5.1?

This is a bug in R 3.5.1 only on Windows. It has been fixed in r-devel as of 17 August 2018.

In R, in list(), the first name is surrounded by `` automatically, Why?

This will happen if you use numeric values as list elements names. This is done to clearly differentiate the list indices (e.g. list[1]) from the list names (e.g. list$'1' - wrong quotes du to formatting here).

In your example:

list <- list(f=c(1,2),h=c(1,2))
$f
[1] 1 2

$h
[1] 1 2

The elements f and h can be accessed through naming or indexing:

# Naming
list$f
[1] 1 2

# Indexing
list[1]
$f
[1] 1 2

# Indexing (alternative)
list[[1]]
[1] 1 2

On the other hand, if your list names have numbers they will be coerced as non-numeric values to avoid confusion:

list <- list("2"=c(1,2),h=c(1,2))
$`2`
[1] 1 2

$h
[1] 1 2

# Naming
list$`2`
[1] 1 2

# Indexing
list[1]
$`2`
[1] 1 2

# Indexing (alternative)
list[[1]]
[1] 1 2

Strange (distortred) output in RStudio notebook in R 3.5.1

It appears that this is a bug that affects GUI applications using R 3.5.1 on Windows (e.g. RGui and RStudio). For example, you can see a similar effect with:

x <- 1
print(list())
save(x, file = tempfile())
output <- encodeString("apple")
print(output)

Sourcing this gives, for me:

> source('~/encoding.R')
list()
[1] "\002ÿþapple\003ÿþ"

We'll have a fix in the next version of RStudio, but for now the workaround is to just avoid printing empty lists before printing data frames in R.

How can I join elements of an array in Bash?

A 100% pure Bash function that supports multi-character delimiters is:

function join_by {
local d=${1-} f=${2-}
if shift 2; then
printf %s "$f" "${@/#/$d}"
fi
}

For example,

join_by , a b c #a,b,c
join_by ' , ' a b c #a , b , c
join_by ')|(' a b c #a)|(b)|(c
join_by ' %s ' a b c #a %s b %s c
join_by $'\n' a b c #a<newline>b<newline>c
join_by - a b c #a-b-c
join_by '\' a b c #a\b\c
join_by '-n' '-e' '-E' '-n' #-e-n-E-n-n
join_by , #
join_by , a #a

The code above is based on the ideas by @gniourf_gniourf, @AdamKatz, @MattCowell, and @x-yuri. It works with options errexit (set -e) and nounset (set -u).

Alternatively, a simpler function that supports only a single character delimiter, would be:

function join_by { local IFS="$1"; shift; echo "$*"; }

For example,

join_by , a "b c" d #a,b c,d
join_by / var local tmp #var/local/tmp
join_by , "${FOO[@]}" #a,b,c

This solution is based on Pascal Pilz's original suggestion.

A detailed explanation of the solutions previously proposed here can be found in "How to join() array elements in a bash script", an article by meleu at dev.to.

Find two consecutive rows

Assuming the rows have sequential IDs, something like this may be what you're looking for:

select top 1 * 
from
Bills b1
inner join Bills b2 on b1.id = b2.id - 1
where
b1.IsEstimate = 1 and b2.IsEstimate = 1
order by
b1.BillDate desc

Purpose of __repr__ method?

__repr__ should return a printable representation of the object, most likely one of the ways possible to create this object. See official documentation here. __repr__ is more for developers while __str__ is for end users.

A simple example:

>>> class Point:
... def __init__(self, x, y):
... self.x, self.y = x, y
... def __repr__(self):
... cls = self.__class__.__name__
... return f'{cls}(x={self.x!r}, y={self.y!r})'
>>> p = Point(1, 2)
>>> p
Point(x=1, y=2)

Doctrine - How to print out the real sql, not just the prepared statement?

Doctrine is not sending a "real SQL query" to the database server : it is actually using prepared statements, which means :

  • Sending the statement, for it to be prepared (this is what is returned by $query->getSql())
  • And, then, sending the parameters (returned by $query->getParameters())
  • and executing the prepared statements

This means there is never a "real" SQL query on the PHP side — so, Doctrine cannot display it.



Related Topics



Leave a reply



Submit