问:
How to run multiple kafka consumers on the same box independent of each other?
I have two Kafka consumer ConsumerA and ConsumerB. I want to run these two kafka consumers independent of each other on the same machine. There is no relation between them at all. These two kafka consumer will work on different topics on the same machine.

Each consumer should have a different Properties object.

Each consumer should have a different thread  pool configuration since they can be run in multithreaded way (consumer group) if needed independent of other consumer.

Below is my design:

Consumer class (abstract):

```<code> public abstract class Consumer implements Runnable {
    private final Properties consumerProps;
    private final String consumerName;

    public Consumer(String consumerName, Properties consumerProps) {
        this.consumerName = consumerName;
        this.consumerProps = consumerProps;
    }

    protected abstract void shutdown();
    protected abstract void run(String consumerName, Properties consumerProps);

    @Override
    public final void run() {
        run(consumerName, consumerProps);
    }
}
</code>```

ConsumerA class:

```<code>public class ConsumerA extends Consumer {
    private final AtomicBoolean closed = new AtomicBoolean(false);
    private KafkaConsumer<byte[], byte[]> consumer;

    public ConsumerA(String consumerName, Properties consumerProps) {
        super(consumerName, consumerProps);
    }

    @Override
    public void shutdown() {
        closed.set(true);
        consumer.wakeup();
    }

    @Override
    protected void run(String consumerName, Properties consumerProps) {
        consumer = new KafkaConsumer<>(consumerProps);
        consumer.subscribe(getTopicsBasisOnConsumerName());

        Map<String, Object> config = new HashMap<>();
        config.put(Config.URLS, TEST_URL);
        GenericRecordDomainDataDecoder decoder = new GenericRecordDomainDataDecoder(config);

        try {
            while (!closed.get()) {
                ConsumerRecords<byte[], byte[]> records = consumer.poll(Long.MAX_VALUE);
                for (ConsumerRecord<byte[], byte[]> record : records) {
                    GenericRecord payload = decoder.decode(record.value());
                    // extract data from payload
                    System.out.println("topic = %s, partition = %s, offset = %d, customer = %s, country = %s\n",
                                      record.topic(), record.partition(), record.offset(), record.key(), record.value());
                }
                consumer.commitAsync();
            }
        } catch (WakeupException ex) {
            // Ignore exception if closing
            System.out.println("error= ", ex);
            if (!closed.get()) throw e;
        } catch (Exception ex) {
            System.out.println("error= ", ex);
        } finally {
            try {
                consumer.commitSync();
            } finally {
                consumer.close();
            }
        }
    }
}
</code>```

ConsumerA B class:

```<code>// similar to `ConsumerA` but with specific details of B
</code>```

ConsumerHandler class:

```<code>public final class ConsumerHandler {
  private final ExecutorService executorServiceConsumer;
  private final Consumer consumer;
  private final List<Consumer> consumers = new ArrayList<>();

  public ConsumerHandler(Consumer consumer, int poolSize) {
    this.executorServiceConsumer = Executors.newFixedThreadPool(poolSize);
    this.consumer = consumer;
    for (int i = 0; i < poolSize; i++) {
      this.consumers.add(consumer);
      executorServiceConsumer.submit(consumer);
    }
 }
  public void shutdown() {
    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        for (Consumer consumer : consumers) {
          consumer.shutdown();
        }
        executorServiceConsumer.shutdown();
        try {
          executorServiceConsumer.awaitTermination(1000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ex) {
          Thread.currentThread().interrupt();
        }
      }
    });
  }
}
</code>```

Below is my main class in one of my project where if I start my server, calls will come first automatically and from this place I start my all kafka consumers where I execute my ConsumerA and ConsumerB. And as soon as shutdown is called, I release all the resources by calling shutdown on all my Kafka consumers.

```<code>import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Singleton;

@Singleton
@DependencyInjectionInitializer
public class Initializer {
  private ConsumerHandler consumerHandlerA;
  private ConsumerHandler consumerHandlerB;

  @PostConstruct
  public void init() {
    consumerHandlerA = new ConsumerHandler (new ConsumerA("consumerA", getConsumerPropsA()), 3);
    consumerHandlerB = new ConsumerHandler (new ConsumerB("consumerB", getConsumerPropsB()), 3);
  }

  @PreDestroy
  public void shutdown() {
    consumerHandlerA.shutdown();
    consumerHandlerB.shutdown();
  }
}
</code>```

Is this the right design for this kind of problem where I want to run multiple kafka consumers on the same box? Let me know if there is any better and efficient way to solve this problem. In general I will be running three or four Kafka consumers max on the same box and each consumer can have their own consumer group if needed.

Here is the Javadoc for KafkaConsumer which I am using in both my consumer. And basis on this article I have created my consumer, it is just that I have used abstract class to extend it. Search for "Putting it all Together" in that link.

In the docs it is mentioned that Consumers are not Thread-Safe but it looks like my code is reusing the same consumer instance for each thread in the pool.

```<code>public ConsumerHandler(Consumer consumer, int poolSize) {
    this.executorServiceConsumer = Executors.newFixedThreadPool(poolSize);
    this.consumer = consumer;
    for (int i = 0; i < poolSize; i++) {
      this.consumers.add(consumer);
      executorServiceConsumer.submit(consumer);
    }
 }
</code>```

What is the best way to solve this thread safety issue and still achieve the same features?

答:
Easiest Solution to solve "What is the best way to solve this thread safety issue and still achieve the same features?" :

Do not implement multi threading (Thread API/Executor Service) but instead use and run each consumer as a single consumer in its own separate JVM process, so if you need 4 consumers on same machine and you dont want to deal with mutli threading headaches then have your kafka consumer code JAR run in its own 4 separate Java processes.

Body:
I have two Kafka consumer ConsumerA and ConsumerB. I want to run these two kafka consumers independent of each other on the same machine. There is no relation between them at all. These two kafka consumer will work on different topics on the same machine.

Each consumer should have a different Properties object.

Each consumer should have a different thread  pool configuration since they can be run in multithreaded way (consumer group) if needed independent of other consumer.

Below is my design:

Consumer class (abstract):

 public abstract class Consumer implements Runnable {
    private final Properties consumerProps;
    private final String consumerName;

    public Consumer(String consumerName, Properties consumerProps) {
        this.consumerName = consumerName;
        this.consumerProps = consumerProps;
    }

    protected abstract void shutdown();
    protected abstract void run(String consumerName, Properties consumerProps);

    @Override
    public final void run() {
        run(consumerName, consumerProps);
    }
}

ConsumerA class:

public class ConsumerA extends Consumer {
    private final AtomicBoolean closed = new AtomicBoolean(false);
    private KafkaConsumer<byte[], byte[]> consumer;

    public ConsumerA(String consumerName, Properties consumerProps) {
        super(consumerName, consumerProps);
    }

    @Override
    public void shutdown() {
        closed.set(true);
        consumer.wakeup();
    }

    @Override
    protected void run(String consumerName, Properties consumerProps) {
        consumer = new KafkaConsumer<>(consumerProps);
        consumer.subscribe(getTopicsBasisOnConsumerName());

        Map<String, Object> config = new HashMap<>();
        config.put(Config.URLS, TEST_URL);
        GenericRecordDomainDataDecoder decoder = new GenericRecordDomainDataDecoder(config);

        try {
            while (!closed.get()) {
                ConsumerRecords<byte[], byte[]> records = consumer.poll(Long.MAX_VALUE);
                for (ConsumerRecord<byte[], byte[]> record : records) {
                    GenericRecord payload = decoder.decode(record.value());
                    // extract data from payload
                    System.out.println("topic = %s, partition = %s, offset = %d, customer = %s, country = %s\n",
                                      record.topic(), record.partition(), record.offset(), record.key(), record.value());
                }
                consumer.commitAsync();
            }
        } catch (WakeupException ex) {
            // Ignore exception if closing
            System.out.println("error= ", ex);
            if (!closed.get()) throw e;
        } catch (Exception ex) {
            System.out.println("error= ", ex);
        } finally {
            try {
                consumer.commitSync();
            } finally {
                consumer.close();
            }
        }
    }
}

ConsumerA B class:

// similar to `ConsumerA` but with specific details of B

ConsumerHandler class:

public final class ConsumerHandler {
  private final ExecutorService executorServiceConsumer;
  private final Consumer consumer;
  private final List<Consumer> consumers = new ArrayList<>();

  public ConsumerHandler(Consumer consumer, int poolSize) {
    this.executorServiceConsumer = Executors.newFixedThreadPool(poolSize);
    this.consumer = consumer;
    for (int i = 0; i < poolSize; i++) {
      this.consumers.add(consumer);
      executorServiceConsumer.submit(consumer);
    }
 }
  public void shutdown() {
    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        for (Consumer consumer : consumers) {
          consumer.shutdown();
        }
        executorServiceConsumer.shutdown();
        try {
          executorServiceConsumer.awaitTermination(1000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ex) {
          Thread.currentThread().interrupt();
        }
      }
    });
  }
}

Below is my main class in one of my project where if I start my server, calls will come first automatically and from this place I start my all kafka consumers where I execute my ConsumerA and ConsumerB. And as soon as shutdown is called, I release all the resources by calling shutdown on all my Kafka consumers.

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Singleton;

@Singleton
@DependencyInjectionInitializer
public class Initializer {
  private ConsumerHandler consumerHandlerA;
  private ConsumerHandler consumerHandlerB;

  @PostConstruct
  public void init() {
    consumerHandlerA = new ConsumerHandler (new ConsumerA("consumerA", getConsumerPropsA()), 3);
    consumerHandlerB = new ConsumerHandler (new ConsumerB("consumerB", getConsumerPropsB()), 3);
  }

  @PreDestroy
  public void shutdown() {
    consumerHandlerA.shutdown();
    consumerHandlerB.shutdown();
  }
}

Is this the right design for this kind of problem where I want to run multiple kafka consumers on the same box? Let me know if there is any better and efficient way to solve this problem. In general I will be running three or four Kafka consumers max on the same box and each consumer can have their own consumer group if needed.

Here is the Javadoc for KafkaConsumer which I am using in both my consumer. And basis on this article I have created my consumer, it is just that I have used abstract class to extend it. Search for "Putting it all Together" in that link.

