问:
Error 404 cant find javascript file in folder on cloud 9 using node.js
I am building a website and  i am using node.js on cloud9 platform. I want to use jquery to make a weather API request. So i created a file with a name weather.js and stored my code in it. But when i run the web page i get an error: GET https://my-restfull-web-app-billias86.c9users.io/weather.js 404 (Not Found).
When i transfered my jquery code to footer.ejs file, the code runs without errors. I searched the web but i couldn't find a solution. I found that the problem is the program can't read my weather.js file. I tried: app.use(express.static(__dirname + "/public")); but it din't work. I don't know what to do.
My construction folder is:
forlder construction

A part of my server code in my app.js  is:

```<code>var bodyParser   = require("body-parser"),
methodOverride   = require("method-override"),
expressSanitizer = require("express-sanitizer"),
mongoose         = require("mongoose"),
express          = require("express"),
rpromise         = require('request-promise'),
request          = require("request"),
//serveStatic      = require('serve-static'),
app              = express();

//console.log(process.env.DATABASEURL);

//App Config
var url = process.env.DATABASEURL || "mongodb://localhost/restfull_web_app";
mongoose.connect(url, {useMongoClient: true});
//mongoose.connect(process.env.DATABASEURL, {useMongoClient: true});
//mongoose.connect("mongodb://Billy:billias@ds127126.mlab.com:27126/movieblog", {useMongoClient: true});

mongoose.Promise = global.Promise;
app.use(bodyParser.urlencoded({extented: true}));
app.use(expressSanitizer());
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));

app.listen(process.env.PORT, process.env.IP, function() {
    console.log("The Web App Server has started!");
});

//app.use(express.static("public"));
//app.use('weather', express.static(__dirname + '/public'));
</code>```

This is the footer.ejs code:

```<code>  <script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
 <script src="scripts/weather.js"></script>

 </body>
</html>
</code>```

And this is the index.ejs where i will display the results from the API request:

```<code> <% include ./partials/header %>

<div class = "ui main text container">
   <div class="main">
        <h2 id="currentLoc">
            Getting location...<span id="nameLoc"></span><!-- This is where the weather forecast will be shown for now-->
        </h2>
        <p id="weather"></p>
        <p id="temp"></p>
        <p id="highLow"></p>
        <a id="toggle" href="#">Toggle ℃ / ℉</a>

    </div>

    <div class="ui huge header">Movie Blog</div>
        <div class="ui top attached segment"><!-- This gives a line box -->
            <div class="ui divided items">
                <% movies.forEach(function(movie) { %>
                    <div class="item">
                        <div class="image">
                            <img src="<%= movie.image %>">
                        </div>
                        <div class="content">
                            <a class="header" href="/movies/<%= movie._id %>"><%= movie.title%></a>
                            <div class="meta">
                                <span><%= movie.created.toDateString() %></span>
                            </div>
                            <div class="description">
                                <p><%= movie.body.substring(0, 100) %>...</p>
                            </div>
                            <div class="extra">
                                <a class="ui floated basic  violet button"  href="/movies/<%= movie._id %>">
                                    Read More
                                </a>
                            </div>
                        </div>
                    </div>
                <% }) %>
            </div>
        </div>
    </div>

<% include ./partials/footer %>
</code>```

答: Ok guys, i found the mistake. My file was in the wrong directory. Now it's working fine.</p>

Body:
I am building a website and  i am using node.js on cloud9 platform. I want to use jquery to make a weather API request. So i created a file with a name weather.js and stored my code in it. But when i run the web page i get an error: GET https://my-restfull-web-app-billias86.c9users.io/weather.js 404 (Not Found).
When i transfered my jquery code to footer.ejs file, the code runs without errors. I searched the web but i couldn't find a solution. I found that the problem is the program can't read my weather.js file. I tried: app.use(express.static(__dirname + "/public")); but it din't work. I don't know what to do.
My construction folder is:
forlder construction

A part of my server code in my app.js  is:

var bodyParser   = require("body-parser"),
methodOverride   = require("method-override"),
expressSanitizer = require("express-sanitizer"),
mongoose         = require("mongoose"),
express          = require("express"),
rpromise         = require('request-promise'),
request          = require("request"),
//serveStatic      = require('serve-static'),
app              = express();

//console.log(process.env.DATABASEURL);

//App Config
var url = process.env.DATABASEURL || "mongodb://localhost/restfull_web_app";
mongoose.connect(url, {useMongoClient: true});
//mongoose.connect(process.env.DATABASEURL, {useMongoClient: true});
//mongoose.connect("mongodb://Billy:billias@ds127126.mlab.com:27126/movieblog", {useMongoClient: true});

mongoose.Promise = global.Promise;
app.use(bodyParser.urlencoded({extented: true}));
app.use(expressSanitizer());
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));

app.listen(process.env.PORT, process.env.IP, function() {
    console.log("The Web App Server has started!");
});

//app.use(express.static("public"));
//app.use('weather', express.static(__dirname + '/public'));

This is the footer.ejs code:

And this is the index.ejs where i will display the results from the API request:

 <% include ./partials/header %>

            Getting location...

        Toggle ℃ / ℉

Movie Blog

                <% movies.forEach(function(movie) { %>

                            <img src="<%= movie.image %>">

                            <a class="header" href="/movies/<%= movie._id %>"><%= movie.title%>

                                <%= movie.created.toDateString() %>

<%= movie.body.substring(0, 100) %>...

                                <a class="ui floated basic  violet button"  href="/movies/<%= movie._id %>">
                                    Read More

                <% }) %>

<% include ./partials/footer %>


来源: stackexchange

问:
How do I create kanji (Japanese letters) animations from SVG data in Objective-C?
I've seen several iPhone/iPad apps that show animated kanji. For those of you who are unfamiliar with kanji, stroke order is a very important part of kanji studying so if you are doing an app showing the animated stroke order is an essential part.

All the apps I've seen that do this, credit the KanjiVG project as their source for the stroke order data. After some research I found that the KanjiVG project gives you the data in SVG format encoded in XML.

Having never programmed graphics before (and being relatively new to iOS) I'm at a loss to where to keep looking for info.

I think I need to:

Parse the XML into SVG.

Render the SVG.

...but I'm not sure. For what I could see how this is done in the iPhone/iPad apps I bought, the animations all look surprisingly similar so there must be a common library that these guys are using that I'm failing to find (probably because I don't know exactly what I'm looking for!)

Any pointers that anyone can give me will be greatly appreciated.

Cheers!

