问:
Fastest implementation of sine, cosine and square root in C++ (doesn't need to be much accurate)
I am googling the question for past hour, but there are only points to Taylor Series or some sample code that is either too slow or does not compile at all. Well, most answer I've found over Google is "Google it, it's already asked", but sadly it's not...

I am profiling my game on low-end Pentium 4 and found out that ~85% of execution time is wasted on calculating sinus, cosinus and square root (from standard C++ library in Visual Studio), and this seems to be heavily CPU dependent (on my I7 the same functions got only 5% of execution time, and the game is waaaaaaaaaay faster). I cannot optimize this three functions out, nor calculate both sine and cosine in one pass (there interdependent), but I don't need too accurate results for my simulation, so I can live with faster approximation.

So, the question: What are the fastest way to calculate sine, cosine and square root for float in C++?

EDIT
Lookup table are more painful as resulting Cache Miss is way more costly on modern CPU than Taylor Series. The CPUs are just so fast these days, and cache is not.

I made a mistake, I though that I need to calculate several factorials for Taylor Series, and I see now they can be implemented as constants.

So the updated question: is there any speedy optimization for square root as well?

EDIT2

I am using square root to calculate distance, not normalization - can't use fast inverse square root algorithm (as pointed in comment: http://en.wikipedia.org/wiki/Fast_inverse_square_root

EDIT3

I also cannot operate on squared distances, I need exact distance for calculations

答:
So let me rephrase that, this idea comes from approximating the cosine & sine functions on an interval [-pi/4,+pi/4] with a bounded error using the Remez algorithm. Then using the range reduced float remainder and a LUT for the outputs cos & sine of the integer quotient, the approximation can be moved to any angular argument.

Its just unique and I thought it could be expanded on to make a more efficient algorithm in terms of a bounded error.

```<code>void sincos_fast(float x, float *pS, float *pC){
    float cosOff4LUT[] = { 0x1.000000p+00,  0x1.6A09E6p-01,  0x0.000000p+00, -0x1.6A09E6p-01, -0x1.000000p+00, -0x1.6A09E6p-01,  0x0.000000p+00,  0x1.6A09E6p-01 };

    int     m, ms, mc;
    float   xI, xR, xR2;
    float   c, s, cy, sy;

    // Cody & Waite's range reduction Algorithm, [-pi/4, pi/4]
    xI  = floorf(x * 0x1.45F306p+00 + 0.5);              // This is 4/pi.
    xR  = (x - xI * 0x1.920000p-01) - xI*0x1.FB5444p-13; // This is pi/4 in two parts per C&W.
    m   = (int) xI;
    xR2 = xR*xR;

    // Find cosine & sine index for angle offsets indices
    mc = (  m  ) & 0x7;     // two's complement permits upper modulus for negative numbers =P
    ms = (m + 6) & 0x7;     // phase correction for sine.

    // Find cosine & sine
    cy = cosOff4LUT[mc];     // Load angle offset neighborhood cosine value
    sy = cosOff4LUT[ms];     // Load angle offset neighborhood sine value

    c = 0xf.ff79fp-4 + xR2 * (-0x7.e58e9p-4);               // TOL = 1.2786e-4
    // c = 0xf.ffffdp-4 + xR2 * (-0x7.ffebep-4 + xR2 * 0xa.956a9p-8);  // TOL = 1.7882e-7

    s = xR * (0xf.ffbf7p-4 + xR2 * (-0x2.a41d0cp-4));   // TOL = 4.835251e-6
    // s = xR * (0xf.fffffp-4 + xR2 * (-0x2.aaa65cp-4 + xR2 * 0x2.1ea25p-8));  // TOL = 1.1841e-8

    *pC = c*cy - s*sy;
    *pS = c*sy + s*cy;
}

float sqrt_fast(float x){
    union {float f; int i; } X, Y;
    float ScOff;
    uint8_t e;

    X.f = x;
    e = (X.i >> 23);           // f.SFPbits.e;

    if(x <= 0) return(0.0f);

    ScOff = ((e & 1) != 0) ? 1.0f : 0x1.6a09e6p0;  // NOTE: If exp=EVEN, b/c (exp-127) a (EVEN - ODD) := ODD; but a (ODD - ODD) := EVEN!!

    e = ((e + 127) >> 1);                            // NOTE: If exp=ODD,  b/c (exp-127) then flr((exp-127)/2)
    X.i = (X.i & ((1uL << 23) - 1)) | (0x7F << 23);  // Mask mantissa, force exponent to zero.
    Y.i = (((uint32_t) e) << 23);

    // Error grows with square root of the exponent. Unfortunately no work around like inverse square root... :(
    // Y.f *= ScOff * (0x9.5f61ap-4 + X.f*(0x6.a09e68p-4));        // Error = +-1.78e-2 * 2^(flr(log2(x)/2))
    // Y.f *= ScOff * (0x7.2181d8p-4 + X.f*(0xa.05406p-4 + X.f*(-0x1.23a14cp-4)));      // Error = +-7.64e-5 * 2^(flr(log2(x)/2))
    // Y.f *= ScOff * (0x5.f10e7p-4 + X.f*(0xc.8f2p-4 +X.f*(-0x2.e41a4cp-4 + X.f*(0x6.441e6p-8))));     // Error =  8.21e-5 * 2^(flr(log2(x)/2))
    // Y.f *= ScOff * (0x5.32eb88p-4 + X.f*(0xe.abbf5p-4 + X.f*(-0x5.18ee2p-4 + X.f*(0x1.655efp-4 + X.f*(-0x2.b11518p-8)))));   // Error = +-9.92e-6 * 2^(flr(log2(x)/2))
    // Y.f *= ScOff * (0x4.adde5p-4 + X.f*(0x1.08448cp0 + X.f*(-0x7.ae1248p-4 + X.f*(0x3.2cf7a8p-4 + X.f*(-0xc.5c1e2p-8 + X.f*(0x1.4b6dp-8))))));   // Error = +-1.38e-6 * 2^(flr(log2(x)/2))
    // Y.f *= ScOff * (0x4.4a17fp-4 + X.f*(0x1.22d44p0 + X.f*(-0xa.972e8p-4 + X.f*(0x5.dd53fp-4 + X.f*(-0x2.273c08p-4 + X.f*(0x7.466cb8p-8 + X.f*(-0xa.ac00ep-12)))))));    // Error = +-2.9e-7 * 2^(flr(log2(x)/2))
    Y.f *= ScOff * (0x3.fbb3e8p-4 + X.f*(0x1.3b2a3cp0 + X.f*(-0xd.cbb39p-4 + X.f*(0x9.9444ep-4 + X.f*(-0x4.b5ea38p-4 + X.f*(0x1.802f9ep-4 + X.f*(-0x4.6f0adp-8 + X.f*(0x5.c24a28p-12 ))))))));   // Error = +-2.7e-6 * 2^(flr(log2(x)/2))

    return(Y.f);
}
</code>```

The longer expressions are longer, slower, but more precise. Polynomials are written per Horner's rule.

Body:
So let me rephrase that, this idea comes from approximating the cosine & sine functions on an interval [-pi/4,+pi/4] with a bounded error using the Remez algorithm. Then using the range reduced float remainder and a LUT for the outputs cos & sine of the integer quotient, the approximation can be moved to any angular argument.

Its just unique and I thought it could be expanded on to make a more efficient algorithm in terms of a bounded error.

void sincos_fast(float x, float *pS, float *pC){
    float cosOff4LUT[] = { 0x1.000000p+00,  0x1.6A09E6p-01,  0x0.000000p+00, -0x1.6A09E6p-01, -0x1.000000p+00, -0x1.6A09E6p-01,  0x0.000000p+00,  0x1.6A09E6p-01 };

    int     m, ms, mc;
    float   xI, xR, xR2;
    float   c, s, cy, sy;

    // Cody & Waite's range reduction Algorithm, [-pi/4, pi/4]
    xI  = floorf(x * 0x1.45F306p+00 + 0.5);              // This is 4/pi.
    xR  = (x - xI * 0x1.920000p-01) - xI*0x1.FB5444p-13; // This is pi/4 in two parts per C&W.
    m   = (int) xI;
    xR2 = xR*xR;

    // Find cosine & sine index for angle offsets indices
    mc = (  m  ) & 0x7;     // two's complement permits upper modulus for negative numbers =P
    ms = (m + 6) & 0x7;     // phase correction for sine.

    // Find cosine & sine
    cy = cosOff4LUT[mc];     // Load angle offset neighborhood cosine value
    sy = cosOff4LUT[ms];     // Load angle offset neighborhood sine value

    c = 0xf.ff79fp-4 + xR2 * (-0x7.e58e9p-4);               // TOL = 1.2786e-4
    // c = 0xf.ffffdp-4 + xR2 * (-0x7.ffebep-4 + xR2 * 0xa.956a9p-8);  // TOL = 1.7882e-7

    s = xR * (0xf.ffbf7p-4 + xR2 * (-0x2.a41d0cp-4));   // TOL = 4.835251e-6
    // s = xR * (0xf.fffffp-4 + xR2 * (-0x2.aaa65cp-4 + xR2 * 0x2.1ea25p-8));  // TOL = 1.1841e-8

    *pC = c*cy - s*sy;
    *pS = c*sy + s*cy;
}

float sqrt_fast(float x){
    union {float f; int i; } X, Y;
    float ScOff;
    uint8_t e;

    X.f = x;
    e = (X.i >> 23);           // f.SFPbits.e;

    if(x <= 0) return(0.0f);

    ScOff = ((e & 1) != 0) ? 1.0f : 0x1.6a09e6p0;  // NOTE: If exp=EVEN, b/c (exp-127) a (EVEN - ODD) := ODD; but a (ODD - ODD) := EVEN!!

    e = ((e + 127) >> 1);                            // NOTE: If exp=ODD,  b/c (exp-127) then flr((exp-127)/2)
    X.i = (X.i & ((1uL << 23) - 1)) | (0x7F << 23);  // Mask mantissa, force exponent to zero.
    Y.i = (((uint32_t) e) << 23);

    // Error grows with square root of the exponent. Unfortunately no work around like inverse square root... :(
    // Y.f *= ScOff * (0x9.5f61ap-4 + X.f*(0x6.a09e68p-4));        // Error = +-1.78e-2 * 2^(flr(log2(x)/2))
    // Y.f *= ScOff * (0x7.2181d8p-4 + X.f*(0xa.05406p-4 + X.f*(-0x1.23a14cp-4)));      // Error = +-7.64e-5 * 2^(flr(log2(x)/2))
    // Y.f *= ScOff * (0x5.f10e7p-4 + X.f*(0xc.8f2p-4 +X.f*(-0x2.e41a4cp-4 + X.f*(0x6.441e6p-8))));     // Error =  8.21e-5 * 2^(flr(log2(x)/2))
    // Y.f *= ScOff * (0x5.32eb88p-4 + X.f*(0xe.abbf5p-4 + X.f*(-0x5.18ee2p-4 + X.f*(0x1.655efp-4 + X.f*(-0x2.b11518p-8)))));   // Error = +-9.92e-6 * 2^(flr(log2(x)/2))
    // Y.f *= ScOff * (0x4.adde5p-4 + X.f*(0x1.08448cp0 + X.f*(-0x7.ae1248p-4 + X.f*(0x3.2cf7a8p-4 + X.f*(-0xc.5c1e2p-8 + X.f*(0x1.4b6dp-8))))));   // Error = +-1.38e-6 * 2^(flr(log2(x)/2))
    // Y.f *= ScOff * (0x4.4a17fp-4 + X.f*(0x1.22d44p0 + X.f*(-0xa.972e8p-4 + X.f*(0x5.dd53fp-4 + X.f*(-0x2.273c08p-4 + X.f*(0x7.466cb8p-8 + X.f*(-0xa.ac00ep-12)))))));    // Error = +-2.9e-7 * 2^(flr(log2(x)/2))
    Y.f *= ScOff * (0x3.fbb3e8p-4 + X.f*(0x1.3b2a3cp0 + X.f*(-0xd.cbb39p-4 + X.f*(0x9.9444ep-4 + X.f*(-0x4.b5ea38p-4 + X.f*(0x1.802f9ep-4 + X.f*(-0x4.6f0adp-8 + X.f*(0x5.c24a28p-12 ))))))));   // Error = +-2.7e-6 * 2^(flr(log2(x)/2))

    return(Y.f);
}

The longer expressions are longer, slower, but more precise. Polynomials are written per Horner's rule.


来源: stackexchange

问:
MySQL, query with "NOT IN ( SELECT ... )" very slow
Here is the query runs over 10 minutes.

```<code>SELECT *
FROM tableA
WHERE name NOT IN(
SELECT a.name
FROM tableA a
INNER JOIN tableB b ON a.code = b.code
INNER JOIN tableC c ON c.number = b.number
INNER JOIN tableD d ON d.code = b.code
INNER JOIN tableE e ON e.content= d.content)
</code>```

Is there alternative query runs faster or any solutions? Thanks a lot.

答:
try not exists</p>

```<code>SELECT *
FROM tableA t1
WHERE not exists (
SELECT 1
from tableB b
INNER JOIN tableC c ON c.number = b.number
INNER JOIN tableD d ON d.code = b.code
INNER JOIN tableE e ON e.content= d.content
where t1.code = b.code

)
</code>```

ContentLicense: CC BY-SA 3.0

Body:
Here is the query runs over 10 minutes.

SELECT *
FROM tableA
WHERE name NOT IN(
SELECT a.name
FROM tableA a
INNER JOIN tableB b ON a.code = b.code
INNER JOIN tableC c ON c.number = b.number
INNER JOIN tableD d ON d.code = b.code
INNER JOIN tableE e ON e.content= d.content)

Is there alternative query runs faster or any solutions? Thanks a lot.

Body:
try not exists

SELECT *
FROM tableA t1
WHERE not exists (
SELECT 1
from tableB b
INNER JOIN tableC c ON c.number = b.number
INNER JOIN tableD d ON d.code = b.code
INNER JOIN tableE e ON e.content= d.content
where t1.code = b.code

)


问:
ScrollView won't scroll, just cuts off the screen. How to fix?
I know this question has been asked, but I haven't found anything that works. Here's my xml:

```<code><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.project.myproject.SpendFragment">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/testBannerAdId">
    </com.google.android.gms.ads.AdView>

    <ScrollView
        android:layout_above="@id/adView"
        android:padding="@dimen/layoutPadding"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/alabamaButton"
                    android:contentDescription="Alabama"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"/>

                <ImageButton
                    android:id="@+id/alaskaButton"
                    android:contentDescription="Alaska"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"/>

                <ImageButton
                    android:id="@+id/arizonaButton"
                    android:contentDescription="Arizona"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/arkansasButton"
                    android:contentDescription="Arkansas"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/californiaButton"
                    android:text="California"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/coloradoButton"
                    android:text="Colorado"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/connecticutButton"
                    android:text="Connecticut"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/delawareButton"
                    android:text="Delaware"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/floridaButton"
                    android:text="Florida"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/georgiaButton"
                    android:text="Georgia"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/hawaiiButton"
                    android:text="Hawaii"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/idahoButton"
                    android:text="Idaho"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/illinoisButton"
                    android:text="Illinois"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/indianaButton"
                    android:text="Indiana"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/iowaButton"
                    android:text="Iowa"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/kansasButton"
                    android:text="Kansas"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/kentuckyButton"
                    android:text="Kentucky"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/louisianaButton"
                    android:text="Louisiana"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/maineButton"
                    android:text="Maine"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/marylandButton"
                    android:text="Maryland"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/massachusettsButton"
                    android:text="Massachusetts"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/michiganButton"
                    android:text="Michigan"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/minnesotaButton"
                    android:text="Minnesota"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/mississippiButton"
                    android:text="Mississippi"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/missouriButton"
                    android:text="Missouri"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/montanaButton"
                    android:text="Montana"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/nebraskaButton"
                    android:text="Nebraska"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/nevadaButton"
                    android:text="Nevada"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>
        </LinearLayout>

    </ScrollView>

</RelativeLayout>
</code>```

In case it's relevant, here's the xml for the Main activity that the fragment's in:

```<code><android.support.design.internal.ScrimInsetsFrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context="com.project.myproject.MainActivity" >

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.NavigationView
            android:id="@+id/navigationView"
            android:layout_gravity="start"
            android:layout_width="@dimen/drawerWidth"
            android:layout_height="@dimen/drawerHeight"
            app:menu="@menu/drawer_menu" />

    </android.support.v4.widget.DrawerLayout>

</android.support.design.internal.ScrimInsetsFrameLayout>
</code>```

There are more LinearLayouts within the LinearLayout, but you get the idea (more ImageButtons of states). I can see the little scroll thing on the right once, but it disappears and doesn't come back up. And in case it's relevant, there's space for the AdView on the bottom but it's just white space. I know that happens sometimes with a real ad, so I just figure the little placeholder ad will show up eventually.

As you can see, my ScrollView's height is match_parent and my LinearLayout's height is wrap_content. I have one child in the ScrollView. The ScrollView is wrapped in a RelativeLayout only because I want the AdView to be under the ScrollView. Is the issue is that there are multiple children in the outer LinearLayout? I've never had any issues with ScrollViews before so this is a little annoying haha.

Thank you!

EDIT: Here are two images. The only difference is that one has the little scrollbar on the side and the other doesn't. It disappears and never reappears on touch or movement. The blue squares are the placeholder images and there are four squares per inner LinearLayout.

答:
Try adding bottom margin to ScrollView as see if it helps:

```<code>android:layout_marginBottom="?attr/actionBarSize"
</code>```

Body:
I know this question has been asked, but I haven't found anything that works. Here's my xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.project.myproject.SpendFragment">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/testBannerAdId">
    </com.google.android.gms.ads.AdView>

    <ScrollView
        android:layout_above="@id/adView"
        android:padding="@dimen/layoutPadding"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/alabamaButton"
                    android:contentDescription="Alabama"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"/>

                <ImageButton
                    android:id="@+id/alaskaButton"
                    android:contentDescription="Alaska"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"/>

                <ImageButton
                    android:id="@+id/arizonaButton"
                    android:contentDescription="Arizona"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/arkansasButton"
                    android:contentDescription="Arkansas"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/californiaButton"
                    android:text="California"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/coloradoButton"
                    android:text="Colorado"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/connecticutButton"
                    android:text="Connecticut"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/delawareButton"
                    android:text="Delaware"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/floridaButton"
                    android:text="Florida"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/georgiaButton"
                    android:text="Georgia"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/hawaiiButton"
                    android:text="Hawaii"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/idahoButton"
                    android:text="Idaho"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/illinoisButton"
                    android:text="Illinois"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/indianaButton"
                    android:text="Indiana"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/iowaButton"
                    android:text="Iowa"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/kansasButton"
                    android:text="Kansas"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/kentuckyButton"
                    android:text="Kentucky"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/louisianaButton"
                    android:text="Louisiana"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/maineButton"
                    android:text="Maine"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/marylandButton"
                    android:text="Maryland"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/massachusettsButton"
                    android:text="Massachusetts"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/michiganButton"
                    android:text="Michigan"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/minnesotaButton"
                    android:text="Minnesota"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/mississippiButton"
                    android:text="Mississippi"
                    android:src="@drawable/placeholder2"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:weightSum="100"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/missouriButton"
                    android:text="Missouri"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/montanaButton"
                    android:text="Montana"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/nebraskaButton"
                    android:text="Nebraska"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/nevadaButton"
                    android:text="Nevada"
                    android:src="@drawable/placeholder"
                    android:adjustViewBounds="true"
                    android:gravity="center"
                    android:layout_weight="25"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>
        </LinearLayout>

    </ScrollView>

</RelativeLayout>

In case it's relevant, here's the xml for the Main activity that the fragment's in:

<android.support.design.internal.ScrimInsetsFrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context="com.project.myproject.MainActivity" >

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.NavigationView
            android:id="@+id/navigationView"
            android:layout_gravity="start"
            android:layout_width="@dimen/drawerWidth"
            android:layout_height="@dimen/drawerHeight"
            app:menu="@menu/drawer_menu" />

    </android.support.v4.widget.DrawerLayout>

</android.support.design.internal.ScrimInsetsFrameLayout>

There are more LinearLayouts within the LinearLayout, but you get the idea (more ImageButtons of states). I can see the little scroll thing on the right once, but it disappears and doesn't come back up. And in case it's relevant, there's space for the AdView on the bottom but it's just white space. I know that happens sometimes with a real ad, so I just figure the little placeholder ad will show up eventually.

As you can see, my ScrollView's height is match_parent and my LinearLayout's height is wrap_content. I have one child in the ScrollView. The ScrollView is wrapped in a RelativeLayout only because I want the AdView to be under the ScrollView. Is the issue is that there are multiple children in the outer LinearLayout? I've never had any issues with ScrollViews before so this is a little annoying haha.

Thank you!

EDIT: Here are two images. The only difference is that one has the little scrollbar on the side and the other doesn't. It disappears and never reappears on touch or movement. The blue squares are the placeholder images and there are four squares per inner LinearLayout.

Body:
Try adding bottom margin to ScrollView as see if it helps:

android:layout_marginBottom="?attr/actionBarSize"


问:
Why does Internet Explorer not support includes()?
I am trying to support cross-browser compatibility, but the function includes() is not supported by IE ( Internet Explorer ). Does anyone know why this is the case? </p>

答:
Array.prototype.includes and String.prototype.includes is part of the ES7 (now ES2017) and ES6 (now ES2015) javascript specification respectively.

From wikipedia:

Internet Explorer ... is a discontinued series of graphical web browsers

key word: discontinued. Microsoft only develops Edge now. IE may still get security updates but it will no longer received new javascript updates or any browser specification updates.

includes() is not supporting in Internet Explorer , can you explain me why ?

Internet Explorer doesn't support Array.prototype.includes or String.prototype.includes because Microsoft no longer develops IE. Those specs came after Microsoft discontinued IE, therefore, it won't get those features.

BUT WAIT

You can still use new javascript in old browser by using polyfills.

You can use core-js as a library of polyfills.

To use includes(...) in IE, just include this at the top of of your  before any scripts.

```<code><script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/core.min.js"></script>
</code>```

Body:
Array.prototype.includes and String.prototype.includes is part of the ES7 (now ES2017) and ES6 (now ES2015) javascript specification respectively.

From wikipedia:

Internet Explorer ... is a discontinued series of graphical web browsers

key word: discontinued. Microsoft only develops Edge now. IE may still get security updates but it will no longer received new javascript updates or any browser specification updates.

includes() is not supporting in Internet Explorer , can you explain me why ?

Internet Explorer doesn't support Array.prototype.includes or String.prototype.includes because Microsoft no longer develops IE. Those specs came after Microsoft discontinued IE, therefore, it won't get those features.

BUT WAIT

You can still use new javascript in old browser by using polyfills.

You can use core-js as a library of polyfills.

To use includes(...) in IE, just include this at the top of of your <head> before any scripts.

<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/core.min.js"></script>


问:
Why is everything a property in iOS SDK tutorials?
In most of the iOS SDK tutorials I've read online both from Apple and elsewhere, so many instance variables are made properties, even when they are only accessed from within their own class.

E.G. in Facebook's latest iOS SDK a user is encouraged to maintain an instance of the Facebook class called facebook in the app delegate. facebook" is a property. This is then referred to throughout the sample code as "self.facebook". But the "facebook" property is not referenced anywhere from outside the app delegate.

Why is this? What am I missing? If an instance variable is only used within its own class, my inclination would be not to make it a property so that it is not publicly visible, and refer to it simply as facebook rather than self.facebook or  self->facebook from within the class.

Frankly, even if I needed the facebook property, I think I'd rather refer to it within the app delegate as simply "facebook" rather than "self.facebook".

So what gives? Why am I seeing properties everywhere?

答:
Properties can generate getters and setters.  Setters, in particular, can help manage retain/release counts of objects (semi)automatically, and thus reduce certain types of potential memory bugs.

Getters and properties for non-objects are mostly for orthogonality of coding style, although they do have potential to be used for mutators, key-value notifications, etc. in some future extension or reuse of the code.

There may be some efficiency differences/disadvantages, but that depends on the particular ObjC run-time in use.

ContentLicense: CC BY-SA 2.5


问:
Use R and Openxlsx to output a list of dataframes as worksheets in a single Excel file
I have a set of CSV files. I want to package them up and export the data to a single Excel file that contains multiple worksheets. I read in the CSV files as a set of data frames.

My problem is how to construct the command in openxlsx, I can do it manually, but I am having a list construction issue. Specifically how to add a data frame as a subcomponent of a named list and then pass as a parameter to write.xlsx()

Example

Ok, so I first list the CSV files on disk and generate a set of data frames in memory...

```<code># Generate a list of csv files on disk and shorten names...
filePath <- "../02benchmark/results/results_20170330/"
filePattern <- "*.csv"
fileListwithPath = list.files(path = filePath, pattern = filePattern, full.names = TRUE)
fileList = list.files(path = filePath, pattern = filePattern, full.names = FALSE)

datasets <- gsub("*.csv$", "", fileList)
datasets <- gsub("sample_", "S", datasets)
datasets

# Now generate the dataframes for each csv file...
list2env(
  lapply(setNames(fileListwithPath, make.names(datasets)),
         read.csv), envir = .GlobalEnv)
</code>```

Example Output:

```<code>dput(datasets)
c("S10000_R3.3.2_201703301839", "S10000_T4.3.0_201703301843",
"S20000_R3.3.2_201703301826", "S20000_T4.3.0_201703301832", "S280000_R3.3.2_201704020847",
"S280000_T4.3.0_201704021100", "S290000_R3.3.2_201704020447",
"S290000_T4.3.0_201704020702", "S30000_R3.3.2_201703301803",
"S30000_T4.3.0_201703301817", "S310000_R3.3.2_201704012331",
"S310000_T4.3.0_201704020242", "S320000_R3.3.2_201704011827",
"S320000_T4.3.0_201704012128", "S330000_R3.3.2_201704011304",
"S330000_T4.3.0_201704011546", "S340000_R3.3.2_201704010652",
"S340000_T4.3.0_201704011010", "S350000_R3.3.2_201704010020",
"S350000_T4.3.0_201704010404", "S360000_R3.3.2_201703311819",
"S360000_T4.3.0_201703312134", "S370000_R3.3.2_201703310914",
"S370000_T4.3.0_201703311301", "S380000_R3.3.2_201703310134",
"S380000_T4.3.0_201703310509", "S390000_R3.3.2_201703301846",
"S390000_T4.3.0_201703302252", "S40000_R3.3.2_201703301738",
"S40000_T4.3.0_201703301752", "S50000_R3.3.2_201703301707", "S50000_T4.3.0_201703301724",
"S60000_R3.3.2_201703301624", "S60000_T4.3.0_201703301647", "S70000_R3.3.2_201703301535",
"S70000_T4.3.0_201703301602", "S80000_R3.3.2_201703301430", "S80000_T4.3.0_201703301508",
"S90000_R3.3.2_201703301324", "S90000_T4.3.0_201703301400")
</code>```

Now we have a set of data frames and we wish to create a single excel file with multiple worksheets...

```<code>wb <- createWorkbook()
saveWorkbook(wb, 'output.xlsx')

lapply(names(myList), function(x) write.xlsx(myList[[x]], 'output.xlsx', sheetName=x, append=TRUE))
</code>```

Problem:

The problem is I can create the list structure manually and can confirm it works BUT I cannot seem to construct the list automatically.

```<code>myList <- sapply(datasets,function(x) NULL)
names(myList)
str(myList)
myList$S10000_R3.3.2_201703301839 <- eval(S10000_R3.3.2_201703301839)
</code>```

thus:

```<code>> str(myList)
List of 40
 $ S10000_R3.3.2_201703301839 :'data.frame':    43 obs. of  4 variables:
  ..$ function.: Factor w/ 42 levels "DF add random number vector",..: 30 25 38 42 36 39 40 29 26 22 ...
  ..$ user     : num [1:43] 2.144 0.263 0.024 0.068 0.008 ...
  ..$ system   : num [1:43] 0.63 0.065 0.001 0.004 0 ...
  ..$ elapsed  : num [1:43] 12.274 1.104 0.047 0.115 0.009 ...
 $ S10000_T4.3.0_201703301843 : NULL
 $ S20000_R3.3.2_201703301826 : NULL
 ...
</code>```

Specific Problem: How to append each data frame to the list...

```<code>myList <- lapply( myList, function(x) eval(x) )
</code>```

what am I doing wrong with lapply here? The above lapply() does not iterate through the list and append the data frame to the name list entry.

```<code>i.e. myList$S10000_R3.3.2_201703301839 <- eval(S10000_R3.3.2_201703301839)
> str(myList)
    List of 40
     $ S10000_R3.3.2_201703301839 :'data.frame':    43 obs. of  4 variables:
      ..$ function.: Factor w/ 42 levels "DF add random number vector",..: 30 25 38 42 36 39 40 29 26 22 ...
      ..$ user     : num [1:43] 2.144 0.263 0.024 0.068 0.008 ...
      ..$ system   : num [1:43] 0.63 0.065 0.001 0.004 0 ...
      ..$ elapsed  : num [1:43] 12.274 1.104 0.047 0.115 0.009 ...
     $ S10000_T4.3.0_201703301843 : NULL
     $ S20000_R3.3.2_201703301826 : NULL
     ...
</code>```

What am I missing? All help gratefully appreciated. Yes, I am pretty certain I am missing something obvious... but... I am stumped.

答:
I don't have your data frames, so I can't test this, but the code below is similar to the approach I use when I need to read and write Excel files. The code below uses the xlsx package, as that's what I'm familiar with, but hopefully you can adapt it if you need to use openxlsx.

```<code>library(xlsx)
</code>```

First, read the files into a list. Something like this:

```<code>filePath <- "../02benchmark/results/results_20170330/"
filePattern <- "*.csv"
fileListwithPath = list.files(path = filePath,
                              pattern = filePattern,
                              full.names = TRUE)
fileList = list.files(path = filePath, pattern = filePattern, full.names = FALSE)
fileListwithPath = setNames( fileListwithPath,
                             list.files(path = filePath, pattern = filePattern))
df.list = lapply(fileListwithPath, read.csv)

# Now we rename the List Names for use in worksheets...
# Remove .csv and sample_ prefix used in filenames...
# Reult in workbook S<size>_<R version>_<date>
names(df.list) <- gsub("\\.csv$","", names(df.list))
names(df.list) <- gsub("sample_","S", names(df.list))
</code>```

You now have a list in which each element is a data frame and each element's name is the name of the file. Now, let's write each data frame to a different worksheet in the same Excel workbook and then save the file as an xlsx file:

```<code>wb = createWorkbook()

lapply( names(df.list),
        function(df) {
          sheet = createSheet(wb, df)
          addDataFrame(df.list[[df]], sheet = sheet, row.names = FALSE)
          } )

saveWorkbook(wb, "My_workbook.xlsx")
</code>```

I've separated reading and writing the csv files for illustration, but you can combine them into a single function that reads each individual csv file and writes it to a new sheet in a single Excel workbook.

Body:
I have a set of CSV files. I want to package them up and export the data to a single Excel file that contains multiple worksheets. I read in the CSV files as a set of data frames.

My problem is how to construct the command in openxlsx, I can do it manually, but I am having a list construction issue. Specifically how to add a data frame as a subcomponent of a named list and then pass as a parameter to write.xlsx()

Example

Ok, so I first list the CSV files on disk and generate a set of data frames in memory...

# Generate a list of csv files on disk and shorten names...
filePath <- "../02benchmark/results/results_20170330/"
filePattern <- "*.csv"
fileListwithPath = list.files(path = filePath, pattern = filePattern, full.names = TRUE)
fileList = list.files(path = filePath, pattern = filePattern, full.names = FALSE)

datasets <- gsub("*.csv$", "", fileList)
datasets <- gsub("sample_", "S", datasets)
datasets

# Now generate the dataframes for each csv file...
list2env(
  lapply(setNames(fileListwithPath, make.names(datasets)),
         read.csv), envir = .GlobalEnv)

Example Output:

dput(datasets)
c("S10000_R3.3.2_201703301839", "S10000_T4.3.0_201703301843",
"S20000_R3.3.2_201703301826", "S20000_T4.3.0_201703301832", "S280000_R3.3.2_201704020847",
"S280000_T4.3.0_201704021100", "S290000_R3.3.2_201704020447",
"S290000_T4.3.0_201704020702", "S30000_R3.3.2_201703301803",
"S30000_T4.3.0_201703301817", "S310000_R3.3.2_201704012331",
"S310000_T4.3.0_201704020242", "S320000_R3.3.2_201704011827",
"S320000_T4.3.0_201704012128", "S330000_R3.3.2_201704011304",
"S330000_T4.3.0_201704011546", "S340000_R3.3.2_201704010652",
"S340000_T4.3.0_201704011010", "S350000_R3.3.2_201704010020",
"S350000_T4.3.0_201704010404", "S360000_R3.3.2_201703311819",
"S360000_T4.3.0_201703312134", "S370000_R3.3.2_201703310914",
"S370000_T4.3.0_201703311301", "S380000_R3.3.2_201703310134",
"S380000_T4.3.0_201703310509", "S390000_R3.3.2_201703301846",
"S390000_T4.3.0_201703302252", "S40000_R3.3.2_201703301738",
"S40000_T4.3.0_201703301752", "S50000_R3.3.2_201703301707", "S50000_T4.3.0_201703301724",
"S60000_R3.3.2_201703301624", "S60000_T4.3.0_201703301647", "S70000_R3.3.2_201703301535",
"S70000_T4.3.0_201703301602", "S80000_R3.3.2_201703301430", "S80000_T4.3.0_201703301508",
"S90000_R3.3.2_201703301324", "S90000_T4.3.0_201703301400")

Now we have a set of data frames and we wish to create a single excel file with multiple worksheets...

wb <- createWorkbook()
saveWorkbook(wb, 'output.xlsx')

lapply(names(myList), function(x) write.xlsx(myList[[x]], 'output.xlsx', sheetName=x, append=TRUE))

Problem:

The problem is I can create the list structure manually and can confirm it works BUT I cannot seem to construct the list automatically.

myList <- sapply(datasets,function(x) NULL)
names(myList)
str(myList)
myList$S10000_R3.3.2_201703301839 <- eval(S10000_R3.3.2_201703301839)

thus:

> str(myList)
List of 40
 $ S10000_R3.3.2_201703301839 :'data.frame':    43 obs. of  4 variables:
  ..$ function.: Factor w/ 42 levels "DF add random number vector",..: 30 25 38 42 36 39 40 29 26 22 ...
  ..$ user     : num [1:43] 2.144 0.263 0.024 0.068 0.008 ...
  ..$ system   : num [1:43] 0.63 0.065 0.001 0.004 0 ...
  ..$ elapsed  : num [1:43] 12.274 1.104 0.047 0.115 0.009 ...
 $ S10000_T4.3.0_201703301843 : NULL
 $ S20000_R3.3.2_201703301826 : NULL
 ...

Specific Problem: How to append each data frame to the list...

myList <- lapply( myList, function(x) eval(x) )

what am I doing wrong with lapply here? The above lapply() does not iterate through the list and append the data frame to the name list entry.

i.e. myList$S10000_R3.3.2_201703301839 <- eval(S10000_R3.3.2_201703301839)
> str(myList)
    List of 40
     $ S10000_R3.3.2_201703301839 :'data.frame':    43 obs. of  4 variables:
      ..$ function.: Factor w/ 42 levels "DF add random number vector",..: 30 25 38 42 36 39 40 29 26 22 ...
      ..$ user     : num [1:43] 2.144 0.263 0.024 0.068 0.008 ...
      ..$ system   : num [1:43] 0.63 0.065 0.001 0.004 0 ...
      ..$ elapsed  : num [1:43] 12.274 1.104 0.047 0.115 0.009 ...
     $ S10000_T4.3.0_201703301843 : NULL
     $ S20000_R3.3.2_201703301826 : NULL
     ...

What am I missing? All help gratefully appreciated. Yes, I am pretty certain I am missing something obvious... but... I am stumped.

Body:
I don't have your data frames, so I can't test this, but the code below is similar to the approach I use when I need to read and write Excel files. The code below uses the xlsx package, as that's what I'm familiar with, but hopefully you can adapt it if you need to use openxlsx.

library(xlsx)

First, read the files into a list. Something like this:

filePath <- "../02benchmark/results/results_20170330/"
filePattern <- "*.csv"
fileListwithPath = list.files(path = filePath,
                              pattern = filePattern,
                              full.names = TRUE)
fileList = list.files(path = filePath, pattern = filePattern, full.names = FALSE)
fileListwithPath = setNames( fileListwithPath,
                             list.files(path = filePath, pattern = filePattern))
df.list = lapply(fileListwithPath, read.csv)

# Now we rename the List Names for use in worksheets...
# Remove .csv and sample_ prefix used in filenames...
# Reult in workbook S<size>_<R version>_<date>
names(df.list) <- gsub("\\.csv$","", names(df.list))
names(df.list) <- gsub("sample_","S", names(df.list))

You now have a list in which each element is a data frame and each element's name is the name of the file. Now, let's write each data frame to a different worksheet in the same Excel workbook and then save the file as an xlsx file:

wb = createWorkbook()

lapply( names(df.list),
        function(df) {
          sheet = createSheet(wb, df)
          addDataFrame(df.list[[df]], sheet = sheet, row.names = FALSE)
          } )

saveWorkbook(wb, "My_workbook.xlsx")

I've separated reading and writing the csv files for illustration, but you can combine them into a single function that reads each individual csv file and writes it to a new sheet in a single Excel workbook.


问:
To assign ng-model to the input type file when clicking the button
```<code>    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script>
    var app = angular.module("myApp",[]);
    app.controller("myCtrl", function($scope) {
    $scope.users=[
     {name:'',email:'',course:'',file:''}
      ]
      $scope.value=function(){
       $scope.users=[{
         name:'abc',
         email:'abc@gmail.com',
         course:'angular js,css',
         file:'abc.jpg';
       }];
     }
     });
   </script>
         <div ng-app="myApp" ng-controller="myCtrl">
     <button type="button" ng-click="value()">To get values</button>
   <div ng-repeat="user in users">
     <input type="text" ng-model="user.name">
     <input typee="text" ng-model="user.email">
     <select ng-model="user.course" multiple>
       <option value="html">html </option>
       <option value="css">css</option>
       <option value="angular js">angular js</option>
     </select>
     <input type="file" ng-model="user.file">
    </div>
  </div>
</code>```

I am  new to the angular js I want to assign the file name  to the input type file when clicking the button to get values i want to only assign value and diaplay the value of input type file and how to bind ng-model for the multiple select box

答:
```<code>.directive("fileread", [function () {
return {
    scope: {
        fileread: "="
    },
    link: function (scope, element, attributes) {
        element.bind("change", function (changeEvent) {
            var reader = new FileReader();
            reader.onload = function (loadEvent) {
                scope.$apply(function () {
                    scope.fileread = loadEvent.target.result;
                });
            }
            reader.readAsDataURL(changeEvent.target.files[0]);
        });
    }
}
</code>```

<p>}]);</p>

```<code><input type="file" fileread="vm.uploadme" />
</code>```

Body:
     <button type="button" ng-click="value()">To get values</button>

     <input type="text" ng-model="user.name">
     <input typee="text" ng-model="user.email">
     <select ng-model="user.course" multiple>
       <option value="html">html </option>
       <option value="css">css</option>
       <option value="angular js">angular js</option>
     </select>
     <input type="file" ng-model="user.file">

I am  new to the angular js I want to assign the file name  to the input type file when clicking the button to get values i want to only assign value and diaplay the value of input type file and how to bind ng-model for the multiple select box

Body:
.directive("fileread", [function () {
return {
    scope: {
        fileread: "="
    },
    link: function (scope, element, attributes) {
        element.bind("change", function (changeEvent) {
            var reader = new FileReader();
            reader.onload = function (loadEvent) {
                scope.$apply(function () {
                    scope.fileread = loadEvent.target.result;
                });
            }
            reader.readAsDataURL(changeEvent.target.files[0]);
        });
    }
}

}]);

<input type="file" fileread="vm.uploadme" />