In the docs it is mentioned that Consumers are not Thread-Safe but it looks like my code is reusing the same consumer instance for each thread in the pool.

public ConsumerHandler(Consumer consumer, int poolSize) {
    this.executorServiceConsumer = Executors.newFixedThreadPool(poolSize);
    this.consumer = consumer;
    for (int i = 0; i < poolSize; i++) {
      this.consumers.add(consumer);
      executorServiceConsumer.submit(consumer);
    }
 }

What is the best way to solve this thread safety issue and still achieve the same features?


来源: stackexchange

问:
Max Date and Concatenate on separate column
Could someone help me with a solution to this problem? I need to do two things with my data.

I need to get the max date

I need to concatenate the values in the Proj_Use column

My data looks like this:

```<code>Report_Date  |  Program  |  Project  |  Proj_Mgr  |  Region  |  Proj_Use

6/5/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU1

6/4/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU2

6/3/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU3

6/5/2017  |  PG2  |  Prj02  |  Monica  |  West  | PU4

6/4/2017  |  PG2  |  Prj02  |  Monica  |  West  |  PU5

6/3/2017  |  PG2  |  Prj02  |  Monica  |  West  |  PU6
</code>```

My results should look like this:

```<code>Report_Date Program Project Proj_MgrRegion  Proj_Use

6/5/2017 | PG1 | Prj01 | Anne | East | PU1, PU2, PU3

6/5/2017 | PG2 | Prj02 | Monica | West | PU4, PU5, PU6
</code>```

I can get the max date by using the following code:

```<code>    SELECT   t.[Report_Date]
        ,t.[Program]
        ,t.[Project]
        ,t.[Proj_Mgr]
        ,t.[Region]
        ,t.[Proj_Use]
    FROM project_table t
    JOIN
    (SELECT MAX([Report_Date]) as [Report Date], [Program]
        FROM project_table
        GROUP BY [Program]) max
        ON t.[Report_Date] = max.[Report Date]
        AND t.[Program] = max.[Program]
</code>```

I searched on the forum for a way to concatenate and found the group_concat method for sql server. I've tried to add that to the code and came up with this:

```<code>    SELECT   t.[Report_Date]
        ,t.[Program]
        ,t.[Project]
        ,t.[Proj_Mgr]
        ,t.[Region]
        ,STUFF ((
            SELECT ',' + t.[Proj_Use]
                FROM project_table
                FOR XML PATH ('')), 1,1, '') AS [Project Use]
    FROM project_table t
    JOIN
    (SELECT MAX([Report_Date]) as [Report Date], [Program]
        FROM project_table
        GROUP BY [Program]) max
        ON t.[Report_Date] = max.[Report Date]
        AND t.[Program] = max.[Program]
</code>```

When I tried to run this using my real data the query ran for over 20 minutes without producing results.

Can anyone let me know if I've structured this query correctly or if there is a better way to get the results I'm looking for?

Thanks for your help.

答:
I am expecting something like this:

```<code>SELECT pt.*,
       STUFF((SELECT ',' + pt2.[Proj_Use]
              FROM project_table pt2
              WHERE pt2.Program = pt.Program
              ORDER BY pt2.[Report Date]
              FOR XML PATH ('')
             ), 1, 1, '') AS [Projects Used]
FROM (SELECT pt.*,
             ROW_NUMBER() OVER (PARTITION BY Program ORDER BY [Report Date] DESC) as seqnum
      FROM project_table t
     ) pt
WHERE seqnum = 1;
</code>```

At a minimum, your subquery to do the concatenation doesn't have a correlation clause to the outer query (so the intermediate string is likely to be very long).  There are probably other issues as well.

ContentLicense: CC BY-SA 3.0

Body:
Could someone help me with a solution to this problem? I need to do two things with my data.

I need to get the max date

I need to concatenate the values in the Proj_Use column

My data looks like this:

Report_Date  |  Program  |  Project  |  Proj_Mgr  |  Region  |  Proj_Use

6/5/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU1

6/4/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU2

6/3/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU3

6/5/2017  |  PG2  |  Prj02  |  Monica  |  West  | PU4

6/4/2017  |  PG2  |  Prj02  |  Monica  |  West  |  PU5

6/3/2017  |  PG2  |  Prj02  |  Monica  |  West  |  PU6

My results should look like this:

Report_Date Program Project Proj_MgrRegion  Proj_Use

6/5/2017 | PG1 | Prj01 | Anne | East | PU1, PU2, PU3

6/5/2017 | PG2 | Prj02 | Monica | West | PU4, PU5, PU6

I can get the max date by using the following code:

    SELECT   t.[Report_Date]
        ,t.[Program]
        ,t.[Project]
        ,t.[Proj_Mgr]
        ,t.[Region]
        ,t.[Proj_Use]
    FROM project_table t
    JOIN
    (SELECT MAX([Report_Date]) as [Report Date], [Program]
        FROM project_table
        GROUP BY [Program]) max
        ON t.[Report_Date] = max.[Report Date]
        AND t.[Program] = max.[Program]

I searched on the forum for a way to concatenate and found the group_concat method for sql server. I've tried to add that to the code and came up with this:

    SELECT   t.[Report_Date]
        ,t.[Program]
        ,t.[Project]
        ,t.[Proj_Mgr]
        ,t.[Region]
        ,STUFF ((
            SELECT ',' + t.[Proj_Use]
                FROM project_table
                FOR XML PATH ('')), 1,1, '') AS [Project Use]
    FROM project_table t
    JOIN
    (SELECT MAX([Report_Date]) as [Report Date], [Program]
        FROM project_table
        GROUP BY [Program]) max
        ON t.[Report_Date] = max.[Report Date]
        AND t.[Program] = max.[Program]

When I tried to run this using my real data the query ran for over 20 minutes without producing results.

Can anyone let me know if I've structured this query correctly or if there is a better way to get the results I'm looking for?

Thanks for your help.

Body:
I am expecting something like this:

SELECT pt.*,
       STUFF((SELECT ',' + pt2.[Proj_Use]
              FROM project_table pt2
              WHERE pt2.Program = pt.Program
              ORDER BY pt2.[Report Date]
              FOR XML PATH ('')
             ), 1, 1, '') AS [Projects Used]
FROM (SELECT pt.*,
             ROW_NUMBER() OVER (PARTITION BY Program ORDER BY [Report Date] DESC) as seqnum
      FROM project_table t
     ) pt
WHERE seqnum = 1;

At a minimum, your subquery to do the concatenation doesn't have a correlation clause to the outer query (so the intermediate string is likely to be very long).  There are probably other issues as well.