答: You can also consider "AnimCJK project" (https://github.com/parsimonhi/animCJK) as source for kanji svg data. The principle is slightly different from "KanjiVG project" (http://kanjivg.tagaini.net/) and the underlying font is different (kaisho style), but extracting the path data is simple. Using a regex such as #<path[^>]+id="z[0-9]+d[0-9]+"[^>]+d="([^"]+)"[^>]+># does the job.</p>

ContentLicense: CC BY-SA 3.0


问:
Identity Server as service provider (WS-fed)
I'm trying to work with idsrv3 recieving tokens issued by an ADFS external company, so idsrv3 works as a service provider and the ADFS as identity provider. I'm using the Ws-Fed(pasive) protocol so the company already added our endpoint and the claims that should issue with the token.

I did a login request over the URL https://adfsapp.companydomain.com/adfs/ls/idpinitiatedsignon.aspx and after introducing the credentials it authenticates the user and it redirects to the idsrv3 endpoint with a SAMLResponse.

Following the idsrv3 documentation I configured the identity provider in the app. ( https://identityserver.github.io/Documentation/docsv2/configuration/identityProviders.html )

```<code> private static void AddExternalProvider(IAppBuilder app, string signInAsType, AdfsWsFederationExternalProvider provider)
    {
        var metadataAddress = ""https://adfs.leastprivilege.vm/federationmetadata/2007-06/federationmetadata.xml";
        var manager =
            new SyncConfigurationManager(new ConfigurationManager<WsFederationConfiguration>(metadataAddress));
        var providerId = provider.ExternalProviderId;

        var options = new WsFederationAuthenticationOptions
        {
            AuthenticationType = providerId,
            Caption = provider.ExternalProviderName,
            SignInAsAuthenticationType = signInAsType,
            CallbackPath = new PathString("/" + providerId),
            ConfigurationManager = manager,
            Wtrealm = provider.Wtrealm,

        };
        app.UseWsFederationAuthentication(options);
    }
</code>```

But the token isn't validated so I have the following questions:

1- Should I declare a token handler to treat the tokens issued by the company STS?

2- The callback path "/providerId" will handle automatically the tokens ?

3- How to know if the endpoint ("/providerId") is waiting for incoming tokens ?

Thanks for your help.

答:
You are using WS-Federation as the protocol but are then trying IDPInitiated which is only applicable to the SAML-P protocol.

idsrv3 does not handle the SAML protocol by default.

What happens when you start from the application and then end up on ADFS?

Body:
I'm trying to work with idsrv3 recieving tokens issued by an ADFS external company, so idsrv3 works as a service provider and the ADFS as identity provider. I'm using the Ws-Fed(pasive) protocol so the company already added our endpoint and the claims that should issue with the token.

I did a login request over the URL https://adfsapp.companydomain.com/adfs/ls/idpinitiatedsignon.aspx and after introducing the credentials it authenticates the user and it redirects to the idsrv3 endpoint with a SAMLResponse.

Following the idsrv3 documentation I configured the identity provider in the app. ( https://identityserver.github.io/Documentation/docsv2/configuration/identityProviders.html )

 private static void AddExternalProvider(IAppBuilder app, string signInAsType, AdfsWsFederationExternalProvider provider)
    {
        var metadataAddress = ""https://adfs.leastprivilege.vm/federationmetadata/2007-06/federationmetadata.xml";
        var manager =
            new SyncConfigurationManager(new ConfigurationManager<WsFederationConfiguration>(metadataAddress));
        var providerId = provider.ExternalProviderId;

        var options = new WsFederationAuthenticationOptions
        {
            AuthenticationType = providerId,
            Caption = provider.ExternalProviderName,
            SignInAsAuthenticationType = signInAsType,
            CallbackPath = new PathString("/" + providerId),
            ConfigurationManager = manager,
            Wtrealm = provider.Wtrealm,

        };
        app.UseWsFederationAuthentication(options);
    }

But the token isn't validated so I have the following questions:

1- Should I declare a token handler to treat the tokens issued by the company STS?

2- The callback path "/providerId" will handle automatically the tokens ?

3- How to know if the endpoint ("/providerId") is waiting for incoming tokens ?

Thanks for your help.


问:
Getting a input option to happen in all input statements without repeating if/elif/else statments
Wondering if there is a way to get python to accept "save" or "s" in a input statement  to do something every time you enter that into any if/elif/else in the .py file, so i don't have to repeat if/elif/else several times. I want some thing like this

```<code>a=input("some other question that i want to accept s/save: ")
b=input("Again but i dont want 2 if statments: ")
</code>```

rather than.

```<code>a=input("question: ")
if a == "y":
    print("something")
elif a == "y":
    print("Something")
elif a in ("s", "save")
    print("save")
else:
    print("not option")
print("A bunch of other code for 100 lines")
a=input("question: ")
if a == "y":
    print("something")
elif a == "y":
    print("Something")
elif a in ("s", "save")
    print("save")
else:
    print("not option")
</code>```

and so on through the code

答:
Put the duplicated code in a function, call the function multiple times:

```<code>def ask_question(q):
    a=input(q)
    if a == "y":
        print("something")
    elif a == "y":
        print("Something")
    elif a in ("s", "save")
        print("save")
    else:
        print("not option")

# now

ask_question("first question")

print("A bunch of other code for 100 lines")

ask_question("second question")
</code>```

Of course your ask_question() needs to be more sophisticated to be useful - e.g. return a value or call other functions.

Body:
Wondering if there is a way to get python to accept "save" or "s" in a input statement  to do something every time you enter that into any if/elif/else in the .py file, so i don't have to repeat if/elif/else several times. I want some thing like this

a=input("some other question that i want to accept s/save: ")
b=input("Again but i dont want 2 if statments: ")

rather than.

a=input("question: ")
if a == "y":
    print("something")
elif a == "y":
    print("Something")
elif a in ("s", "save")
    print("save")
else:
    print("not option")
print("A bunch of other code for 100 lines")
a=input("question: ")
if a == "y":
    print("something")
elif a == "y":
    print("Something")
elif a in ("s", "save")
    print("save")
else:
    print("not option")

and so on through the code

Body:
Put the duplicated code in a function, call the function multiple times:

def ask_question(q):
    a=input(q)
    if a == "y":
        print("something")
    elif a == "y":
        print("Something")
    elif a in ("s", "save")
        print("save")
    else:
        print("not option")

# now

ask_question("first question")

print("A bunch of other code for 100 lines")

ask_question("second question")

Of course your ask_question() needs to be more sophisticated to be useful - e.g. return a value or call other functions.


问:
Returning a DataTable using Entity Framework ExecuteStoreQuery
I am working with a system that has many stored procedures that need to be displayed. Creating entities for each of my objects is not practical.

Is it possible and how would I return a DataTable using ExecuteStoreQuery ?

```<code>public ObjectResult<DataTable> MethodName(string fileSetName) {
using (var dataContext = new DataContext(_connectionString))
{
var returnDataTable = ((IObjectContextAdapter)dataContext).ObjectContext.ExecuteStoreQuery<DataTable>("SP_NAME","SP_PARAM");
return returnDataTable;
}
</code>```

答:
Yes it can easily be done like this:</p>

```<code>var table = new DataTable();
using (var ctx = new SomeContext())
{
    var cmd = ctx.Database.Connection.CreateCommand();
    cmd.CommandText = "Select Col1, Col2 from SomeTable";

    cmd.Connection.Open();
    table.Load(cmd.ExecuteReader());
}
</code>```

Body:
I am working with a system that has many stored procedures that need to be displayed. Creating entities for each of my objects is not practical.

Is it possible and how would I return a DataTable using ExecuteStoreQuery ?

public ObjectResult<DataTable> MethodName(string fileSetName) {
using (var dataContext = new DataContext(_connectionString))
{
var returnDataTable = ((IObjectContextAdapter)dataContext).ObjectContext.ExecuteStoreQuery<DataTable>("SP_NAME","SP_PARAM");
return returnDataTable;
}

Body:
Yes it can easily be done like this:

var table = new DataTable();
using (var ctx = new SomeContext())
{
    var cmd = ctx.Database.Connection.CreateCommand();
    cmd.CommandText = "Select Col1, Col2 from SomeTable";

    cmd.Connection.Open();
    table.Load(cmd.ExecuteReader());
}


问:
Missing scope within Object.keys
I'm getting confused, I'm loosing scope within my Object.keys(foo).forEach function and I'm not sure why. Here's the code:

```<code>...
var that = this;
items.forEach(function(value, key) {
   ...
   Object.keys(value).forEach(function(key) {
      that._someFunction();
   }, that);
});
</code>```

Within items.forEach everything is fine, I can access that, but within the Object.keys... that is gone, what's wrong here?

Thanks in advance, Chris

答:
As you said you can not use ES6, you can write something like:</p>

```<code>items.forEach( (function(value, key) {
   ...
   Object.keys(value).forEach((function(key) {
      this._someFunction();
   }).bind(this), this);
}).bind(this));
</code>```

Title: Missing scope within Object.keys

Body:
I'm getting confused, I'm loosing scope within my Object.keys(foo).forEach function and I'm not sure why. Here's the code:

...
var that = this;
items.forEach(function(value, key) {
   ...
   Object.keys(value).forEach(function(key) {
      that._someFunction();
   }, that);
});

Within items.forEach everything is fine, I can access that, but within the Object.keys... that is gone, what's wrong here?

Thanks in advance, Chris

Body:
As you said you can not use ES6, you can write something like:

items.forEach( (function(value, key) {
   ...
   Object.keys(value).forEach((function(key) {
      this._someFunction();
   }).bind(this), this);
}).bind(this));


问:
Error while using kable with booktabs=TRUE option in R-Studio
Does anyone have problem with compiling .pdf output using kable with booktabs=TRUE option in R Studio? Without booktabs it works just fine. When I try to improve the table format adding booktabs=TRUE to kable, I get an error "! Illegal unit of measure (pt inserted)." followed by message of pandoc "Error: pandoc document conversion failed with error 43".

My code is:

```<code>kable(table, col.names = c("Groups", "N","% Cum", "%", "N","% Cum.", "%"),
      caption="Some Caption",row.names = FALSE,align="c",
      format ="latex", booktabs=FALSE)  %>%
      add_header_above(c(" ", "Group 1" = 3, "Group 2" = 3)) %>%
      kable_styling(latex_options = c("striped", "hold_position"))
</code>```

答:
You need to add a call to the booktabs package in LATEX.
Add this to your preamble

```<code>---
output: pdf_document
header-includes:
  - \usepackage{booktabs}
---
</code>```

Body:
Does anyone have problem with compiling .pdf output using kable with booktabs=TRUE option in R Studio? Without booktabs it works just fine. When I try to improve the table format adding booktabs=TRUE to kable, I get an error "! Illegal unit of measure (pt inserted)." followed by message of pandoc "Error: pandoc document conversion failed with error 43".

My code is:

kable(table, col.names = c("Groups", "N","% Cum", "%", "N","% Cum.", "%"),
      caption="Some Caption",row.names = FALSE,align="c",
      format ="latex", booktabs=FALSE)  %>%
      add_header_above(c(" ", "Group 1" = 3, "Group 2" = 3)) %>%
      kable_styling(latex_options = c("striped", "hold_position"))

Body:
You need to add a call to the booktabs package in LATEX.
Add this to your preamble

---
output: pdf_document
header-includes:
  - \usepackage{booktabs}
---


问:
Using ElementArrayFinder.filter() with async/await
I've been using the following function to filter element arrays for the past few years with Webdriver's Control Flow enabled:

```<code>filterElementsByText (elemList, comparator, locator) {
  return elemList.filter((elem) => {
    let searchTarget = locator ? elem.element(locator) : elem
    return searchTarget.getText().then((text) => text === comparator)
  })
}
</code>```

I am now trying to migrate my repo to using async/await which requires turning off the Control Flow.

This transition has been mostly successful, but I'm having trouble with the function above.  Intermittently, I am seeing this error:

Failed: java.net.ConnectException: Connection refused: connect

I am able to reproduce this issue with a test case I've written against https://angularjs.org, although it happens with much higher frequency against my own app.

```<code>let todoList = element.all(by.repeater('todo in todoList.todos'))
let todoText = element(by.model('todoList.todoText'))
let todoSubmit = element(by.css('[value="add"]'))

let addItem = async (itemLabel = 'write first protractor test') => {
  await todoText.sendKeys(itemLabel)
  return todoSubmit.click()
}

let filterElementsByText = (elemList, comparator, locator) => {
  return elemList.filter((elem) => {
    let searchTarget = locator ? elem.element(locator) : elem
    return searchTarget.getText().then((text) => {
      console.log(`Element text is: ${text}`)
      return text === comparator
    })
  })
}

describe('filter should', () => {
  beforeAll(async () => {
    browser.ignoreSynchronization = true
    await browser.get('https://angularjs.org')

    for (let i = 0; i < 10; i++) {
      await addItem(`item${i}`)
    }
    return addItem()
  })

  it('work', async () => {
    let filteredElements = await filterElementsByText(todoList, 'write first protractor test')
    return expect(filteredElements.length).toEqual(1)
  })
})
</code>```

This is being run with the following set in Protractor's conf file:

```<code>SELENIUM_PROMISE_MANAGER: false
</code>```

With the simplified test case it seems to occur on 5-10% of executions (although, anecdotally it does seem to occur more frequently once it occurs the first time)

My problem is, this feels like a bug in Webdriver, but I'm not sure what conditions would cause that error so I'm not sure how to proceed.

答:
For anyone reading and wondering, the problem with my own app was two-fold.

First, as described in the comments to the original question, ElementArrayFinder.filter() causes this error because it runs parallel requests for each element in the array.

Secondly (and not apparent in the original question), rather than passing an ElementArrayFinder as described in this test case, I was actually passing in a chained child of each element in the array such as:

```<code>element.all(by.repeater('todo in todoList.todos').$$('span')
</code>```

Looking at the Webdriver output as this happens I noticed that this then causes all those locators to be retrieved in parallel leading to the same error.

I was able to work around both issues by filtering this way:

```<code>let filterElementsByText = async (elemList, comparator, locator) => {
  let filteredElements = []
  let elems = await elemList
  for (let i = 0; i < elems.length; i++) {
    let elem = await elems[i]
    let searchTarget = locator ? elem.element(locator) : elem
    let text = await searchTarget.getText()
    if (text === comparator) {
      filteredElements.push(elem)
    }
  }
  return filteredElements
}
</code>```

This unblocks me, but it still feels like an issue that these functions are just unusable with async/await.

Body:
I've been using the following function to filter element arrays for the past few years with Webdriver's Control Flow enabled:

filterElementsByText (elemList, comparator, locator) {
  return elemList.filter((elem) => {
    let searchTarget = locator ? elem.element(locator) : elem
    return searchTarget.getText().then((text) => text === comparator)
  })
}

I am now trying to migrate my repo to using async/await which requires turning off the Control Flow.

This transition has been mostly successful, but I'm having trouble with the function above.  Intermittently, I am seeing this error:

Failed: java.net.ConnectException: Connection refused: connect

I am able to reproduce this issue with a test case I've written against https://angularjs.org, although it happens with much higher frequency against my own app.

let todoList = element.all(by.repeater('todo in todoList.todos'))
let todoText = element(by.model('todoList.todoText'))
let todoSubmit = element(by.css('[value="add"]'))

let addItem = async (itemLabel = 'write first protractor test') => {
  await todoText.sendKeys(itemLabel)
  return todoSubmit.click()
}

let filterElementsByText = (elemList, comparator, locator) => {
  return elemList.filter((elem) => {
    let searchTarget = locator ? elem.element(locator) : elem
    return searchTarget.getText().then((text) => {
      console.log(`Element text is: ${text}`)
      return text === comparator
    })
  })
}

describe('filter should', () => {
  beforeAll(async () => {
    browser.ignoreSynchronization = true
    await browser.get('https://angularjs.org')

    for (let i = 0; i < 10; i++) {
      await addItem(`item${i}`)
    }
    return addItem()
  })

  it('work', async () => {
    let filteredElements = await filterElementsByText(todoList, 'write first protractor test')
    return expect(filteredElements.length).toEqual(1)
  })
})

This is being run with the following set in Protractor's conf file:

SELENIUM_PROMISE_MANAGER: false

With the simplified test case it seems to occur on 5-10% of executions (although, anecdotally it does seem to occur more frequently once it occurs the first time)

My problem is, this feels like a bug in Webdriver, but I'm not sure what conditions would cause that error so I'm not sure how to proceed.

Body:
For anyone reading and wondering, the problem with my own app was two-fold.

First, as described in the comments to the original question, ElementArrayFinder.filter() causes this error because it runs parallel requests for each element in the array.

Secondly (and not apparent in the original question), rather than passing an ElementArrayFinder as described in this test case, I was actually passing in a chained child of each element in the array such as:

element.all(by.repeater('todo in todoList.todos').$$('span')

Looking at the Webdriver output as this happens I noticed that this then causes all those locators to be retrieved in parallel leading to the same error.

I was able to work around both issues by filtering this way:

let filterElementsByText = async (elemList, comparator, locator) => {
  let filteredElements = []
  let elems = await elemList
  for (let i = 0; i < elems.length; i++) {
    let elem = await elems[i]
    let searchTarget = locator ? elem.element(locator) : elem
    let text = await searchTarget.getText()
    if (text === comparator) {
      filteredElements.push(elem)
    }
  }
  return filteredElements
}

This unblocks me, but it still feels like an issue that these functions are just unusable with async/await.


问:
how to suppress info messages for hadoop fs command
When we run hadoop fs -get command we see all the info messages. How can I suppress these messages.</p>

```<code>17/12/05 17:59:02 INFO s3n.S3NativeFileSystem: Opening 's3://testbucketzs/Manish/test1/data/csv-serde-1.1.2-0.11.0-all - Copy (796) -
</code>```

答:
You need to modify the file as below :

$HADOOP_HOME/conf/log4j.properties
In this file, replace the following line:

log4j.appender.EventCounter=org.apache.hadoop.log.EventCounter

with this line:
log4j.appender.EventCounter=org.apache.hadoop.log.metrics.EventCounter

If you don't want to do that globally point the stderr to the black hole something like this hdfs dfs -get /hdfs_path/ /local_path/ > 2>/dev/null

Another suggestion use hdfs dfs -get instead of hadoop fs its deprecated.

This link has the explanation if you are interested.

Body:
When we run hadoop fs -get command we see all the info messages. How can I suppress these messages.

17/12/05 17:59:02 INFO s3n.S3NativeFileSystem: Opening 's3://testbucketzs/Manish/test1/data/csv-serde-1.1.2-0.11.0-all - Copy (796) -


问:
Seed data for grails application
What is the best way to load seed (initial or test) data into grails application. I'm considering 3 options

Putting everything in *BootStrap.groovy files. This is tedious if the domain classes and test data are many.

Write custom functionality to load it through xml. May not be too difficult with the excellent xml support by groovy, but lot of switch statements for different domain classes.

Use Liquibase LoadData api. I see you can load the data fairly easy from csv files.

Choice 3 seems the easiest. But, I'm not familiar with Liquibase. Is it good in this scenario, or only used for migration, db changes etc. If anyone could provide a better sol, or point to an example with Liquibase, it would be great help..

答:
One way I have generated seed data is using a service. I created a class, lets call it SeederService. I can inject this service in the Bootstrap.groovy and call whatever method I would want.

The beauty of SeederService is that you can also use the same service in your unit-tests. Simply inject the service class in your unit test and generate your seed data.

Title: Seed data for grails application


问:
Compare the mapping table and then change with the database headers(row 1)
How do I compare a mapping table  (values  in different cells) in excel and map the value of that header to my main database.

Main Database:

Mapping Table:

Tanu's Sheet:

It should map the headers(wgt, ht, bmi, etc)  of the file (tanu, sweety, Raju)  and compare it with main database and replace it with the headers of main database

```<code> The code written so far
 Sub SelectColumn()
 Dim xColIndex As Integer
 Dim xRowIndex As Integer
   xIndex = Application.ActiveCell.Column
    xRowIndex = Application.ActiveSheet.Cells(Rows.Count,
     xIndex).End(xlUp).Row
     Range(Cells(2, xIndex), Cells(xRowIndex, xIndex)).Select
   End Sub
</code>```

Can't get through

答:
This code will check your mapping table and replace headers in each of their Sheets for each workbook tanu, sweety and etc, (it will look for the headers in the range A1:Z1000, change this if you need it to be a bigger range):</p>

```<code>Sub foo3()
Dim Wbook As Workbook
Dim wSheet As Worksheet
Dim wb As ThisWorkbook
Set wb = ThisWorkbook
Application.DisplayAlerts = False
LastCol = wb.Sheets("LMal").Cells(1, Columns.Count).End(xlToLeft).Column 'Check how many columns in the Mapping Table
LastRow = wb.Sheets("LMal").Cells(Rows.Count, "A").End(xlUp).Row 'Check how many rows in the Mapping Table
For i = 2 To LastCol
    Filename = "C:\Users\tanu\Desktop\" & wb.Sheets("LMal").Cells(1, i) & ".xlsx" ' Get the Sheet name such as tanu, sweety, etc
    Set Wbook = Workbooks.Open(Filename)
        For x = 2 To LastRow ' loop through rows
        Search = wb.Sheets("LMal").Cells(x, i).Value
        On Error Resume Next
            For Each wSheet In Wbook.Worksheets
                Set strGotIt = wSheet.Cells.Find(What:=Search, After:=wSheet.Cells(1, 1), _
                           LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                           SearchDirection:=xlNext, MatchCase:=True)
                If strGotIt <> vbNullString Then
                   wSheet.Cells(strGotIt.Row, strGotIt.Column).Value = wb.Sheets("LMal").Cells(x, 1).Value 'replace the value in tanu's sheet
                   On Error GoTo 0
                End If

            Next
           On Error GoTo 0
        Next x

Wbook.Close SaveChanges:=True
Application.DisplayAlerts = True
Next i
End Sub
</code>```

Body:
How do I compare a mapping table  (values  in different cells) in excel and map the value of that header to my main database.

Main Database:

Mapping Table:

Tanu's Sheet:

It should map the headers(wgt, ht, bmi, etc)  of the file (tanu, sweety, Raju)  and compare it with main database and replace it with the headers of main database

 The code written so far
 Sub SelectColumn()
 Dim xColIndex As Integer
 Dim xRowIndex As Integer
   xIndex = Application.ActiveCell.Column
    xRowIndex = Application.ActiveSheet.Cells(Rows.Count,
     xIndex).End(xlUp).Row
     Range(Cells(2, xIndex), Cells(xRowIndex, xIndex)).Select
   End Sub

Can't get through

Body:
This code will check your mapping table and replace headers in each of their Sheets for each workbook tanu, sweety and etc, (it will look for the headers in the range A1:Z1000, change this if you need it to be a bigger range):

Sub foo3()
Dim Wbook As Workbook
Dim wSheet As Worksheet
Dim wb As ThisWorkbook
Set wb = ThisWorkbook
Application.DisplayAlerts = False
LastCol = wb.Sheets("LMal").Cells(1, Columns.Count).End(xlToLeft).Column 'Check how many columns in the Mapping Table
LastRow = wb.Sheets("LMal").Cells(Rows.Count, "A").End(xlUp).Row 'Check how many rows in the Mapping Table
For i = 2 To LastCol
    Filename = "C:\Users\tanu\Desktop\" & wb.Sheets("LMal").Cells(1, i) & ".xlsx" ' Get the Sheet name such as tanu, sweety, etc
    Set Wbook = Workbooks.Open(Filename)
        For x = 2 To LastRow ' loop through rows
        Search = wb.Sheets("LMal").Cells(x, i).Value
        On Error Resume Next
            For Each wSheet In Wbook.Worksheets
                Set strGotIt = wSheet.Cells.Find(What:=Search, After:=wSheet.Cells(1, 1), _
                           LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                           SearchDirection:=xlNext, MatchCase:=True)
                If strGotIt <> vbNullString Then
                   wSheet.Cells(strGotIt.Row, strGotIt.Column).Value = wb.Sheets("LMal").Cells(x, 1).Value 'replace the value in tanu's sheet
                   On Error GoTo 0
                End If

            Next
           On Error GoTo 0
        Next x

Wbook.Close SaveChanges:=True
Application.DisplayAlerts = True
Next i
End Sub


问:
Why is my code so slow?
Top-posted (sorry) answer, for those who don't have time to get into it but may have similar problems.

Rule #1, as always, move as much as you can out of loops.

2,  moving TField var := ADODataSet.FieldByname()  out of the loop
3, ADODataSet.DisableControls(); and ADODataSet.EnableControls(); around the loop
4, stringGrid.Rows[r].BeginUpdate() and EndUpdate() on each row (cannot do on teh whle control)
each of these shaved off a few seconds, but I got it down to "faster than the eye can see" by changing

```<code>loop
  stringGrid.RowCount := stringGrid.RowCount + 1;
end loop
</code>```

to putting stringGrid.RowCount := ADODataSet.RecordCount; before the loop

+1 and heartfelt thanks to all who helped.

(now I will go and see what I can do to optimize drawing a TChart, which is also slow ;-)

with about 3,600 rows in the table this takes 45 seconds to populate the string grid. What am I doing wrong?

```
   ADODataSet := TADODataSet.Create(Nil);
   ADODataSet.Connection := AdoConnection;

   ADODataSet.CommandText := 'SELECT * FROM measurements';
   ADODataSet.CommandType := cmdText;
   ADODataSet.Open();

   while not ADODataSet.eof do
   begin
      TestRunDataStringGrid.RowCount := TestRunDataStringGrid.RowCount + 1;

      measurementDateTime   := UnixToDateTime(ADODataSet.FieldByname('time_stamp').AsInteger);
      DoSQlCommandWithResultSet('SELECT * FROM start_time_stamp', AdoConnection, resultSet);
      startDateTime := UnixToDateTime(StrToInt64(resultSet.Strings[0]));
      elapsedTime   := measurementDateTime - startDateTime;
      TestRunDataStringGrid.Cells[0, Pred(TestRunDataStringGrid.RowCount)] := FormatDateTime('hh:mm:ss', elapsedTime);
      TestRunDataStringGrid.Cells[1, Pred(TestRunDataStringGrid.RowCount)] := FloatToStrWithPrecision(ADODataSet.FieldByname('inputTemperature').AsFloat);
      TestRunDataStringGrid.Cells[2, Pred(TestRunDataStringGrid.RowCount)] := FloatToStrWithPrecision(ADODataSet.FieldByname('outputTemperature').AsFloat);
      TestRunDataStringGrid.Cells[3, Pred(TestRunDataStringGrid.RowCount)] := FloatToStrWithPrecision(ADODataSet.FieldByname('flowRate').AsFloat);
      TestRunDataStringGrid.Cells[4, Pred(TestRunDataStringGrid.RowCount)] := FloatToStrWithPrecision(ADODataSet.FieldByname('waterPressure').AsFloat * convert);
      TestRunDataStringGrid.Cells[5, Pred(TestRunDataStringGrid.RowCount)] := FloatToStrWithPrecision(ADODataSet.FieldByname('waterLevel').AsFloat);
      TestRunDataStringGrid.Cells[6, Pred(TestRunDataStringGrid.RowCount)] := FloatToStrWithPrecision(ADODataSet.FieldByname('cod').AsFloat);
      ADODataSet.Next;
   end;

   ADODataSet.Close();
   ADODataSet.Free();
```

update:

```
Function  DoSQlCommandWithResultSet(const command : String; AdoConnection : TADOConnection; resultSet : TStringList): Boolean;
  var
        i : Integer;
        AdoQuery : TADOQuery;

begin
  Result := True;
  resultSet.Clear();

  AdoQuery := TADOQuery.Create(nil);
  try
    AdoQuery.Connection := AdoConnection;
    AdoQuery.SQL.Add(command);
    AdoQuery.Open();
    i := 0;
    while not  AdoQuery.eof do
    begin
      resultSet.Add(ADOQuery.Fields[i].Value);
      i := i + 1;
      AdoQuery.Next;
    end;

  finally
    AdoQuery.Close();
    AdoQuery.Free();
  end;
end;

```

答:
You are executing the command SELECT * FROM start_time_stamp 3,600 times, but it does not appear to me that it is correlated with your outer loop in any way.  Why not execute it once before the loop?

That SELECT command appears to return only a single column of a single record, yet you use "*" to load all columns, and no WHERE clause to limit the results to a single row (if there's more than one row in the table).

You use only a limited number of columns from Measurements, but you retrieve all columns with "*".

You don't show the contents of DoSQlCommandWithResultSet, so it's not clear if there's a problem in that routine.

It's not clear whether the problem is in your database access or the string grid.  Comment out all the lines pertaining to the string grid and run the program.  How long does the database access alone take?

ContentLicense: CC BY-SA 2.5

Title: Why is my code so slow?

Body:
Top-posted (sorry) answer, for those who don't have time to get into it but may have similar problems.

Rule #1, as always, move as much as you can out of loops.

2,  moving TField var := ADODataSet.FieldByname()  out of the loop
3, ADODataSet.DisableControls(); and ADODataSet.EnableControls(); around the loop
4, stringGrid.Rows[r].BeginUpdate() and EndUpdate() on each row (cannot do on teh whle control)
each of these shaved off a few seconds, but I got it down to "faster than the eye can see" by changing

loop
  stringGrid.RowCount := stringGrid.RowCount + 1;
end loop

to putting stringGrid.RowCount := ADODataSet.RecordCount; before the loop

+1 and heartfelt thanks to all who helped.

(now I will go and see what I can do to optimize drawing a TChart, which is also slow ;-)

with about 3,600 rows in the table this takes 45 seconds to populate the string grid. What am I doing wrong?

   ADODataSet := TADODataSet.Create(Nil);
   ADODataSet.Connection := AdoConnection;

   ADODataSet.CommandText := 'SELECT * FROM measurements';
   ADODataSet.CommandType := cmdText;
   ADODataSet.Open();

   while not ADODataSet.eof do
   begin
      TestRunDataStringGrid.RowCount := TestRunDataStringGrid.RowCount + 1;

      measurementDateTime   := UnixToDateTime(ADODataSet.FieldByname('time_stamp').AsInteger);
      DoSQlCommandWithResultSet('SELECT * FROM start_time_stamp', AdoConnection, resultSet);
      startDateTime := UnixToDateTime(StrToInt64(resultSet.Strings[0]));
      elapsedTime   := measurementDateTime - startDateTime;
      TestRunDataStringGrid.Cells[0, Pred(TestRunDataStringGrid.RowCount)] := FormatDateTime('hh:mm:ss', elapsedTime);
      TestRunDataStringGrid.Cells[1, Pred(TestRunDataStringGrid.RowCount)] := FloatToStrWithPrecision(ADODataSet.FieldByname('inputTemperature').AsFloat);
      TestRunDataStringGrid.Cells[2, Pred(TestRunDataStringGrid.RowCount)] := FloatToStrWithPrecision(ADODataSet.FieldByname('outputTemperature').AsFloat);
      TestRunDataStringGrid.Cells[3, Pred(TestRunDataStringGrid.RowCount)] := FloatToStrWithPrecision(ADODataSet.FieldByname('flowRate').AsFloat);
      TestRunDataStringGrid.Cells[4, Pred(TestRunDataStringGrid.RowCount)] := FloatToStrWithPrecision(ADODataSet.FieldByname('waterPressure').AsFloat * convert);
      TestRunDataStringGrid.Cells[5, Pred(TestRunDataStringGrid.RowCount)] := FloatToStrWithPrecision(ADODataSet.FieldByname('waterLevel').AsFloat);
      TestRunDataStringGrid.Cells[6, Pred(TestRunDataStringGrid.RowCount)] := FloatToStrWithPrecision(ADODataSet.FieldByname('cod').AsFloat);
      ADODataSet.Next;
   end;

   ADODataSet.Close();
   ADODataSet.Free();

update:

Function  DoSQlCommandWithResultSet(const command : String; AdoConnection : TADOConnection; resultSet : TStringList): Boolean;
  var
        i : Integer;
        AdoQuery : TADOQuery;

begin
  Result := True;
  resultSet.Clear();

  AdoQuery := TADOQuery.Create(nil);
  try
    AdoQuery.Connection := AdoConnection;
    AdoQuery.SQL.Add(command);
    AdoQuery.Open();
    i := 0;
    while not  AdoQuery.eof do
    begin
      resultSet.Add(ADOQuery.Fields[i].Value);
      i := i + 1;
      AdoQuery.Next;
    end;

  finally
    AdoQuery.Close();
    AdoQuery.Free();
  end;
end;


问:
Why doesn't this "is not" if statement work?
This is my code,

```<code>if diff != "1" or diff != "2" or diff != "3":
            print("You need to pick either 1, 2 or 3\n")
</code>```

For some reason, the outcome is,

```<code>Pick a difficulty:
1) Easy
2) Medium
3) Hard
>> 2
You need to pick either 1, 2 or 3
</code>```

I want the if statement to check if the variable diff is not equal to strings 1, 2 and 3. But when I put either 1, 2 or 3, the error message for when diff doesn't equal the numbers prints instead. Why does this happen?

答:
To apply the logic of "not any" you would want to check if it is any of the valid results then invert. (NOR)

```<code>if not (diff == "1" or diff == "2" or diff == "3"):
</code>```

Or applying DeMorgan's theorem this would be equivelent to "not equal to 1 AND not equal to 2 AND not equal to 3"

```<code>if diff != "1" and diff != "2" and diff != "3":
</code>```

of course python also has the in and not in operator which makes this much cleaner:

```<code>if diff not in ("1", "2", "3"):
</code>```

Body:
This is my code,

if diff != "1" or diff != "2" or diff != "3":
            print("You need to pick either 1, 2 or 3\n")

For some reason, the outcome is,

Pick a difficulty:
1) Easy
2) Medium
3) Hard
>> 2
You need to pick either 1, 2 or 3

I want the if statement to check if the variable diff is not equal to strings 1, 2 and 3. But when I put either 1, 2 or 3, the error message for when diff doesn't equal the numbers prints instead. Why does this happen?

Body:
To apply the logic of "not any" you would want to check if it is any of the valid results then invert. (NOR)

if not (diff == "1" or diff == "2" or diff == "3"):

Or applying DeMorgan's theorem this would be equivelent to "not equal to 1 AND not equal to 2 AND not equal to 3"

if diff != "1" and diff != "2" and diff != "3":

of course python also has the in and not in operator which makes this much cleaner:

if diff not in ("1", "2", "3"):


问:
AWS S3 Server side encryption Access denied error

I have A and B AWS accounts and I am syncing S3 bucket from A account SoruceS3Bucket to B account DestinationS3Bucket.

Following is the bucket policy which is applied on Destination bucket and it is allowing Source AWS account to sync the content with DestinationS3Bucket.

```{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PermissionsToAAccount",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::XXXXXXX:root"
            },
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::DestinationS3Bucket",
                "arn:aws:s3:::DestinationS3Bucket/*"
            ]
        }
    ]
}```

Here the sync was working perfectly since long time and it is still working but from last few days at the DestinationS3Bucket files are not accessible with the Server side encryption Access denied error.

I have verified there no encryption(Default encryption, none) on SourceS3Bucket and DestinationS3Bucket and I am using Source AWS account secret and access key to sync the content. Thanks in advance.

答:
When you copy files from one S3 bucket in account A using credentials of account A to a bucket in account B, the owner of the files in the destination bucket will be account A. (Account A is the principal that created the files in account B's bucket).

During the file copy from source to destination bucket, add the --acl bucket-owner-full-control option so that account B can control the files. Otherwise you might have files in account B's bucket that account B cannot access or control.

Another option is to use the credentials of account B to copy from the source to the destination bucket. This way the owner of the copied files is account B.

Body:
I have A and B AWS accounts and I am syncing S3 bucket from A account SoruceS3Bucket to B account DestinationS3Bucket.

Following is the bucket policy which is applied on Destination bucket and it is allowing Source AWS account to sync the content with DestinationS3Bucket.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PermissionsToAAccount",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::XXXXXXX:root"
            },
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::DestinationS3Bucket",
                "arn:aws:s3:::DestinationS3Bucket/*"
            ]
        }
    ]
}

Here the sync was working perfectly since long time and it is still working but from last few days at the DestinationS3Bucket files are not accessible with the Server side encryption Access denied error.

I have verified there no encryption(Default encryption, none) on SourceS3Bucket and DestinationS3Bucket and I am using Source AWS account secret and access key to sync the content. Thanks in advance.


问:
T-SQL hierarchy query
I have a table with hierarchical data:

This is a sample of data with id, parent id, name, code (which is sometimes not filled), level and isroot column. In real scenario there will be more levels than just 2 but now let's look at the simplified example.

What I need to do is to loop over all records and find rows where id is not filled at any level of the hierarchy:

Rows with id ranging should be returned from 6 till 10 because they do not have code filled in at any point of the hierarchy

Rows from 1 till 5 should not be returned because code was mentioned somewhere in the hierarchy.

How should one solve this problem with T-SQL?

The only solution that came to my mind was recursivity (cte or WHILE) but I what I was trying to implement was too complex and did not solve the problem.

答:
Query like this should work:

```<code>; with cte as
(
select id, parent_id,code,parent_id as RootId from tableT where IsRootLevel=1
UNION ALL
select T2.id,T2.parent_id,T2.code,T1.RootId as RootId from tableT T2 join
cte T1 on T1.id=T2.parent_id and IsRootLevel=0
)
,
cte2 as
(select id,MAX(case when code ='' then NULL else code end) over( partition by RootId) as code from cte)

select T1.* from tableT T1 left join cte2 T2
on T1.id=T2.id
where T2.code is NULL
</code>```