问:
codeigniter, set_flashdata('abc', 'hi);
I am using codeigniter on my project.

when page refresh,

in controller page,

```<code>$this->session->set_flashdata('abc', 'hi);
</code>```

in view page,
echo

```<code>$this->session->flashdata('abc');
</code>```

works fine.

but, when ajax call, not page refresh,
controller,

```<code> $this->session->set_flashdata('abc', 'hi);
</code>```

view,

```<code>echo $this->session->flashdata('abc');
</code>```

doesn't work.....

echoing nothing~ -_-;
ajax doesn't have mechanism to catch session flash data?

any idea?

答:
You probably should not use flashdata

You just need to echo. The echo should come from your controller method. and your javascript accepting the response should insert it into an element in your page.

ContentLicense: CC BY-SA 2.5

Title: codeigniter, set_flashdata('abc', 'hi);

Body:
I am using codeigniter on my project.

when page refresh,

in controller page,

$this->session->set_flashdata('abc', 'hi);

in view page,
echo

$this->session->flashdata('abc');

works fine.

but, when ajax call, not page refresh,
controller,

 $this->session->set_flashdata('abc', 'hi);

view,

echo $this->session->flashdata('abc');

doesn't work.....

echoing nothing~ -_-;
ajax doesn't have mechanism to catch session flash data?

any idea?


问:
ggplot2 summarySE plotting small data set - missing points using geom_point
I am working with a small data set as follows:

```<code>Thermo  Chl     Month
1.53    1.53    May
3.63    1.53    June
3.83    2.33    July
3.84    2.33    July
5.08           August
5.50           August
5.04    1.55    September
5.90    1.70    September
3.75           September
3.63    1.70    September
5.90    1.70    September
9.00    2.50    October
11.03   1.03    October
11.00   1.00    October
9.00    2.37    October
9.00    2.03    November
14.96   2.04    November
7.13    15.95   January
13.03   9.00    February
9.00    1.03    April
9.00    2.50    May
8.97    2.50    May
4.88    2.50    June
4.88    2.50    June
3.63    2.50    July
4.88    1.03    July
3.83    2.33    August
5.53    2.30    August
7.20    2.37    September
7.17    2.33    September
7.17    2.33    September
7.20    2.37    September
7.17    2.33    September
7.17    2.33    September
7.17    1.08    September
7.17    2.33    September
7.17    1.03    September
7.17    2.33    September
7.17    2.33    September
7.17    1.03    September
9.00    1.03    October
9.00    2.37    October
8.97    5.50    October
11.00   5.50    October
13.00   10.00   November
13.00   10.00   December
11.00   15.17   January
11.00   1.03    February
9.00    2.33    March
5.50    1.03    April
3.83    2.37    May
3.87    2.33    June
5.50    3.83    June
3.83    2.33    July
5.50    3.37    July
7.17    2.37    July
7.17    2.30    August
7.17    2.33    September
9.00    2.33    September
9.03    3.83    September
9.00    3.83    September
9.00    2.33    September
9.00    2.37    September
7.17    2.33    September
7.17    4.00    September
7.17    3.50    September
7.17    2.33    September
7.20    2.33    September
7.17    3.83    September
7.17    2.33    October
7.17    2.30    October
7.17    2.33    October
7.17    2.33    October
8.68    2.33    October
7.20    2.33    October
9.00    3.87    October
9.00    5.50    October
11.00   5.50    November
13.00   1.03    November
11.00   3.83    November
9.00    3.83    December
6.03    13.00   March
8.00    15.67   March
5.97    0.70    April
5.57    2.40    May
7.25    1.03    May
5.50    2.33    June
5.50    3.00    July
4.70    2.00    July
5.50    2.33    August
5.50    2.33    August
9.00    2.33    September
9.00    1.03    September
9.00    2.33    September
7.13    2.37    September
7.17    2.33    September
7.17    2.33    September
7.17    2.33    September
7.17    2.43    September
7.20    1.07    September
7.17    2.00    September
7.17    2.33    September
7.17    2.00    September
7.17    1.03    September
7.13    2.33    October
7.17    2.33    October
7.17    2.30    October
7.17    2.33    October
9.00    3.83    October
13.00   2.33    October
14.97   3.83    October
14.93   2.03    October
</code>```

When I plot with the summarySE and ggplot2 functions as follows:

```<code>phyto.maxchl$Month <- factor(phyto.maxchl$Month)

phyto.se <- summarySE(phyto.maxchl, measurevar="Thermocline", groupvars=c("Month"))

ggplot(phyto.se, aes(x=phyto.se$Month, y=phyto.se$Thermocline)) +
  geom_point(position=position_dodge(),stat="identity") +
  geom_errorbar(aes(ymin=phyto.se$Thermocline-se, ymax=phyto.se$Thermocline+se),
                width=.2,
                position=position_dodge(.9))+
  ylab("Thermocline Depth (m)")+
  xlab("Month")+
  theme_bw()+
  theme(axis.text.x=element_text(angle=70,size=15,vjust=0.60))+
  labs(title = "Thermocline Depth for 2013 - 2016")+
  scale_y_continuous(trans='reverse')+
  theme(legend.title=element_blank())
</code>```

I am missing points for January - April. Is this because I don't have enough data points to satisfy the requirements needed to calculate standard error?

Body:
I am working with a small data set as follows:

Thermo  Chl     Month
1.53    1.53    May
3.63    1.53    June
3.83    2.33    July
3.84    2.33    July
5.08           August
5.50           August
5.04    1.55    September
5.90    1.70    September
3.75           September
3.63    1.70    September
5.90    1.70    September
9.00    2.50    October
11.03   1.03    October
11.00   1.00    October
9.00    2.37    October
9.00    2.03    November
14.96   2.04    November
7.13    15.95   January
13.03   9.00    February
9.00    1.03    April
9.00    2.50    May
8.97    2.50    May
4.88    2.50    June
4.88    2.50    June
3.63    2.50    July
4.88    1.03    July
3.83    2.33    August
5.53    2.30    August
7.20    2.37    September
7.17    2.33    September
7.17    2.33    September
7.20    2.37    September
7.17    2.33    September
7.17    2.33    September
7.17    1.08    September
7.17    2.33    September
7.17    1.03    September
7.17    2.33    September
7.17    2.33    September
7.17    1.03    September
9.00    1.03    October
9.00    2.37    October
8.97    5.50    October
11.00   5.50    October
13.00   10.00   November
13.00   10.00   December
11.00   15.17   January
11.00   1.03    February
9.00    2.33    March
5.50    1.03    April
3.83    2.37    May
3.87    2.33    June
5.50    3.83    June
3.83    2.33    July
5.50    3.37    July
7.17    2.37    July
7.17    2.30    August
7.17    2.33    September
9.00    2.33    September
9.03    3.83    September
9.00    3.83    September
9.00    2.33    September
9.00    2.37    September
7.17    2.33    September
7.17    4.00    September
7.17    3.50    September
7.17    2.33    September
7.20    2.33    September
7.17    3.83    September
7.17    2.33    October
7.17    2.30    October
7.17    2.33    October
7.17    2.33    October
8.68    2.33    October
7.20    2.33    October
9.00    3.87    October
9.00    5.50    October
11.00   5.50    November
13.00   1.03    November
11.00   3.83    November
9.00    3.83    December
6.03    13.00   March
8.00    15.67   March
5.97    0.70    April
5.57    2.40    May
7.25    1.03    May
5.50    2.33    June
5.50    3.00    July
4.70    2.00    July
5.50    2.33    August
5.50    2.33    August
9.00    2.33    September
9.00    1.03    September
9.00    2.33    September
7.13    2.37    September
7.17    2.33    September
7.17    2.33    September
7.17    2.33    September
7.17    2.43    September
7.20    1.07    September
7.17    2.00    September
7.17    2.33    September
7.17    2.00    September
7.17    1.03    September
7.13    2.33    October
7.17    2.33    October
7.17    2.30    October
7.17    2.33    October
9.00    3.83    October
13.00   2.33    October
14.97   3.83    October
14.93   2.03    October

When I plot with the summarySE and ggplot2 functions as follows:

phyto.maxchl$Month <- factor(phyto.maxchl$Month)

phyto.se <- summarySE(phyto.maxchl, measurevar="Thermocline", groupvars=c("Month"))

ggplot(phyto.se, aes(x=phyto.se$Month, y=phyto.se$Thermocline)) +
  geom_point(position=position_dodge(),stat="identity") +
  geom_errorbar(aes(ymin=phyto.se$Thermocline-se, ymax=phyto.se$Thermocline+se),
                width=.2,
                position=position_dodge(.9))+
  ylab("Thermocline Depth (m)")+
  xlab("Month")+
  theme_bw()+
  theme(axis.text.x=element_text(angle=70,size=15,vjust=0.60))+
  labs(title = "Thermocline Depth for 2013 - 2016")+
  scale_y_continuous(trans='reverse')+
  theme(legend.title=element_blank())

I am missing points for January - April. Is this because I don't have enough data points to satisfy the requirements needed to calculate standard error?

Body:
summarySE() returns NA if it encounters any NA inside a group, unless na.rm = TRUE is supplied as argument.

Rmisc::summarySE(df, "Chl", groupvars="Month")
#>        Month  N       Chl        sd        se         ci
#> 1      April  3  0.920000 0.1905256 0.1100000  0.4732918
#> 2     August  7        NA        NA        NA         NA
#> 3   December  2  6.915000 4.3628488 3.0850000 39.1986416
#> 4   February  2  5.015000 5.6356410 3.9850000 50.6342259
#> 5    January  2 15.560000 0.5515433 0.3900000  4.9554198
#> 6       July  9  2.362222 0.6471626 0.2157209  0.4974532
#> 7       June  6  2.503333 0.7439803 0.3037287  0.7807594
#> 8      March  3 10.333333 7.0584866 4.0752191 17.5342528
#> 9        May  6  2.055000 0.6229848 0.2543325  0.6537824
#> 10  November  6  4.071667 3.3103318 1.3514373  3.4739802
#> 11   October 24  2.747083 1.2873312 0.2627754  0.5435923
#> 12 September 42        NA        NA        NA         NA

As opposed to:

Rmisc::summarySE(df, "Chl", groupvars="Month", na.rm = T)
#>        Month  N       Chl         sd          se          ci
#> 1      April  3  0.920000 0.19052559 0.110000000  0.47329180
#> 2     August  5  2.318000 0.01643168 0.007348469  0.02040262
#> 3   December  2  6.915000 4.36284884 3.085000000 39.19864161
#> 4   February  2  5.015000 5.63564105 3.985000000 50.63422587
#> 5    January  2 15.560000 0.55154329 0.390000000  4.95541985
#> 6       July  9  2.362222 0.64716261 0.215720870  0.49745322
#> 7       June  6  2.503333 0.74398029 0.303728680  0.78075943
#> 8      March  3 10.333333 7.05848662 4.075219149 17.53425280
#> 9        May  6  2.055000 0.62298475 0.254332460  0.65378240
#> 10  November  6  4.071667 3.31033181 1.351437301  3.47398018
#> 11   October 24  2.747083 1.28733118 0.262775376  0.54359228
#> 12 September 41  2.246098 0.75104220 0.117293086  0.23705817

That's why you get two missing values. The fact that the NAs are relative to January and April as opposed to the correct August and September is probably because of a discrepancy between the Month factor values and its labels.

levels(df$Month) <- month.name

or

df$Month <- factor(df$Month, levels = month.name)

should fix it.

Following is your exact code, whithout NAs and some fixes at the gggplot call. In particular you should never use df$Var inside a aes(), the name of the variable is enough.

df2 <- Rmisc::summarySE(df, "Chl", groupvars="Month", na.rm = T)

library(ggplot2)

ggplot(df2, aes(x=factor(Month, levels = month.name), y=Chl)) +
  geom_point(position=position_dodge(),stat="identity") +
  geom_errorbar(aes(ymin=Chl-se, ymax=Chl+se),
                width=.2,
                position=position_dodge(.9))+
  ylab("Thermocline Depth (m)")+
  xlab("Month")+
  theme_bw()+
  theme(axis.text.x=element_text(angle=70,size=15,vjust=0.60))+
  labs(title = "Thermocline Depth for 2013 - 2016")+
  scale_y_continuous(trans='reverse')+
  theme(legend.title=element_blank())
#> Warning: Width not defined. Set with `position_dodge(width = ?)`

Just for completeness, I'll show you how is not even necessary to calculate the summary beforehand, and we can leave the calculation to ggplot:

library(ggplot2)

ggplot(df, aes(factor(Month, levels = month.name), Chl)) +
  stat_summary(geom = 'point', position = position_dodge(.9)) +
  stat_summary(geom = 'errorbar', width=.2,
               position=position_dodge(.9))+
  ylab("Thermocline Depth (m)")+
  xlab("Month")+
  theme_bw()+
  theme(axis.text.x=element_text(angle=70,size=15,vjust=0.60))+
  labs(title = "Thermocline Depth for 2013 - 2016")+
  scale_y_continuous(trans='reverse')+
  theme(legend.title=element_blank())

Data:

df <- read.table(text = 'Thermo  Chl     Month
1.53    1.53    May
             3.63    1.53    June
             3.83    2.33    July
             3.84    2.33    July
             5.08     NA      August
             5.50     NA      August
             5.04    1.55    September
             5.90    1.70    September
             3.75     NA      September
             3.63    1.70    September
             5.90    1.70    September
             9.00    2.50    October
             11.03   1.03    October
             11.00   1.00    October
             9.00    2.37    October
             9.00    2.03    November
             14.96   2.04    November
             7.13    15.95   January
             13.03   9.00    February
             9.00    1.03    April
             9.00    2.50    May
             8.97    2.50    May
             4.88    2.50    June
             4.88    2.50    June
             3.63    2.50    July
             4.88    1.03    July
             3.83    2.33    August
             5.53    2.30    August
             7.20    2.37    September
             7.17    2.33    September
             7.17    2.33    September
             7.20    2.37    September
             7.17    2.33    September
             7.17    2.33    September
             7.17    1.08    September
             7.17    2.33    September
             7.17    1.03    September
             7.17    2.33    September
             7.17    2.33    September
             7.17    1.03    September
             9.00    1.03    October
             9.00    2.37    October
             8.97    5.50    October
             11.00   5.50    October
             13.00   10.00   November
             13.00   10.00   December
             11.00   15.17   January
             11.00   1.03    February
             9.00    2.33    March
             5.50    1.03    April
             3.83    2.37    May
             3.87    2.33    June
             5.50    3.83    June
             3.83    2.33    July
             5.50    3.37    July
             7.17    2.37    July
             7.17    2.30    August
             7.17    2.33    September
             9.00    2.33    September
             9.03    3.83    September
             9.00    3.83    September
             9.00    2.33    September
             9.00    2.37    September
             7.17    2.33    September
             7.17    4.00    September
             7.17    3.50    September
             7.17    2.33    September
             7.20    2.33    September
             7.17    3.83    September
             7.17    2.33    October
             7.17    2.30    October
             7.17    2.33    October
             7.17    2.33    October
             8.68    2.33    October
             7.20    2.33    October
             9.00    3.87    October
             9.00    5.50    October
             11.00   5.50    November
             13.00   1.03    November
             11.00   3.83    November
             9.00    3.83    December
             6.03    13.00   March
             8.00    15.67   March
             5.97    0.70    April
             5.57    2.40    May
             7.25    1.03    May
             5.50    2.33    June
             5.50    3.00    July
             4.70    2.00    July
             5.50    2.33    August
             5.50    2.33    August
             9.00    2.33    September
             9.00    1.03    September
             9.00    2.33    September
             7.13    2.37    September
             7.17    2.33    September
             7.17    2.33    September
             7.17    2.33    September
             7.17    2.43    September
             7.20    1.07    September
             7.17    2.00    September
             7.17    2.33    September
             7.17    2.00    September
             7.17    1.03    September
             7.13    2.33    October
             7.17    2.33    October
             7.17    2.30    October
             7.17    2.33    October
             9.00    3.83    October
             13.00   2.33    October
             14.97   3.83    October
             14.93   2.03    October', h = TRUE)


问:
Updating Notification Continuously
Hello There I am a newbie for Android Development, working to learn it to my own!
I just want to update my notification in a Java Thread in my application (I am just learning and curious about how can I do it).

I have an activity, a simple thread to increment an Integer value. Then, I just want to show it in my Notification whenever the Integer value increments!
My Code is as:

```<code>    public class MainActivity extends Activity{
        private final String LOG_KEY = this.getClass().getSimpleName();
        private int c = 0;
        private boolean flag = true;
        private NotificationCompat.Builder builder;
        private NotificationManager notificationManager;

        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            builder = new NotificationCompat.Builder(MainActivity.this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setAutoCancel(false);

            builder.setOngoing(true);

            notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

            Thread t = new Thread(new MyThread());
            t.start();
        }//OnCreate ends

        @Override
        protected void onStop() {
            super.onStop();
            flag = false;
        }//stop ends

        @Override
        protected void onDestroy() {
            super.onDestroy();
            flag = false;
        }//destroy ends

        private class MyThread implements Runnable {
            @Override
            public void run() {
                while (flag) {
                    c+=1;
                    showNotification(Integer.toString(c) + " Counts");
                }//while ends
            }//run ends

            private void showNotification(String msg) {
                try {
                    //set the notification
                    builder.setContentText(msg);
                    notificationManager.notify(0, builder.build());
                } catch (Exception exp) {
                    Log.e("xmn", exp.toString());
                }//try catch ends
            }//showNotification ends

        }//private class ends
    }//MainActivity class ends here
</code>```

As from my code, the notification appears and updates the value! But the problem is that it freezes the device and application at a sudden!

I just want help for what I am doing wrong as I am a newbie and learning it to my own. Any help and idea will be highly appreciated!

Thanks

答:
You shouldn't be creating a notification and then continuously updating it as fast as you can from a thread.  It's really not designed for that.

The closest thing I can think of that would meet your use case is using a notification to display progress.  See this link:

Displaying Progress in a Notification

You might want to put some kind a rate limiter in your thread, unless you want your count to reach very high numbers very quickly.  Perhaps make the thread sleep for a second between updates.

Title: Updating Notification Continuously

Body:
Hello There I am a newbie for Android Development, working to learn it to my own!
I just want to update my notification in a Java Thread in my application (I am just learning and curious about how can I do it).

I have an activity, a simple thread to increment an Integer value. Then, I just want to show it in my Notification whenever the Integer value increments!
My Code is as:

    public class MainActivity extends Activity{
        private final String LOG_KEY = this.getClass().getSimpleName();
        private int c = 0;
        private boolean flag = true;
        private NotificationCompat.Builder builder;
        private NotificationManager notificationManager;

        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            builder = new NotificationCompat.Builder(MainActivity.this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setAutoCancel(false);

            builder.setOngoing(true);

            notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

            Thread t = new Thread(new MyThread());
            t.start();
        }//OnCreate ends

        @Override
        protected void onStop() {
            super.onStop();
            flag = false;
        }//stop ends

        @Override
        protected void onDestroy() {
            super.onDestroy();
            flag = false;
        }//destroy ends

        private class MyThread implements Runnable {
            @Override
            public void run() {
                while (flag) {
                    c+=1;
                    showNotification(Integer.toString(c) + " Counts");
                }//while ends
            }//run ends

            private void showNotification(String msg) {
                try {
                    //set the notification
                    builder.setContentText(msg);
                    notificationManager.notify(0, builder.build());
                } catch (Exception exp) {
                    Log.e("xmn", exp.toString());
                }//try catch ends
            }//showNotification ends

        }//private class ends
    }//MainActivity class ends here

As from my code, the notification appears and updates the value! But the problem is that it freezes the device and application at a sudden!

I just want help for what I am doing wrong as I am a newbie and learning it to my own. Any help and idea will be highly appreciated!

Thanks


问:
How to create a project for Xamarin.Forms with .NET Standard (VS2017)
I tried both the .NET Standard and the Class Library (Xamarin.Forms) projects and didn't get any project for iOS and Android. I then tried Cross Platform App (Xamarin) and had to manually change the target to .NET Standard, which forced me to first remove the original NuGet packages. And now the project is littered with red squiggly lines.

So how do I create a Xamarin.Forms project targeting .NET Standard, for iOS and Android (and UWP)?

答:
I don't think there is a template for that at the moment.

If you want to put all the platform independent code into the NetStandard project create one using Cross Platform App (Xamarin) - then select Xamarin.Forms and Portable Class Library (PCL)

Then delete the PCL and (from the solution and the folder) and add a netstandard library in it's place (and reference it in the platform projects)

I'm always using the 'Shared Project' approach and adding everything non Xamarin.Forms (like ViewModels) in a separate PCL / netstandard library.


问:
jQuery code works for last iteration of php loop generated button
I'm creating a forum where people can vote on posts. The vote is called 'consents'.

```<code><?php
$postshow = "SELECT * FROM ideas_1 ORDER BY consents DESC LIMIT 10";
$done = mysqli_query($conn, $postshow);
//$show = mysqli_fetch_assoc($done);

while ($show = mysqli_fetch_assoc($done)) {
    echo"<div class = 'comments'>";
    echo"<br>";
    echo"<br>";
    echo $show['post'];
    echo"<br>";
    echo"<p class = 'counter'>";
    echo $show['consents'];
    echo"</p>";
    echo"<br>";
    $postid = $show['postid'];
    $userid = $show['userid'];
    $consents = $show['consents'];
      $asd = "SELECT * FROM consents WHERE dept = 'ideas' and task = '1' and postid = '$postid' and voterid = '$id'";
      $doit = mysqli_query($conn, $asd);
      $exist = mysqli_num_rows($doit);
      if ($exist == 0){
    echo"<button id = \"consents$postid\">Consent This</button>";
      }
    echo"</div>";

}
?>
<script>
    var posterid = "<?php echo $userid;?>"
    var postid = "<?php echo $postid;?>"
    var voterid = "<?php echo $id;?>"
    var consents = "<?php echo $consents;?>"

$(document).ready(function () {
   $('#consents' + postid).click(function(){
    $(this).hide();
    $.post ("consentsideas.php", {
        dept: "ideas",
        task: "1",
        postid: postid,
        voterid: voterid,
        consents: consents,
        posterid: posterid
    });
    $(".counter").html(++consents);
   });
   $(".answer").mousedown(function(){
            $(".post_answer").show();
            });
   $(document).click(function(event) {
    if(!$(event.target).closest('.post_answer').length) {
        if($('.post_answer').is(":visible")) {
            $('.post_answer').hide();
        }
    }
});
  $(".post_answer").click(function(){
    var answer = $(".answer").val();
    $.post ("answerideas.php", {
        answer: answer,
        id: voterid
            });
    $(".answer").val('');
    });
}
  );
</script>
</code>```

But any button looped by the while loop, when clicked, only updates the CONSENTS counter for the last iteration of the while loop.
As you can see, I've made the button's id unique(I hope I did that correctly). So could anyone tell me where's the problem?

答:
The problem is not in your PHP; it's in your JavaScript. You're setting postid once, using the very last $postid that is set in the last iteration of the while loop.

One way to solve this problem is by binding the click event not to the id (like #consents123) but rather to a class that is set on each of the "Consent This" buttons (like .consents).

Then you'll need to cache the other data you need (like postid) somewhere, for example as a data attribute on the button, or in the element id, and then retrieve it when you need it in the click handler.

PHP

```<code><button id = \"consents$postid\" class="consents" data-postid="$postid">Consent This</button>
</code>```

JS

```<code>$('.consents').click(function() {
    ...
    var postid = $(this).data('postid');
    ...
});
</code>```

Body:
I'm creating a forum where people can vote on posts. The vote is called 'consents'.

<?php
$postshow = "SELECT * FROM ideas_1 ORDER BY consents DESC LIMIT 10";
$done = mysqli_query($conn, $postshow);
//$show = mysqli_fetch_assoc($done);

while ($show = mysqli_fetch_assoc($done)) {
    echo"
";
    echo"
";
    echo"
";
    echo $show['post'];
    echo"
";
    echo"
";
    echo $show['consents'];
    echo"
";
    echo"
";
    $postid = $show['postid'];
    $userid = $show['userid'];
    $consents = $show['consents'];
      $asd = "SELECT * FROM consents WHERE dept = 'ideas' and task = '1' and postid = '$postid' and voterid = '$id'";
      $doit = mysqli_query($conn, $asd);
      $exist = mysqli_num_rows($doit);
      if ($exist == 0){
    echo"<button id = \"consents$postid\">Consent This</button>";
      }
    echo"
";

}
?>

But any button looped by the while loop, when clicked, only updates the CONSENTS counter for the last iteration of the while loop.
As you can see, I've made the button's id unique(I hope I did that correctly). So could anyone tell me where's the problem?

Body:
The problem is not in your PHP; it's in your JavaScript. You're setting postid once, using the very last $postid that is set in the last iteration of the while loop.

One way to solve this problem is by binding the click event not to the id (like #consents123) but rather to a class that is set on each of the "Consent This" buttons (like .consents).

Then you'll need to cache the other data you need (like postid) somewhere, for example as a data attribute on the button, or in the element id, and then retrieve it when you need it in the click handler.

PHP

<button id = \"consents$postid\" class="consents" data-postid="$postid">Consent This</button>

JS

$('.consents').click(function() {
    ...
    var postid = $(this).data('postid');
    ...
});


问:
pyspark toPandas Error?
I have a messy and very big data set consisting of Chinese characters, numbers, strings, date.etc. After I did some cleaning using pyspark and want to turn it into a pandas, it raises this error:

    IOPub data rate exceeded.
    The notebook server will temporarily stop sending output
    to the client in order to avoid crashing it.
    To change this limit, set the config variable
    --NotebookApp.iopub_data_rate_limit.
17/06/06 18:48:54 WARN TaskSetManager: Lost task 8.0 in stage 13.0 (TID 393, localhost): TaskKilled (killed intentionally)

And above the error, it outputs some of my original data.It's very long. So I just post part of it.

I have checked my cleaned data. All column type are int, double. Why does it still output my old data?

答:
Try launching jupyter notebook increasing 'iopub_data_rate_limit' as:

jupyter notebook --NotebookApp.iopub_data_rate_limit=10000000000

Source: https://github.com/jupyter/notebook/issues/2287

Title: pyspark toPandas Error?


问:
Dynamic script to change specific variables in Angular 2 or higher
Currently, you can use the user's final script. This script can modify some previously available variables.
I created a small example in pure javascript I would like to do the same in angular 2+ https://jsfiddle.net/hxs3d0hu/2/

Thank you in advance for your attention

HTML Code

```<code>    <div class="container">
      <form>
        <div class="form-group">
          <label for="descricao">Nome</label>
          <input type="text" id="descricao" name="descricao" class="form-control" />
        </div>
        <div class="form-group">
          <label for="valor">Valor</label>
          <input type="text" id="valor" name="valor" value="10" class="form-control" />
        </div>
        <div class="form-group">
          <label for="quantidade">Quantidade</label>
          <input type="text" id="quantidade" name="quantidade" value="20" class="form-control" />
        </div>
        <div class="form-group">
          <label for="total">Total</label>
          <input type="text" id="total" name="total" class="form-control" />
        </div>
        <div class="form-group">
          <label for="codex">Code</label>
          <textarea id="codex" name="codex" rows="10" cols="100" class="form-control">
          if (qtde < 15) {
              vlr = 10;
          }
          else
          {
              vlr = 9;
          }
          tot = vlr * qtde;
          </textarea>
        </div>

        <button type="button" id="calcular" name="calcular" onclick="calcularx();" class="btn btn-primary">Calcular</button>
      </form>
    </div>
</code>```

JavaScript Code

```<code>    var vlr = 0;
    var qtde = 0;
    var tot = 0;

        function calcularx(){
            var valor = document.getElementById("valor");
            var quantidade = document.getElementById("quantidade");
            var total = document.getElementById("total");

            vlr = valor.value;
            qtde = quantidade.value;
            tot = 0;

            tot = qtde * vlr;
            total.value = tot;

            var codex = document.getElementById("codex").value;
            var cst = document.getElementById("customcodescript");
            var corpo = "function custom(){ {0} }".replace("{0}", codex);
            //cst.innerHTML = corpo;
            load_js(cst, corpo);

            custom();

            valor.value = vlr;
            quantidade.value = qtde;
            total.value = tot;

            console.log(tot);
        }

       function load_js(cst, corpo)
       {
          if(cst != null){
            cst.remove();
          }
          var head= document.getElementsByTagName('head')[0];
          var script= document.createElement('script');
          script.id = "customcodescript";
          script.type= 'text/javascript';
          script.innerHTML = corpo;
          //script.src= 'source_file.js';
          head.appendChild(script);
       }

    Element.prototype.remove = function() {
        this.parentElement.removeChild(this);
    }
    NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
        for(var i = this.length - 1; i >= 0; i--) {
            if(this[i] && this[i].parentElement) {
                this[i].parentElement.removeChild(this[i]);
            }
        }
    }
</code>```

答:
Here's a plnkr that would achieve what you're looking for.

http://embed.plnkr.co/w2FVfKlWP72pzXIsfsCU/

You create a function with eval, then call it using the component context. All the function and variable in your component will be available to your textbox code :

The template
    Value:

```<code><label>Code:</label>
<textarea [(ngModel)]="code"></textarea> <br>

<button (click)="executeCode(code)">Do it.</button>
</code>```

The component :

```<code>export class HelloWorld {

  value = 100;
  code = 'this.value = this.value * 100';

  executeCode(code){
    let fn = eval("(function(){ {0} })".replace("{0}", code));
    fn.call(this);
  }
}
</code>```

However, keep in mind that eval is usually evil and that this use case is quite weird. I'm not sure if your user should control the code. Anyhow, that's up to you.

Also, if you want to avoid the "this." in the code text box, you can always use replace on your variable name to add it behind the scene.

Body:
Currently, you can use the user's final script. This script can modify some previously available variables.
I created a small example in pure javascript I would like to do the same in angular 2+ https://jsfiddle.net/hxs3d0hu/2/

Thank you in advance for your attention

HTML Code

      <form>

          Nome
          <input type="text" id="descricao" name="descricao" class="form-control" />

          Valor
          <input type="text" id="valor" name="valor" value="10" class="form-control" />

          Quantidade
          <input type="text" id="quantidade" name="quantidade" value="20" class="form-control" />

          Total
          <input type="text" id="total" name="total" class="form-control" />

          Code
          <textarea id="codex" name="codex" rows="10" cols="100" class="form-control">
          if (qtde < 15) {
              vlr = 10;
          }
          else
          {
              vlr = 9;
          }
          tot = vlr * qtde;
          </textarea>

        <button type="button" id="calcular" name="calcular" onclick="calcularx();" class="btn btn-primary">Calcular</button>
      </form>

JavaScript Code

    var vlr = 0;
    var qtde = 0;
    var tot = 0;

        function calcularx(){
            var valor = document.getElementById("valor");
            var quantidade = document.getElementById("quantidade");
            var total = document.getElementById("total");

            vlr = valor.value;
            qtde = quantidade.value;
            tot = 0;

            tot = qtde * vlr;
            total.value = tot;

            var codex = document.getElementById("codex").value;
            var cst = document.getElementById("customcodescript");
            var corpo = "function custom(){ {0} }".replace("{0}", codex);
            //cst.innerHTML = corpo;
            load_js(cst, corpo);

            custom();

            valor.value = vlr;
            quantidade.value = qtde;
            total.value = tot;

            console.log(tot);
        }

       function load_js(cst, corpo)
       {
          if(cst != null){
            cst.remove();
          }
          var head= document.getElementsByTagName('head')[0];
          var script= document.createElement('script');
          script.id = "customcodescript";
          script.type= 'text/javascript';
          script.innerHTML = corpo;
          //script.src= 'source_file.js';
          head.appendChild(script);
       }

    Element.prototype.remove = function() {
        this.parentElement.removeChild(this);
    }
    NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
        for(var i = this.length - 1; i >= 0; i--) {
            if(this[i] && this[i].parentElement) {
                this[i].parentElement.removeChild(this[i]);
            }
        }
    }

Body:
Here's a plnkr that would achieve what you're looking for.

http://embed.plnkr.co/w2FVfKlWP72pzXIsfsCU/

You create a function with eval, then call it using the component context. All the function and variable in your component will be available to your textbox code :

The template
    Value:

<label>Code:</label>
<textarea [(ngModel)]="code"></textarea> <br>

<button (click)="executeCode(code)">Do it.</button>

The component :

export class HelloWorld {

  value = 100;
  code = 'this.value = this.value * 100';

  executeCode(code){
    let fn = eval("(function(){ {0} })".replace("{0}", code));
    fn.call(this);
  }
}

However, keep in mind that eval is usually evil and that this use case is quite weird. I'm not sure if your user should control the code. Anyhow, that's up to you.

Also, if you want to avoid the "this." in the code text box, you can always use replace on your variable name to add it behind the scene.


问:
Manipulating React State from an Existing DOM Node
I'm currently using a, "Handsontable," wrapper for React (react-handsontable).

The main issue I've been having is being able to hook into a <select> field that was created by the wrapper to change my application state.

I need to hook into an input that's on the DOM (not the virtual DOM) and onchange change my component state.

How can I go about accomplishing this.

答:
Using refs to bind the parent container to access the children and their event delegation

```<code><div ref={ this.bindElementToInstance }>
  <DataTable
    tableData={ tableData }
    header={ OPTIONS[selectedTab].header }
   />
</div>
</code>```

Bind the element

```<code>bindElementToInstance = (element) => {
  this.tableContainer = element;
}
</code>```

use the change event propagation

```<code>componentDidMount() {
 this.tableContainer.addEventListener('change', (e) => {
   if (e.target.name === GRAIN_INPUT_NAME) {
     this.setState({
       selectedGrain: e.target.value,
     });
   }
 });
}
</code>```

I'm now changing the state of my react application with some dark magic

Body:
Using refs to bind the parent container to access the children and their event delegation

<div ref={ this.bindElementToInstance }>
  <DataTable
    tableData={ tableData }
    header={ OPTIONS[selectedTab].header }
   />
</div>

Bind the element

bindElementToInstance = (element) => {
  this.tableContainer = element;
}

use the change event propagation

componentDidMount() {
 this.tableContainer.addEventListener('change', (e) => {
   if (e.target.name === GRAIN_INPUT_NAME) {
     this.setState({
       selectedGrain: e.target.value,
     });
   }
 });
}

I'm now changing the state of my react application with some dark magic


问:
Copy-Item Remotely from list of servers in CSV
Pardon if this is redundant but I can't seem to get this to work.

```<code>    Import-CSV C:\Scripts\VMLIST.csv | ForEach {
$vm = $_.vmname
Copy-Item -Path C:\PSWindowsUpdate\ -Destination \\$vm\C$\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWindowsUpdate\
Copy-Item -Path C:\PSWindowsUpdate\* -Destination \\$vm\C$\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWindowsUpdate\
}
</code>```

The problem is with the $vm variable. I just don't know what I'm doing wrong with syntax. I've tried '', "", brackets. My column is correctly titled and I've used the CSV for snapshots so I know that isn't the problem.

The output says Network Path is Not Found. So does that mean it is not possible?

Thanks for the help.

答:
You issue is the second pipeline command $vm = $_.vmname. This is not allowed to be there like it is.

But it would work if you include it in your ForEach-Object statement:

```<code>Import-CSV C:\Scripts\VMLIST.csv | ForEach {
$vm = $_.vmname
Copy-Item -Path C:\PSWindowsUpdate\ -Destination "\\$vm\C$\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWindowsUpdate\"
Copy-Item -Path C:\PSWindowsUpdate\* -Destination "\\$vm\C$\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWindowsUpdate\"
}
</code>```

It's more readable, if you use the switch -recurse in your copy-statement too:

```<code>Import-CSV C:\Scripts\VMLIST.csv | ForEach {
$vm = $_.vmname
Copy-Item -Path C:\PSWindowsUpdate\ -Destination "\\$vm\C$\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWindowsUpdate\" -rec
}
</code>```

Body:
Pardon if this is redundant but I can't seem to get this to work.

    Import-CSV C:\Scripts\VMLIST.csv | ForEach {
$vm = $_.vmname
Copy-Item -Path C:\PSWindowsUpdate\ -Destination \\$vm\C$\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWindowsUpdate\
Copy-Item -Path C:\PSWindowsUpdate\* -Destination \\$vm\C$\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWindowsUpdate\
}

The problem is with the $vm variable. I just don't know what I'm doing wrong with syntax. I've tried '', "", brackets. My column is correctly titled and I've used the CSV for snapshots so I know that isn't the problem.

The output says Network Path is Not Found. So does that mean it is not possible?

Thanks for the help.

Body:
You issue is the second pipeline command $vm = $_.vmname. This is not allowed to be there like it is.

But it would work if you include it in your ForEach-Object statement:

Import-CSV C:\Scripts\VMLIST.csv | ForEach {
$vm = $_.vmname
Copy-Item -Path C:\PSWindowsUpdate\ -Destination "\\$vm\C$\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWindowsUpdate\"
Copy-Item -Path C:\PSWindowsUpdate\* -Destination "\\$vm\C$\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWindowsUpdate\"
}

It's more readable, if you use the switch -recurse in your copy-statement too:

Import-CSV C:\Scripts\VMLIST.csv | ForEach {
$vm = $_.vmname
Copy-Item -Path C:\PSWindowsUpdate\ -Destination "\\$vm\C$\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWindowsUpdate\" -rec
}


问:
Linq to Sql ExecuteCommand for CREATE DATABASE
I have a ASP.NET MVC application and I am using LinqToSql for database access.

I want to create a database from a SQL script into my DBMS (SQL Server Express).

When I run my script no SQL Server Management Studio it works with same user connection, however, when I run it from LinqToSql with ExecuteCommand it does give errors about I cannot use GO statements. If I remove GO statements and try again, it says CREATE SCHEMA should be the first line of the batch.

Therefore, I am confused, is there any other(better) way of running the sql script, which contains CREATE DATABASE and CREATE SCHEMA statements, from asp.net mvc without these issues.

Thanks,
cas sakal

Here is the content of the script;

```<code>USE [master]
GO
CREATE DATABASE [TESTDB]
CREATE SCHEMA [base] AUTHORIZATION [dbo]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [base].[TestTable](
[Id] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[IsActive] [bit] NOT NULL,
 CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,     ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
</code>```

答:
This works for me:</p>

```<code> using (var db = CreateContext()) {
    if (db.DatabaseExists())
      db.DeleteDatabase();
    db.CreateDatabase();
    using (var sr = new StreamReader("prepareDatabase.sql")) {
      var commands = sr.ReadToEnd().Split(new string[] {"GO\r"}, System.StringSplitOptions.RemoveEmptyEntries);
      foreach (var cmd in commands) {
        db.ExecuteCommand(cmd);
      }
    }
  }
</code>```

Body:
I have a ASP.NET MVC application and I am using LinqToSql for database access.

I want to create a database from a SQL script into my DBMS (SQL Server Express).

When I run my script no SQL Server Management Studio it works with same user connection, however, when I run it from LinqToSql with ExecuteCommand it does give errors about I cannot use GO statements. If I remove GO statements and try again, it says CREATE SCHEMA should be the first line of the batch.

Therefore, I am confused, is there any other(better) way of running the sql script, which contains CREATE DATABASE and CREATE SCHEMA statements, from asp.net mvc without these issues.

Thanks,
cas sakal

Here is the content of the script;

USE [master]
GO
CREATE DATABASE [TESTDB]
CREATE SCHEMA [base] AUTHORIZATION [dbo]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [base].[TestTable](
[Id] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[IsActive] [bit] NOT NULL,
 CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,     ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Body:
This works for me:

 using (var db = CreateContext()) {
    if (db.DatabaseExists())
      db.DeleteDatabase();
    db.CreateDatabase();
    using (var sr = new StreamReader("prepareDatabase.sql")) {
      var commands = sr.ReadToEnd().Split(new string[] {"GO\r"}, System.StringSplitOptions.RemoveEmptyEntries);
      foreach (var cmd in commands) {
        db.ExecuteCommand(cmd);
      }
    }
  }


问:
Lift LinearLayout on click
In res/animator directory I have lift_on_touch.xml file which should lift linear layout on click (NOT press or hold but just short CLICK). The file itself

```<code><selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <objectAnimator android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="elevation"
            android:valueTo="6dp"
            android:valueType="floatType" />
    </item>
    <item>
        <objectAnimator android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="elevation"
            android:valueTo="2dp"
            android:valueType="floatType" />
    </item>
</selector>
</code>```

But when I apply stateListAnimator to my linear layout in layout xml like this android:stateListAnimator="@animator/lift_on_touch" it only works (i.e. lifts) when I press and hold my linear layout BUT NOT when I just click it. I also have android:clickable="true" in my linear layout.

Question: How to lift my linear layout on click?

答:
i think you should use onTouchListner() instead of onClickListner
  bcs here you can get the event of Hold and unHold of View by using
  below code

```<code>linearLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = MotionEventCompat.getActionMasked(event);

                switch(action) {
                    case (MotionEvent.ACTION_DOWN) :
                        Log.d(DEBUG_TAG, "Action was DOWN");
                        // Hold Event will invoke continues
                        return true;
                    case (MotionEvent.ACTION_MOVE) :
                        Log.d(DEBUG_TAG,"Action was MOVE");
                        return true;
                    case (MotionEvent.ACTION_UP) :
                        Log.d(DEBUG_TAG,"Action was UP");
                        // here unhold event will invoke
                        return true;
                    case (MotionEvent.ACTION_CANCEL) :
                        Log.d(DEBUG_TAG,"Action was CANCEL");
                        return true;
                    case (MotionEvent.ACTION_OUTSIDE) :
                        Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                                "of current screen element");
                        return true;

                }
                return false;
            }
        });
</code>```

Title: Lift LinearLayout on click

Body:
In res/animator directory I have lift_on_touch.xml file which should lift linear layout on click (NOT press or hold but just short CLICK). The file itself

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <objectAnimator android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="elevation"
            android:valueTo="6dp"
            android:valueType="floatType" />
    </item>
    <item>
        <objectAnimator android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="elevation"
            android:valueTo="2dp"
            android:valueType="floatType" />
    </item>
</selector>

But when I apply stateListAnimator to my linear layout in layout xml like this android:stateListAnimator="@animator/lift_on_touch" it only works (i.e. lifts) when I press and hold my linear layout BUT NOT when I just click it. I also have android:clickable="true" in my linear layout.

Question: How to lift my linear layout on click?

Body:
i think you should use onTouchListner() instead of onClickListner
  bcs here you can get the event of Hold and unHold of View by using
  below code

linearLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = MotionEventCompat.getActionMasked(event);

                switch(action) {
                    case (MotionEvent.ACTION_DOWN) :
                        Log.d(DEBUG_TAG, "Action was DOWN");
                        // Hold Event will invoke continues
                        return true;
                    case (MotionEvent.ACTION_MOVE) :
                        Log.d(DEBUG_TAG,"Action was MOVE");
                        return true;
                    case (MotionEvent.ACTION_UP) :
                        Log.d(DEBUG_TAG,"Action was UP");
                        // here unhold event will invoke
                        return true;
                    case (MotionEvent.ACTION_CANCEL) :
                        Log.d(DEBUG_TAG,"Action was CANCEL");
                        return true;
                    case (MotionEvent.ACTION_OUTSIDE) :
                        Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                                "of current screen element");
                        return true;

                }
                return false;
            }
        });


问:
Trouble with background position fixed on mobiles
Sorry in advandce, my english is poor... i'm going to try be clear.

I develop a website with a background picture fixed. Obviously background-position: fixed doesn't works on mobiles and IPAD!
So i've succeed to get around the problem as you can see in my code, ( picture display width and height 100% in background)
here's it:

```<code>  <body>
    <div class="container-fluid no-marge img-responsive">

    <img  src="crayons.jpg" class="bg-img" alt="">

    <div class="rowtitle col-md-12 outline">
      <h1>title</h1>
    </div>
        <h1 class="col-md-12 titre-accueil">Bienvenue</h1>
        <div class="row col-md-12 carousel-space">

        </div>
        <div class="row ">
            <div class="col-md-12 textePresentation ">
                <div class="col-md-offset-1 col-md-10 col-md-offset-1 ">
                    <h1 class="titre-qsn"><br>Qui sommes nous ?</h1><br><br><br>
                    <div class="col-md-4 "><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!</p>       <p>Lorem ibus porro? A vel, quam totam!</p>     <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!</p>  <br><br>
                    </div>
                    <div class="col-md-4"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!</p>        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!</p>      <p>tetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!</p><br><br>
                    </div>
                    <div class="col-md-4"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!</p>        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!</p><br><br>
                    </div>
                </div>
            </div>
        </div>

        <div id="footer" class="row footer col-lg-12 no-marge "><br>

        </div>
    </div><!--container-fluid-->

</body>
</code>```

and the css:

```<code>html
{
    height: 100%;
    width:100%;
}

body
{
 /* content: "";
  display: block;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: -10;
  background: url('crayons.jpg') no-repeat center center;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;*/
}

.rowtitle
{
  background-color: red;
}

.bg-img
{
  position: fixed;
  top: 0;
  left: 0;
  min-width: 100%;
  min-height: 100%;
  background: url('crayons.jpg');
  background-size: cover;
}

.titre-accueil
{
    margin-top: 50px;
    margin-bottom: 50px;
    width:100%;
    font-size: 3.8em;
    text-shadow: -1px -1px #eee, 1px 1px #888, -3px 0 4px #000;
    font-family:"Segoe print", Arial, Helvetica, sans-serif;
    color:white;
    font-weight:lighter;
    text-align:center;
    display:inline;
 }

.bandeau-carousel
{
    border: 35px solid rgba(255, 255, 255, .3);
    border-radius: 10px;
    padding: 0;
}

.textePresentation
{
    margin-top: 50px;
    width : 100%;
    background-color: rgb(235, 235, 235);
    box-shadow: 8px 8px 12px #aaa;
}

.carousel-space
{
    width:100%;
    height: 250px;
}

.footer
{
    /*padding-top: 50px;*/
    height: 200px;
    background-color: rgb(255, 73, 53);
    text-align: center;
    vertical-align: middle;
    margin: auto;
    position: relative;
}

.no-marge
{
    margin:0;
    padding:0;
}

.outline
{
  border: solid 3px red;
}
</code>```

The issue is that when i scroll the top menu ( nav-bar and title) down and we see the background. i want the background don't exceed the .

I think it's better with a screenshot :
illustration

Maybe someone had a solution to fix this problem ? or just improve the user experience about this.

Thank you very much !

答:
For this kind of situations we have media queries that will help a lot.
This is only for ipad landscape mode. I've didn't tested the code but you can play with styles inside this  { }

```<code>@media only screen
        and (min-device-width : 768px)
        and (max-device-width : 1024px)
        and (orientation : landscape)
        and (-webkit-min-device-pixel-ratio: 1)  {
              width: 100%;
              height: 100%;
              max-width: 800px;
              max-height: 600px;
    }
</code>```

Body:
Sorry in advandce, my english is poor... i'm going to try be clear.

I develop a website with a background picture fixed. Obviously background-position: fixed doesn't works on mobiles and IPAD!
So i've succeed to get around the problem as you can see in my code, ( picture display width and height 100% in background)
here's it:

title

Bienvenue

Qui sommes nous ?

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!

Lorem ibus porro? A vel, quam totam!

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!

tetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates error libero repellendus ullam, deserunt vitae sint obcaecati asperiores, maiores culpa optio assumenda, mollitia repellat doloribus porro? A vel, quam totam!

and the css:

html
{
    height: 100%;
    width:100%;
}

body
{
 /* content: "";
  display: block;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: -10;
  background: url('crayons.jpg') no-repeat center center;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;*/
}

.rowtitle
{
  background-color: red;
}

.bg-img
{
  position: fixed;
  top: 0;
  left: 0;
  min-width: 100%;
  min-height: 100%;
  background: url('crayons.jpg');
  background-size: cover;
}

.titre-accueil
{
    margin-top: 50px;
    margin-bottom: 50px;
    width:100%;
    font-size: 3.8em;
    text-shadow: -1px -1px #eee, 1px 1px #888, -3px 0 4px #000;
    font-family:"Segoe print", Arial, Helvetica, sans-serif;
    color:white;
    font-weight:lighter;
    text-align:center;
    display:inline;
 }

.bandeau-carousel
{
    border: 35px solid rgba(255, 255, 255, .3);
    border-radius: 10px;
    padding: 0;
}

.textePresentation
{
    margin-top: 50px;
    width : 100%;
    background-color: rgb(235, 235, 235);
    box-shadow: 8px 8px 12px #aaa;
}

.carousel-space
{
    width:100%;
    height: 250px;
}

.footer
{
    /*padding-top: 50px;*/
    height: 200px;
    background-color: rgb(255, 73, 53);
    text-align: center;
    vertical-align: middle;
    margin: auto;
    position: relative;
}

.no-marge
{
    margin:0;
    padding:0;
}

.outline
{
  border: solid 3px red;
}

The issue is that when i scroll the top menu ( nav-bar and title) down and we see the background. i want the background don't exceed the .

I think it's better with a screenshot :
illustration

Maybe someone had a solution to fix this problem ? or just improve the user experience about this.

Thank you very much !

Body:
For this kind of situations we have media queries that will help a lot.
This is only for ipad landscape mode. I've didn't tested the code but you can play with styles inside this  { }

@media only screen
        and (min-device-width : 768px)
        and (max-device-width : 1024px)
        and (orientation : landscape)
        and (-webkit-min-device-pixel-ratio: 1)  {
              width: 100%;
              height: 100%;
              max-width: 800px;
              max-height: 600px;
    }


问:
Make distinction between inner and outer NA's in a raster in R
In R, how could I make the distinction between inner and outer NA's in a raster with some shape having NA's both around but also inside?

In the example below, how could I for exemple select only the NA's outside the R logo (i.e., how can I make everything which is included in the circle of the logo appear as white)?

```<code>library(raster)
r <- raster(system.file("external/rlogo.grd", package="raster"))
r[r>240] = NA
par(mfrow=c(1,2))
plot(r, main='r')
plot(is.na(r), main="is.na(r)")
</code>```

答:
I agree with @maRtin, it's  a bit tricky. Not only don't you have a dedicated NoData value, but also the image is a bit smudgy.

Nevertheless, I think I found a way which is a bit better than clump, which uses the spatial domain to separate the areas:

First, I'm getting the focal values of the pixel neighbourhoods:

```<code>#make copy
r2 <- r

# focal values
fv <- getValuesFocal(r2,ngb = c(3,3))
</code>```

Then I I first I exclude all pixels, which have a neighbourhood value of greater than 242.8. This was purely trial and error, but it gives a good result.

```<code>ix <- rowMeans(fv,na.rm = T) > 242.8
r2[ix] <- NA
</code>```

You could actually already deem this acceptable. The only problem is, that there is a small border around the value area which should be NA.

So somehow I need to get rid of the remaining NA pixels. This I try to do with an iterative exclusion. For every iteration, I see if there's pixels which have still NA values around and a max value lower than a certain threshold. Again, there's a lot of playing around involved and I guess you could achieve a better result than this, but I guess this would be a way to go.

```<code>while (TRUE){

  fv <- getValuesFocal(r2,ngb = c(3,3))
  ix <- apply(fv,1,function(x) max(x,na.rm=T)) > 243 & rowSums(is.na(fv)) > 0

  if (any(ix)){

    r2[ix] <- NA

  } else {
    break
  }
}
</code>```

After a couple of iterations, I get this:

There are clearly already some pixels gone which shouldn't be, maybe it could be done with a bit more fiddling around.

Another interesting thought would be looking at all three channels. If you load the image with brick, you can get the RGB channels. I've tried a few things like max, mean, mode, sd, etc. but to no avail.

Body:
In R, how could I make the distinction between inner and outer NA's in a raster with some shape having NA's both around but also inside?

In the example below, how could I for exemple select only the NA's outside the R logo (i.e., how can I make everything which is included in the circle of the logo appear as white)?

library(raster)
r <- raster(system.file("external/rlogo.grd", package="raster"))
r[r>240] = NA
par(mfrow=c(1,2))
plot(r, main='r')
plot(is.na(r), main="is.na(r)")

Body:
I agree with @maRtin, it's  a bit tricky. Not only don't you have a dedicated NoData value, but also the image is a bit smudgy.

Nevertheless, I think I found a way which is a bit better than clump, which uses the spatial domain to separate the areas:

First, I'm getting the focal values of the pixel neighbourhoods:

#make copy
r2 <- r

# focal values
fv <- getValuesFocal(r2,ngb = c(3,3))

Then I I first I exclude all pixels, which have a neighbourhood value of greater than 242.8. This was purely trial and error, but it gives a good result.

ix <- rowMeans(fv,na.rm = T) > 242.8
r2[ix] <- NA

You could actually already deem this acceptable. The only problem is, that there is a small border around the value area which should be NA.

So somehow I need to get rid of the remaining NA pixels. This I try to do with an iterative exclusion. For every iteration, I see if there's pixels which have still NA values around and a max value lower than a certain threshold. Again, there's a lot of playing around involved and I guess you could achieve a better result than this, but I guess this would be a way to go.

while (TRUE){

  fv <- getValuesFocal(r2,ngb = c(3,3))
  ix <- apply(fv,1,function(x) max(x,na.rm=T)) > 243 & rowSums(is.na(fv)) > 0

  if (any(ix)){

    r2[ix] <- NA

  } else {
    break
  }
}

After a couple of iterations, I get this:

There are clearly already some pixels gone which shouldn't be, maybe it could be done with a bit more fiddling around.

Another interesting thought would be looking at all three channels. If you load the image with brick, you can get the RGB channels. I've tried a few things like max, mean, mode, sd, etc. but to no avail.


问:
Convert List to csv String
I'm trying to convert the list to CSV string.list have 200+ properties.So I need properties as well values
Example

Id,name,class,lastname....so on..

1,test,second,test....so on..

2,test2,second,test2...so on...

I used append with foreach

```<code>  foreach (var value in testData)
        {
            sb.Append(separator).Append(value);
            separator = ",";
              sb.Append(separator).Append(value);
            separator = ",";
        }
</code>```

I need to pass 200 properties it increases the line of code.

答:
Here is something I have implemented that takes an object and reads the property values then formats it as a csv'd string.

The Write.WriteObject() is an example of how you would use it.

```<code>public static class StringExtensions
{
    public static string ToCSV(this object field, bool first)
    {
        if(field != null)
        {
            string retVal = field.ToString();
            retVal = retVal.Contains(",") || retVal.Contains("\"") || retVal.Contains("\r\n")
                ? "\"" + retVal + "\"" : retVal;
            retVal = first ? retVal : "," + retVal;
            return retVal;
        }
        else
        {
            return first ? "" : ",";
        }
    }
}

public class Format
{
    public string CommaSeparatedObject(Type layoutType, object value, bool header)
    {
        bool first = true;

        string retVal = "";
        PropertyInfo[] props = layoutType.GetProperties();

        if (header)
        {
            foreach (PropertyInfo prop in props)
            {
                retVal += prop.Name.ToCSV(first);
                first = false;
            }
        }
        else
        {
            foreach (PropertyInfo prop in props)
            {
                retVal += prop.GetValue(value).ToCSV(first);
                first = false;
            }
        }

        return retVal;
    }
}

public class Write
{
    public void WriteObject(IEnumerable<myClass> myObj)
    {
        StreamWriter sw = new StreamWriter(parameters.OutputFile, true, Encoding.ASCII, 16 * 1024);

        sw.WriteLine(new Format().CommaSeparatedObject(typeof(myClass), item, true))); //write header

        foreach(var item in myObj)
        {
            sw.WriteLine(new Format().CommaSeparatedObject(typeof(myClass), item, false)));
        }
    }
}
</code>```

Title: Convert List to csv String

Body:
I'm trying to convert the list to CSV string.list have 200+ properties.So I need properties as well values
Example

Id,name,class,lastname....so on..

1,test,second,test....so on..

2,test2,second,test2...so on...

I used append with foreach

  foreach (var value in testData)
        {
            sb.Append(separator).Append(value);
            separator = ",";
              sb.Append(separator).Append(value);
            separator = ",";
        }

I need to pass 200 properties it increases the line of code.

Body:
Here is something I have implemented that takes an object and reads the property values then formats it as a csv'd string.

The Write.WriteObject() is an example of how you would use it.

public static class StringExtensions
{
    public static string ToCSV(this object field, bool first)
    {
        if(field != null)
        {
            string retVal = field.ToString();
            retVal = retVal.Contains(",") || retVal.Contains("\"") || retVal.Contains("\r\n")
                ? "\"" + retVal + "\"" : retVal;
            retVal = first ? retVal : "," + retVal;
            return retVal;
        }
        else
        {
            return first ? "" : ",";
        }
    }
}

public class Format
{
    public string CommaSeparatedObject(Type layoutType, object value, bool header)
    {
        bool first = true;

        string retVal = "";
        PropertyInfo[] props = layoutType.GetProperties();

        if (header)
        {
            foreach (PropertyInfo prop in props)
            {
                retVal += prop.Name.ToCSV(first);
                first = false;
            }
        }
        else
        {
            foreach (PropertyInfo prop in props)
            {
                retVal += prop.GetValue(value).ToCSV(first);
                first = false;
            }
        }

        return retVal;
    }
}

public class Write
{
    public void WriteObject(IEnumerable<myClass> myObj)
    {
        StreamWriter sw = new StreamWriter(parameters.OutputFile, true, Encoding.ASCII, 16 * 1024);

        sw.WriteLine(new Format().CommaSeparatedObject(typeof(myClass), item, true))); //write header

        foreach(var item in myObj)
        {
            sw.WriteLine(new Format().CommaSeparatedObject(typeof(myClass), item, false)));
        }
    }
}


问:
How to fetch particular info from XML (Python)
I need to fetch the username from this XML script. I read up on the element tree library in Python and have tried what they did with my XML file but to no avail have I succeeded. All I need is an example to fetch the username and that would be very helpful and help me understand the rest. First is the XML file and then the code.</p>

```<code><?xml version="1.0" encoding="UTF-8" ?>
- <definitions name="GetEmailCert" targetNamespace="urn:GetEmailCert"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="urn:GetEmailCert" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <message name="GetEmailCertRequest">
  <part name="username" type="xsd:string" />
  </message>
- <message name="GetEmailCertResponse">
  <part name="fullname" type="xsd:string" />
  <part name="email" type="xsd:string" />
  <part name="certificate" type="xsd:string" />
  </message>
</code>```

<p> </p>

```<code>from xml.etree.ElementTree import ElementTree

tree = ElementTree()
tree.parse("test.xml")
root = tree.getroot()
root.tag
root.attrib

for child in root:
        print child.tag, child.attrib
#This one works

for username in root.iter('username'):
        print username
#This one I do not know how to correctly implement
</code>```

答:
In addition to the - (hyphen) char discovered by @Robᵩ (which makes me believe that you copy/pasted the XML blob from user-friendly displaying tool), I discovered 2 additional ones:

definitions node was not properly closed

the XML had more than one root node (3 in our case). According to [W3]: Extensible Markup Language (XML) 1.0 (Fifth Edition):

Definition: There is exactly one element, called the root, or document element ...

Because these errors, the XML is still invalid:

Python's xml.etree.ElementTree fails to parse it

2 Web Browsers (Chrome and IE) fail to render it

After fixing all the errors, the XML looks like this (note that I've encapsulated everything in a root node (that I called root)):

```<code><?xml version="1.0" encoding="UTF-8" ?>
  <root>
    <definitions name="GetEmailCert" targetNamespace="urn:GetEmailCert"
      xmlns="http://schemas.xmlsoap.org/wsdl/"
      xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
      xmlns:tns="urn:GetEmailCert" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <message name="GetEmailCertRequest">
      <part name="username" type="xsd:string"/>
    </message>
    <message name="GetEmailCertResponse">
      <part name="fullname" type="xsd:string"/>
      <part name="email" type="xsd:string"/>
      <part name="certificate" type="xsd:string"/>
    </message>
  </root>
</code>```

Code-wise: there are more ways (including XML libraries) of achieving your goal; I chose the one that uses [Python]: xml.etree.ElementTree — The ElementTree XML API (as you started implementing).

Now, I didn't quite understand what did you mean by "fetch the username", but I assumed that it's the <part name="username" type="xsd:string"/> node. Considering that my assumption is correct, here's the tree-like approach (it's supposed to be general):

The XML document from test.xml has one root node (tagged root)

The root node has 0 or more (2 for our XML) message nodes (tagged message)

Each message node has 0 or more part nodes (tagged part)

Each part node may or may not have a name attribute whose value is username; if it does, then it's the node that we want (print it)

And the code:

```<code>import xml.etree.ElementTree as ET

def main():
    tree = ET.parse("test.xml")
    root_node = tree.getroot()

    message_nodes = root_node.iter("message")
    for message_node in message_nodes:
        part_nodes = message_node.iter("part")
        for part_node in part_nodes:
            if part_node.attrib.get("name", "") == "username":
                print(ET.tostring(part_node))

if __name__ == "__main__":
    main()
</code>```

Body:
I need to fetch the username from this XML script. I read up on the element tree library in Python and have tried what they did with my XML file but to no avail have I succeeded. All I need is an example to fetch the username and that would be very helpful and help me understand the rest. First is the XML file and then the code.

<?xml version="1.0" encoding="UTF-8" ?>
- <definitions name="GetEmailCert" targetNamespace="urn:GetEmailCert"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="urn:GetEmailCert" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <message name="GetEmailCertRequest">
  <part name="username" type="xsd:string" />
  </message>
- <message name="GetEmailCertResponse">
  <part name="fullname" type="xsd:string" />
  <part name="email" type="xsd:string" />
  <part name="certificate" type="xsd:string" />
  </message>

from xml.etree.ElementTree import ElementTree

tree = ElementTree()
tree.parse("test.xml")
root = tree.getroot()
root.tag
root.attrib

for child in root:
        print child.tag, child.attrib
#This one works

for username in root.iter('username'):
        print username
#This one I do not know how to correctly implement

Body:
In addition to the - (hyphen) char discovered by @Robᵩ (which makes me believe that you copy/pasted the XML blob from user-friendly displaying tool), I discovered 2 additional ones:

definitions node was not properly closed

the XML had more than one root node (3 in our case). According to [W3]: Extensible Markup Language (XML) 1.0 (Fifth Edition):

Definition: There is exactly one element, called the root, or document element ...

Because these errors, the XML is still invalid:

Python's xml.etree.ElementTree fails to parse it

2 Web Browsers (Chrome and IE) fail to render it

After fixing all the errors, the XML looks like this (note that I've encapsulated everything in a root node (that I called root)):

<?xml version="1.0" encoding="UTF-8" ?>
  <root>
    <definitions name="GetEmailCert" targetNamespace="urn:GetEmailCert"
      xmlns="http://schemas.xmlsoap.org/wsdl/"
      xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
      xmlns:tns="urn:GetEmailCert" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <message name="GetEmailCertRequest">
      <part name="username" type="xsd:string"/>
    </message>
    <message name="GetEmailCertResponse">
      <part name="fullname" type="xsd:string"/>
      <part name="email" type="xsd:string"/>
      <part name="certificate" type="xsd:string"/>
    </message>
  </root>

Code-wise: there are more ways (including XML libraries) of achieving your goal; I chose the one that uses [Python]: xml.etree.ElementTree — The ElementTree XML API (as you started implementing).

Now, I didn't quite understand what did you mean by "fetch the username", but I assumed that it's the <part name="username" type="xsd:string"/> node. Considering that my assumption is correct, here's the tree-like approach (it's supposed to be general):

The XML document from test.xml has one root node (tagged root)

The root node has 0 or more (2 for our XML) message nodes (tagged message)

Each message node has 0 or more part nodes (tagged part)

Each part node may or may not have a name attribute whose value is username; if it does, then it's the node that we want (print it)

And the code:

import xml.etree.ElementTree as ET

def main():
    tree = ET.parse("test.xml")
    root_node = tree.getroot()

    message_nodes = root_node.iter("message")
    for message_node in message_nodes:
        part_nodes = message_node.iter("part")
        for part_node in part_nodes:
            if part_node.attrib.get("name", "") == "username":
                print(ET.tostring(part_node))

if __name__ == "__main__":
    main()