See working demo

Title: T-SQL hierarchy query

Body:
Query like this should work:

; with cte as
(
select id, parent_id,code,parent_id as RootId from tableT where IsRootLevel=1
UNION ALL
select T2.id,T2.parent_id,T2.code,T1.RootId as RootId from tableT T2 join
cte T1 on T1.id=T2.parent_id and IsRootLevel=0
)
,
cte2 as
(select id,MAX(case when code ='' then NULL else code end) over( partition by RootId) as code from cte)

select T1.* from tableT T1 left join cte2 T2
on T1.id=T2.id
where T2.code is NULL

See working demo


问:
Show only the first letters, three dots and the last N letter of a span
If I've a very long text like this (for example in a table):

```<code><p class='line'>
    <span class='fixed1'>FIXED</span>
    <span class='long_text'>This is my very very very very long text</span>
    <span class='fixed2'>FIXED</span>
</p>
</code>```

By default the
 width is 100% and if the text is longer than the screen it's showed on multi lines. Instead I want it in a single line where only the last N letter are showed.

Here some example:

Screen large enough:

```<code>FIXED This is my very very very very long text FIXED
</code>```

Screen not enough large, text overflow.

```<code>FIXED This is my ve... text FIXED
</code>```

I can obtain something similar using:

```<code>span.long_text {
    overflow: hidden;
    text-overflow: ellipsis;
}
</code>```

Is it possible to obtain the effect I described? How?

答:
Ellipsis only work with single line things so that's OK here. The problem is with those "N letter".

Here's a starter with :after (you must have your N last letters in some CSS. Suboptimal => https://codepen.io/PhilippeVay/pen/vWMePJ?editors=0100

.line {
  display: flex;
  border: 1px solid black;
  width: 240px;
}
.line > * {
  white-space: nowrap;
}
.long_text {
  position: relative;
  flex-grow: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  background: yellow;
  padding-right: 2rem;
}
.long_text::after {
  content: "g text";
  position: absolute;
  right: 0;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><p class='line'>
    <span class='fixed1'>FIXED</span>
    <span class='long_text'>This is my very very very very long text</span>
    <span class='fixed2'>FIXED</span>
</p></code>```

It could be content: attr(data-last-letters) and  which now works in most browsers.

Or background-image: -moz-element(#someId); that copy-paste an element elsewhere and that only works in Firefox (for the past 7 years: won't work cross-browser anytime soon I guess).

Body:
If I've a very long text like this (for example in a table):

    FIXED
    This is my very very very very long text
    FIXED

By default the
 width is 100% and if the text is longer than the screen it's showed on multi lines. Instead I want it in a single line where only the last N letter are showed.

Here some example:

Screen large enough:

FIXED This is my very very very very long text FIXED

Screen not enough large, text overflow.

FIXED This is my ve... text FIXED

I can obtain something similar using:

span.long_text {
    overflow: hidden;
    text-overflow: ellipsis;
}

Is it possible to obtain the effect I described? How?

Body:
Ellipsis only work with single line things so that's OK here. The problem is with those "N letter".

Here's a starter with :after (you must have your N last letters in some CSS. Suboptimal => https://codepen.io/PhilippeVay/pen/vWMePJ?editors=0100

.line {
  display: flex;
  border: 1px solid black;
  width: 240px;
}
.line > * {
  white-space: nowrap;
}
.long_text {
  position: relative;
  flex-grow: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  background: yellow;
  padding-right: 2rem;
}
.long_text::after {
  content: "g text";
  position: absolute;
  right: 0;
}

<p class='line'>
    <span class='fixed1'>FIXED</span>
    <span class='long_text'>This is my very very very very long text</span>
    <span class='fixed2'>FIXED</span>
</p>

It could be content: attr(data-last-letters) and <span class='long_text' data-last-letters="g text"> which now works in most browsers.

Or background-image: -moz-element(#someId); that copy-paste an element elsewhere and that only works in Firefox (for the past 7 years: won't work cross-browser anytime soon I guess).


问:
Ajax cannot display json data from php. What's wrong with json format?
I have read all the related questions that reference to this topic, but still cannot find answer here.
 So, php and ajax works great. The problem starts when i try to include json, between php and ajax, to passing data.

here is my ajax:

```<code>function likeButton(commentId, userId, sessionUserId) {

  // check if the comment belong to the session userId
  if(sessionUserId == userId) {
    alert("You cannot like your own comment.");
  }
  else if(sessionUserId != userId) {
    var like_upgrade = false;
    $.ajax({
      url: "requests.php",
      type: "POST",
      dataType: "json",
      data: {
        keyLike: "like",
        commentId: commentId,
        userId: userId,
        sessionUserId: sessionUserId,
        like_upgrade: like_upgrade
      },
      success: function(data) {
        var data = $.parseJSON(data);
        $("#comment_body td").find("#updRow #updComLike[data-id='" +commentId+ "']").html(data.gaming_comment_like);
        if(data.like_upgrade == true) {
        upgradeReputation(userId);
      }
      }
    });
  }
}
</code>```

Note, that i try not to include this:

```<code>var data = $.parseJSON(data);
</code>```

Also i tried with diferent variable like so:

```<code>var response = $.parseJSON(data);
</code>```

and also tried this format:

```<code>var data = jQuery.parseJSON(data);
</code>```

None of these worked.

here is requests.php file:

```<code>    if(isset($_POST['keyLike'])) {
    if($_POST['keyLike'] == "like") {
    $commentId = $_POST['commentId'];
    $userId = $_POST['userId'];
    $sessionUserId = $_POST['sessionUserId'];

    $sql_upgrade_like = "SELECT * FROM gaming_comments WHERE gaming_comment_id='$commentId'";
    $result_upgrade_like = mysqli_query($conn, $sql_upgrade_like);
    if($row_upgrade_like = mysqli_fetch_assoc($result_upgrade_like)) {
    $gaming_comment_like = $row_upgrade_like['gaming_comment_like'];
       }
    $gaming_comment_like = $gaming_comment_like + 1;

    $sql_update_like = "UPDATE gaming_comments SET gaming_comment_like='$gaming_comment_like' WHERE gaming_comment_id='$commentId'";
    $result_update_like = mysqli_query($conn, $sql_update_like);

    $sql_insert_like = "INSERT INTO gaming_comment_likes (gaming_comment_id, user_id, user_id_like) VALUES ('$commentId', '$userId', '$sessionUserId')";
    $result_insert_like = mysqli_query($conn, $sql_insert_like);
    $like_upgrade = true;
//json format
    $data = array("gaming_comment_like" => $gaming_comment_like,
     "like_upgrade" => $like_upgrade);
    echo json_encode($data);
    exit();
           }
        }
</code>```

Note: i also try to include this to the top of my php file:

```<code>header('Content-type: json/application');
</code>```

but still not worked.

What am i missing here?

答:
Don't call $.parseJSON. jQuery does that automatically when you specify dataType: 'json', so data contains the object already.

You should also learn to use parametrized queries instead of substituting variables into the SQL. Your code is vulnerable to SQL injection.

Body:
I have read all the related questions that reference to this topic, but still cannot find answer here.
 So, php and ajax works great. The problem starts when i try to include json, between php and ajax, to passing data.

here is my ajax:

function likeButton(commentId, userId, sessionUserId) {

  // check if the comment belong to the session userId
  if(sessionUserId == userId) {
    alert("You cannot like your own comment.");
  }
  else if(sessionUserId != userId) {
    var like_upgrade = false;
    $.ajax({
      url: "requests.php",
      type: "POST",
      dataType: "json",
      data: {
        keyLike: "like",
        commentId: commentId,
        userId: userId,
        sessionUserId: sessionUserId,
        like_upgrade: like_upgrade
      },
      success: function(data) {
        var data = $.parseJSON(data);
        $("#comment_body td").find("#updRow #updComLike[data-id='" +commentId+ "']").html(data.gaming_comment_like);
        if(data.like_upgrade == true) {
        upgradeReputation(userId);
      }
      }
    });
  }
}

Note, that i try not to include this:

var data = $.parseJSON(data);

Also i tried with diferent variable like so:

var response = $.parseJSON(data);

and also tried this format:

var data = jQuery.parseJSON(data);

None of these worked.

here is requests.php file:

    if(isset($_POST['keyLike'])) {
    if($_POST['keyLike'] == "like") {
    $commentId = $_POST['commentId'];
    $userId = $_POST['userId'];
    $sessionUserId = $_POST['sessionUserId'];

    $sql_upgrade_like = "SELECT * FROM gaming_comments WHERE gaming_comment_id='$commentId'";
    $result_upgrade_like = mysqli_query($conn, $sql_upgrade_like);
    if($row_upgrade_like = mysqli_fetch_assoc($result_upgrade_like)) {
    $gaming_comment_like = $row_upgrade_like['gaming_comment_like'];
       }
    $gaming_comment_like = $gaming_comment_like + 1;

    $sql_update_like = "UPDATE gaming_comments SET gaming_comment_like='$gaming_comment_like' WHERE gaming_comment_id='$commentId'";
    $result_update_like = mysqli_query($conn, $sql_update_like);

    $sql_insert_like = "INSERT INTO gaming_comment_likes (gaming_comment_id, user_id, user_id_like) VALUES ('$commentId', '$userId', '$sessionUserId')";
    $result_insert_like = mysqli_query($conn, $sql_insert_like);
    $like_upgrade = true;
//json format
    $data = array("gaming_comment_like" => $gaming_comment_like,
     "like_upgrade" => $like_upgrade);
    echo json_encode($data);
    exit();
           }
        }

Note: i also try to include this to the top of my php file:

header('Content-type: json/application');

but still not worked.

What am i missing here?


问:
How do you get month number from UTC time for a specific timezone with a DateTime object?
I have the end of the year (12/31/2017) for the EST timezone stored in UTC in a DateTime object.  When I get the Month property it's 1 because it's store in UTC and the end of the year in EST when converted UTC is 01/01/2018 04:59:59.

How can I convert this date into the correct timezone using the America/New_York timezone to get the correct month number for that timezone?

答:
I ended up using Noda to handle the conversion for me.</p>

```<code>using NodaTime;
//Get the correct timezone
DateTimeZone zone = DateTimeZoneProviders.Tzdb ["America/New_York"];
//Convert to the correct month
var month = Instant.FromDateTimeUtc(endOfYear.ToUniversalTime()).InZone(zone.Month);
Console.Write(endOfYear.Month); //1 Not expected value
Console.Write(month); // 12 expected value
</code>```

Body:
I ended up using Noda to handle the conversion for me.

using NodaTime;
//Get the correct timezone
DateTimeZone zone = DateTimeZoneProviders.Tzdb ["America/New_York"];
//Convert to the correct month
var month = Instant.FromDateTimeUtc(endOfYear.ToUniversalTime()).InZone(zone.Month);
Console.Write(endOfYear.Month); //1 Not expected value
Console.Write(month); // 12 expected value


问:
R glmnet as.matrix() error message
I'm trying to use the glmnet package on a dataset.  I'm using cv.glmnet() to get a lambda value for glmnet().  I'm excluding columns 1,2,7,12 as they are: id column, response column, contain NA's, and contain NA's.

Here's the dataset and error message:

```<code>> head(t2)
  X1 X2        X3 X4 X5         X6    X7 X8 X9 X10 X11 X12
1  1  1 0.7661266 45  2 0.80298213  9120 13  0   6   0   2
2  2  0 0.9571510 40  0 0.12187620  2600  4  0   0   0   1
3  3  0 0.6581801 38  1 0.08511338  3042  2  1   0   0   0
4  4  0 0.2338098 30  0 0.03604968  3300  5  0   0   0   0
5  5  0 0.9072394 49  1 0.02492570 63588  7  0   1   0   0
6  6  0 0.2131787 74  0 0.37560697  3500  3  0   1   0   1
> str(t2)
'data.frame':   150000 obs. of  12 variables:
 $ X1 : int  1 2 3 4 5 6 7 8 9 10 ...
 $ X2 : int  1 0 0 0 0 0 0 0 0 0 ...
 $ X3 : num  0.766 0.957 0.658 0.234 0.907 ...
 $ X4 : int  45 40 38 30 49 74 57 39 27 57 ...
 $ X5 : int  2 0 1 0 1 0 0 0 0 0 ...
 $ X6 : num  0.803 0.1219 0.0851 0.036 0.0249 ...
 $ X7 : int  9120 2600 3042 3300 63588 3500 NA 3500 NA 23684 ...
 $ X8 : int  13 4 2 5 7 3 8 8 2 9 ...
 $ X9 : int  0 0 1 0 0 0 0 0 0 0 ...
 $ X10: int  6 0 0 0 1 1 3 0 0 4 ...
 $ X11: int  0 0 0 0 0 0 0 0 0 0 ...
 $ X12: int  2 1 0 0 0 1 0 0 NA 2 ...

> cv1 <- cv.glmnet(as.matrix(t2[,-c(1,2,7,12)]), t2[,2], family="binomial")
Error in as.matrix(cbind2(1, newx) %*% nbeta) :
  error in evaluating the argument 'x' in selecting a method for function 'as.matrix': Error in t(.Call(Csparse_dense_crossprod, y, t(x))) :
  error in evaluating the argument 'x' in selecting a method for function 't': Error: invalid class 'NA' to dup_mMatrix_as_dgeMatrix
> cv1 <- cv.glmnet(as.matrix(t2[,-c(1,2,7,12)]), t2[,2], family="multinomial")
Error in t(.Call(Csparse_dense_crossprod, y, t(x))) :
  error in evaluating the argument 'x' in selecting a method for function 't': Error: invalid class 'NA' to dup_mMatrix_as_dgeMatrix
</code>```

Any suggestions?

答:
I had a similar error message when using cv.glmnet. cv.glmnet would function correctly in RStudio, but it would fail to work when using Rscript. The fix for me was to add to the top of my script:

require(methods)

It seems that the "glmnet" package may be using some functions from the "methods" package, but this package is not loaded at startup when using Rscript. However, the "methods" package is normally loaded by default in R.

Here is information about this Rscript functionality:

Rscript does not load methods package, R does -- why, and what are the consequences?

I hope this helps someone that runs into the same issue that I did.

Title: R glmnet as.matrix() error message

Body:
I'm trying to use the glmnet package on a dataset.  I'm using cv.glmnet() to get a lambda value for glmnet().  I'm excluding columns 1,2,7,12 as they are: id column, response column, contain NA's, and contain NA's.

Here's the dataset and error message:

> head(t2)
  X1 X2        X3 X4 X5         X6    X7 X8 X9 X10 X11 X12
1  1  1 0.7661266 45  2 0.80298213  9120 13  0   6   0   2
2  2  0 0.9571510 40  0 0.12187620  2600  4  0   0   0   1
3  3  0 0.6581801 38  1 0.08511338  3042  2  1   0   0   0
4  4  0 0.2338098 30  0 0.03604968  3300  5  0   0   0   0
5  5  0 0.9072394 49  1 0.02492570 63588  7  0   1   0   0
6  6  0 0.2131787 74  0 0.37560697  3500  3  0   1   0   1
> str(t2)
'data.frame':   150000 obs. of  12 variables:
 $ X1 : int  1 2 3 4 5 6 7 8 9 10 ...
 $ X2 : int  1 0 0 0 0 0 0 0 0 0 ...
 $ X3 : num  0.766 0.957 0.658 0.234 0.907 ...
 $ X4 : int  45 40 38 30 49 74 57 39 27 57 ...
 $ X5 : int  2 0 1 0 1 0 0 0 0 0 ...
 $ X6 : num  0.803 0.1219 0.0851 0.036 0.0249 ...
 $ X7 : int  9120 2600 3042 3300 63588 3500 NA 3500 NA 23684 ...
 $ X8 : int  13 4 2 5 7 3 8 8 2 9 ...
 $ X9 : int  0 0 1 0 0 0 0 0 0 0 ...
 $ X10: int  6 0 0 0 1 1 3 0 0 4 ...
 $ X11: int  0 0 0 0 0 0 0 0 0 0 ...
 $ X12: int  2 1 0 0 0 1 0 0 NA 2 ...

> cv1 <- cv.glmnet(as.matrix(t2[,-c(1,2,7,12)]), t2[,2], family="binomial")
Error in as.matrix(cbind2(1, newx) %*% nbeta) :
  error in evaluating the argument 'x' in selecting a method for function 'as.matrix': Error in t(.Call(Csparse_dense_crossprod, y, t(x))) :
  error in evaluating the argument 'x' in selecting a method for function 't': Error: invalid class 'NA' to dup_mMatrix_as_dgeMatrix
> cv1 <- cv.glmnet(as.matrix(t2[,-c(1,2,7,12)]), t2[,2], family="multinomial")
Error in t(.Call(Csparse_dense_crossprod, y, t(x))) :
  error in evaluating the argument 'x' in selecting a method for function 't': Error: invalid class 'NA' to dup_mMatrix_as_dgeMatrix

Any suggestions?


问:
Selection of columns for the table in Shiny
I would like to add a new category at the beginning which will select the columns for the table. I can not combine variables with other elements in an application. Could someone explain to me what I'm doing wrong? As you can see on the graphics program does not work well.

My code:

```<code> library(shiny)

data <- data.frame(
  Category1 = rep(letters[1:3], each = 15),
  Info = paste("Text info", 1:45),
  Category2 = sample(letters[15:20], 45, replace = T),
  Size = sample(1:100, 45),
  MoreStuff = paste("More Stuff", 1:45)
)
ui <- fluidPage(titlePanel("Test Explorer"),
                sidebarLayout(
                  sidebarPanel(
                    selectizeInput(
                      "show_vars",
                      "Columns to show:",
                      choices = colnames(data),
                      multiple = TRUE,
                      selected = c("Category1", "Info", "Category2")
                    ),
                    uiOutput("category1"),
                    uiOutput("category2"),
                    uiOutput("sizeslider")
                  ),
                  mainPanel(tableOutput("table"))
                ))

server <- function(input, output, session) {
  data2 <- reactive({
    req(input$table)
    if (input$table == "All") {
      return(data)
    }
    data[, names(data) %in% input$show_vars]
  })

  output$category1 <- renderUI({
    selectizeInput('cat1',
                   'Choose Cat 1',
                   choices = c("All", sort(as.character(
                     unique(data$Category1)
                   ))),
                   selected = "All")
  })

  df_subset <- eventReactive(input$cat1, {
    if (input$cat1 == "All") {
      df_subset <- data
    }
    else{
      df_subset <- data[data$Category1 == input$cat1, ]
    }
  })

  df_subset1 <- reactive({
    if (is.null(input$cat2)) {
      df_subset()
    } else {
      df_subset()[df_subset()$Category2 %in% input$cat2, ]
    }
  })

  output$category2 <- renderUI({
    selectizeInput(
      'cat2',
      'Choose Cat 2 (optional):',
      choices = sort(as.character(unique(
        df_subset()$Category2
      ))),
      multiple = TRUE,
      options = NULL
    )
  })

  output$sizeslider <- renderUI({
    sliderInput(
      "size",
      label = "Size Range",
      min = min(df_subset1()$Size),
      max = max(df_subset1()$Size),
      value = c(min(df_subset1()$Size), max(df_subset1()$Size))
    )
  })

  df_subset2 <- reactive({
    if (is.null(input$size)) {
      df_subset1()
    } else {
      df_subset1()[df_subset1()$Size >= input$size[1] &
                     df_subset1()$Size <= input$size[2], ]
    }
  })

  output$table <- renderTable({
    df_subset2()
  })
}

shinyApp(ui, server)
</code>```

答:
You have few problems with your code

You stored that reactive value of the columns selection in data2(), and displaying table df_subset2(). As with your code, the columns change when you add columns and select Cat1 dropdown, since its values are dependent on the data.react.

Avoid using generic names like data to store data. Sometimes it interfere with R base names

You need to use ObserveEvent and eventReactive, when you expect the change on UI to reflect

Below is what I fixed, you can change accordingly.

Added a submit button

Wrapped the input selections code into an ObserveEvent
By this, your data is displayed only when you click the submit button.

Here is the code.

```<code>library(shiny)

data.input <- data.frame(
  Category1 = rep(letters[1:3], each = 15),
  Info = paste("Text info", 1:45),
  Category2 = sample(letters[15:20], 45, replace = T),
  Size = sample(1:100, 45),
  MoreStuff = paste("More Stuff", 1:45)
)
</code>```

ui.r

```<code>ui <- fluidPage(titlePanel("Test Explorer"),
                sidebarLayout(
                  sidebarPanel(
                    selectizeInput(
                      "show_vars",
                      "Columns to show:",
                      choices = colnames(data.input),
                      multiple = TRUE,
                      selected = c("Category1", "Info", "Category2")
                    ),
                    actionButton("button", "An action button"),
                    uiOutput("category1"),
                    uiOutput("category2"),
                    uiOutput("sizeslider")
                  ),
                  mainPanel(tableOutput("table"))
                ))
</code>```

server.r

```<code>server <- function(input, output, session) {
  data.react <- eventReactive(input$button, {
    data.input[, input$show_vars]
  })
  observeEvent(input$button, {
    output$category1 <- renderUI({
      data.sel <- data.react()
      selectizeInput('cat1',
                     'Choose Cat 1',
                     choices = c("All", sort(as.character(
                       unique(data.sel$Category1)
                     ))),
                     selected = "All")
    })

    df_subset <- eventReactive(input$cat1, {
      data.sel <- data.react()
      if (input$cat1 == "All") {
        data.sel
      }
      else{
        data.sel[data.sel$Category1 == input$cat1,]
      }
    })

    output$category2 <- renderUI({
      selectizeInput(
        'cat2',
        'Choose Cat 2 (optional):',
        choices = sort(as.character(unique(
          df_subset()$Category2
        ))),
        multiple = TRUE,
        options = NULL
      )
    })

    df_subset1 <- reactive({
      if (is.null(input$cat2)) {
        df_subset()
      } else {
        df_subset()[df_subset()$Category2 %in% input$cat2,]
      }
    })

    output$sizeslider <- renderUI({
      sliderInput(
        "size",
        label = "Size Range",
        min = min(data.input$Size),
        max = max(data.input$Size),
        value = c(min(data.input$Size), max(data.input$Size))
      )
    })

    df_subset2 <- reactive({
      if (is.null(input$size)) {
        df_subset1()
      } else {
        df_subset1()[data.input$Size >= input$size[1] &
                       data.input$Size <= input$size[2],]
      }
    })
    output$table <- renderTable({
      df_subset2()

    })
  })
}

shinyApp(ui, server)
</code>```

Body:
I would like to add a new category at the beginning which will select the columns for the table. I can not combine variables with other elements in an application. Could someone explain to me what I'm doing wrong? As you can see on the graphics program does not work well.

My code:

 library(shiny)

data <- data.frame(
  Category1 = rep(letters[1:3], each = 15),
  Info = paste("Text info", 1:45),
  Category2 = sample(letters[15:20], 45, replace = T),
  Size = sample(1:100, 45),
  MoreStuff = paste("More Stuff", 1:45)
)
ui <- fluidPage(titlePanel("Test Explorer"),
                sidebarLayout(
                  sidebarPanel(
                    selectizeInput(
                      "show_vars",
                      "Columns to show:",
                      choices = colnames(data),
                      multiple = TRUE,
                      selected = c("Category1", "Info", "Category2")
                    ),
                    uiOutput("category1"),
                    uiOutput("category2"),
                    uiOutput("sizeslider")
                  ),
                  mainPanel(tableOutput("table"))
                ))

server <- function(input, output, session) {
  data2 <- reactive({
    req(input$table)
    if (input$table == "All") {
      return(data)
    }
    data[, names(data) %in% input$show_vars]
  })

  output$category1 <- renderUI({
    selectizeInput('cat1',
                   'Choose Cat 1',
                   choices = c("All", sort(as.character(
                     unique(data$Category1)
                   ))),
                   selected = "All")
  })

  df_subset <- eventReactive(input$cat1, {
    if (input$cat1 == "All") {
      df_subset <- data
    }
    else{
      df_subset <- data[data$Category1 == input$cat1, ]
    }
  })

  df_subset1 <- reactive({
    if (is.null(input$cat2)) {
      df_subset()
    } else {
      df_subset()[df_subset()$Category2 %in% input$cat2, ]
    }
  })

  output$category2 <- renderUI({
    selectizeInput(
      'cat2',
      'Choose Cat 2 (optional):',
      choices = sort(as.character(unique(
        df_subset()$Category2
      ))),
      multiple = TRUE,
      options = NULL
    )
  })

  output$sizeslider <- renderUI({
    sliderInput(
      "size",
      label = "Size Range",
      min = min(df_subset1()$Size),
      max = max(df_subset1()$Size),
      value = c(min(df_subset1()$Size), max(df_subset1()$Size))
    )
  })

  df_subset2 <- reactive({
    if (is.null(input$size)) {
      df_subset1()
    } else {
      df_subset1()[df_subset1()$Size >= input$size[1] &
                     df_subset1()$Size <= input$size[2], ]
    }
  })

  output$table <- renderTable({
    df_subset2()
  })
}

shinyApp(ui, server)

Body:
You have few problems with your code

You stored that reactive value of the columns selection in data2(), and displaying table df_subset2(). As with your code, the columns change when you add columns and select Cat1 dropdown, since its values are dependent on the data.react.

Avoid using generic names like data to store data. Sometimes it interfere with R base names

You need to use ObserveEvent and eventReactive, when you expect the change on UI to reflect

Below is what I fixed, you can change accordingly.

Added a submit button

Wrapped the input selections code into an ObserveEvent
By this, your data is displayed only when you click the submit button.

Here is the code.

library(shiny)

data.input <- data.frame(
  Category1 = rep(letters[1:3], each = 15),
  Info = paste("Text info", 1:45),
  Category2 = sample(letters[15:20], 45, replace = T),
  Size = sample(1:100, 45),
  MoreStuff = paste("More Stuff", 1:45)
)

ui.r

ui <- fluidPage(titlePanel("Test Explorer"),
                sidebarLayout(
                  sidebarPanel(
                    selectizeInput(
                      "show_vars",
                      "Columns to show:",
                      choices = colnames(data.input),
                      multiple = TRUE,
                      selected = c("Category1", "Info", "Category2")
                    ),
                    actionButton("button", "An action button"),
                    uiOutput("category1"),
                    uiOutput("category2"),
                    uiOutput("sizeslider")
                  ),
                  mainPanel(tableOutput("table"))
                ))

server.r

server <- function(input, output, session) {
  data.react <- eventReactive(input$button, {
    data.input[, input$show_vars]
  })
  observeEvent(input$button, {
    output$category1 <- renderUI({
      data.sel <- data.react()
      selectizeInput('cat1',
                     'Choose Cat 1',
                     choices = c("All", sort(as.character(
                       unique(data.sel$Category1)
                     ))),
                     selected = "All")
    })

    df_subset <- eventReactive(input$cat1, {
      data.sel <- data.react()
      if (input$cat1 == "All") {
        data.sel
      }
      else{
        data.sel[data.sel$Category1 == input$cat1,]
      }
    })

    output$category2 <- renderUI({
      selectizeInput(
        'cat2',
        'Choose Cat 2 (optional):',
        choices = sort(as.character(unique(
          df_subset()$Category2
        ))),
        multiple = TRUE,
        options = NULL
      )
    })

    df_subset1 <- reactive({
      if (is.null(input$cat2)) {
        df_subset()
      } else {
        df_subset()[df_subset()$Category2 %in% input$cat2,]
      }
    })

    output$sizeslider <- renderUI({
      sliderInput(
        "size",
        label = "Size Range",
        min = min(data.input$Size),
        max = max(data.input$Size),
        value = c(min(data.input$Size), max(data.input$Size))
      )
    })

    df_subset2 <- reactive({
      if (is.null(input$size)) {
        df_subset1()
      } else {
        df_subset1()[data.input$Size >= input$size[1] &
                       data.input$Size <= input$size[2],]
      }
    })
    output$table <- renderTable({
      df_subset2()

    })
  })
}

shinyApp(ui, server)