问:
Caused by: java.sql.SQLException: Out of range value for column 'age' : value age
I keep getting SQLException but I suspect that it is not the problem. Table is :

```<code>create table person (first varchar(30) DEFAULT NULL, last varchar(30)      DEFAULT NULL, gender char(1) DEFAULT NULL, age int(4) DEFAULT NULL);
</code>```

Insert statements:

```<code>insert into person values('Barack','Obama','M',54);
insert into person values('Hillary','Clinton','f',34);
</code>```

Spark code:

```<code>public void test3() {
    // SparkConf conf = new SparkConf().setAppName("Stackoverflow").setMaster("local[4]");
    // JavaSparkContext sc = new JavaSparkContext(conf);
    // SQLContext sqlContext = new SQLContext(sc);
    SparkSession spark =
    SparkSession.builder().appName("JavaALS").master("local[1]").getOrCreate();
    SQLContext context = spark.sqlContext();
    Map<String, String> options = new HashMap<>();
    options.put("url", "jdbc:mariadb://localhost:3306/test");
    options.put("user", "root");
    options.put("password", "lyhao1999");
    options.put("dbtable", "(select * from person where gender = 'M') as someone");

    Dataset<Row> jdbcDF = context.read().format("jdbc").options(options).load();
    jdbcDF.show();
}
</code>```

Error:

```<code>Caused by: java.sql.SQLException: Out of range value for column 'age' : value age
at org.mariadb.jdbc.internal.com.read.resultset.rowprotocol.TextRowProtocol.getInternalLong(TextRowProtocol.java:348)
at org.mariadb.jdbc.internal.com.read.resultset.rowprotocol.TextRowProtocol.getInternalInt(TextRowProtocol.java:254)
at org.mariadb.jdbc.internal.com.read.resultset.SelectResultSet.getInt(SelectResultSet.java:985)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.$anonfun$makeGetter$7(JdbcUtils.scala:431)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.$anonfun$makeGetter$7$adapted(JdbcUtils.scala:430)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anon$1.getNext(JdbcUtils.scala:367)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anon$1.getNext(JdbcUtils.scala:349)
at org.apache.spark.util.NextIterator.hasNext(NextIterator.scala:73)
at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
at org.apache.spark.util.CompletionIterator.hasNext(CompletionIterator.scala:31)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIteratorForCodegenStage1.processNext(Unknown Source)
at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
at org.apache.spark.sql.execution.WholeStageCodegenExec$$anon$1.hasNext(WholeStageCodegenExec.scala:759)
at org.apache.spark.sql.execution.SparkPlan.$anonfun$getByteArrayRdd$1(SparkPlan.scala:349)
at org.apache.spark.rdd.RDD.$anonfun$mapPartitionsInternal$2(RDD.scala:898)
at org.apache.spark.rdd.RDD.$anonfun$mapPartitionsInternal$2$adapted(RDD.scala:898)
at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:373)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:337)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
at org.apache.spark.scheduler.Task.run(Task.scala:131)
at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$3(Executor.scala:506)
at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1462)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:509)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
</code>```

I saw it during debugging：

```<code>java.lang.NumberFormatException: For input string: "age"
</code>```

This is a code example I found on the web, and if it is an empty table, it will not be reported as an error; I tried to change the field property to varchar, but the result of the query became the field name.

first
last
gender
age

first
last
gender
age

答:
I resolve it by change the url from mariadb to mysql，
although i did use the mariadb.</p>
```<code>    options.put("url", "jdbc:mysql://localhost:3306/spark");
</code>```

Body:
I keep getting SQLException but I suspect that it is not the problem. Table is :

create table person (first varchar(30) DEFAULT NULL, last varchar(30)      DEFAULT NULL, gender char(1) DEFAULT NULL, age int(4) DEFAULT NULL);

Insert statements:

insert into person values('Barack','Obama','M',54);
insert into person values('Hillary','Clinton','f',34);

Spark code:

public void test3() {
    // SparkConf conf = new SparkConf().setAppName("Stackoverflow").setMaster("local[4]");
    // JavaSparkContext sc = new JavaSparkContext(conf);
    // SQLContext sqlContext = new SQLContext(sc);
    SparkSession spark =
    SparkSession.builder().appName("JavaALS").master("local[1]").getOrCreate();
    SQLContext context = spark.sqlContext();
    Map<String, String> options = new HashMap<>();
    options.put("url", "jdbc:mariadb://localhost:3306/test");
    options.put("user", "root");
    options.put("password", "lyhao1999");
    options.put("dbtable", "(select * from person where gender = 'M') as someone");

    Dataset<Row> jdbcDF = context.read().format("jdbc").options(options).load();
    jdbcDF.show();
}

Error:

Caused by: java.sql.SQLException: Out of range value for column 'age' : value age
at org.mariadb.jdbc.internal.com.read.resultset.rowprotocol.TextRowProtocol.getInternalLong(TextRowProtocol.java:348)
at org.mariadb.jdbc.internal.com.read.resultset.rowprotocol.TextRowProtocol.getInternalInt(TextRowProtocol.java:254)
at org.mariadb.jdbc.internal.com.read.resultset.SelectResultSet.getInt(SelectResultSet.java:985)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.$anonfun$makeGetter$7(JdbcUtils.scala:431)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.$anonfun$makeGetter$7$adapted(JdbcUtils.scala:430)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anon$1.getNext(JdbcUtils.scala:367)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anon$1.getNext(JdbcUtils.scala:349)
at org.apache.spark.util.NextIterator.hasNext(NextIterator.scala:73)
at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
at org.apache.spark.util.CompletionIterator.hasNext(CompletionIterator.scala:31)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIteratorForCodegenStage1.processNext(Unknown Source)
at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
at org.apache.spark.sql.execution.WholeStageCodegenExec$$anon$1.hasNext(WholeStageCodegenExec.scala:759)
at org.apache.spark.sql.execution.SparkPlan.$anonfun$getByteArrayRdd$1(SparkPlan.scala:349)
at org.apache.spark.rdd.RDD.$anonfun$mapPartitionsInternal$2(RDD.scala:898)
at org.apache.spark.rdd.RDD.$anonfun$mapPartitionsInternal$2$adapted(RDD.scala:898)
at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:373)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:337)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
at org.apache.spark.scheduler.Task.run(Task.scala:131)
at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$3(Executor.scala:506)
at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1462)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:509)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

I saw it during debugging：

java.lang.NumberFormatException: For input string: "age"

This is a code example I found on the web, and if it is an empty table, it will not be reported as an error; I tried to change the field property to varchar, but the result of the query became the field name.

first
last
gender
age

first
last
gender
age

Body:
I resolve it by change the url from mariadb to mysql，
although i did use the mariadb.

    options.put("url", "jdbc:mysql://localhost:3306/spark");


来源: stackexchange

ContentLicense: CC BY-SA 4.0

Title: I don't know where is the problem: three different data with only one result appear

Body:
Sorry, I tried your answer but there is a small problem with my code.

I used data, data2, data3 and it dosen't work perfectly.

I think there is sth wrong with my part code below:

output$mydata <- renderUI({            ## table

    global$data3
    global$data2
    global$data
  })

My whole code with data, data2, and data3 here:

options(encoding = "UTF-8")
library(shiny)
library(shinythemes)
library(psych)              ## corr.test
library(DT)
library(pheatmap)
########

data<-structure(list(Name = c("A", "A", "A", "B", "B", "B", "C", "C",
                              "C", "D"), `0610005C13Rik` = c(0.42, 0.28, 0.16, 0.14, 0.23,
                                                             0.12, 0, 0.06, 0.09, 0.27), `0610006L08Rik` = c(0, 0, 0, 0, 0,
                                                                                0, 0, 0, 0, 0), `0610007P14Rik` = c(12.81, 11.44, 13.94, 14.26,
                                                                                14.95, 14.55, 6.61, 8.52, 7.68, 5.13), `0610009B22Rik` = c(7.53,
                                                                                6.55, 7.32, 7.12, 6.33, 7.12, 5.87, 3.57, 3.95, 3.49), `0610009E02Rik` = c(0.19,
                                                                                0.25, 0.23, 0.18, 0.28, 0.3, 0.26, 0.14, 0.11, 0.11), `0610009L18Rik` = c(1.32,
                                                                                1.61, 1.26, 0.78, 1.12, 0.95, 3.45, 1.36, 0.94, 1.31), `0610009O20Rik` = c(18.73,
                                                                                17.38, 18.56, 21.46, 22.64, 21.24, 20.85, 21.85, 17.9, 23.44),
                     `0610010B08Rik` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `0610010F05Rik` = c(5.14,
                                                                                4.49, 4.68, 4.63, 4.5, 4.15, 1.74, 2.3, 2.03, 2.28), `0610010K14Rik` = c(34.97,
                                                                                28.72, 32.6, 29.98, 29.93, 30.05, 38.07, 29.76, 28.63, 27.74
                                                                                )), row.names = c(NA, 10L), class = "data.frame")
data
data2<-structure(list(Name = c("A", "A", "A", "B", "B", "B", "C", "C",
                              "C", "D"), `0610005C13Rik` = c(0.4, 0.2, 0.1, 0.1, 0.2,
                                                             0.1, 0, 0.01, 0.04, 0.2), `0610006L08Rik` = c(0, 0, 0, 0, 0,
                                                                                0, 0, 0, 0, 0), `0610007P14Rik` = c(12.81, 11.44, 13.94, 14.26,
                                                                                14.95, 14.55, 6.61, 8.52, 7.68, 5.13), `0610009B22Rik` = c(7.53,
                                                                                6.55, 7.32, 7.12, 6.33, 7.12, 5.87, 3.57, 3.95, 3.49), `0610009E02Rik` = c(0.19,
                                                                                0.25, 0.23, 0.18, 0.28, 0.3, 0.26, 0.14, 0.11, 0.11), `0610009L18Rik` = c(1.32,
                                                                                1.61, 1.26, 0.78, 1.12, 0.95, 3.45, 1.36, 0.94, 1.31), `0610009O20Rik` = c(18.73,
                                                                                17.38, 18.56, 21.46, 22.64, 21.24, 20.85, 21.85, 17.9, 23.44),
                     `0610010B08Rik` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `0610010F05Rik` = c(5.14,
                                                                                4.49, 4.68, 4.63, 4.5, 4.15, 1.74, 2.3, 2.03, 2.28), `0610010K14Rik` = c(34.97,
                                                                                28.72, 32.6, 29.98, 29.93, 30.05, 38.07, 29.76, 28.63, 27.74
                                                                                )), row.names = c(NA, 10L), class = "data.frame")
data3<-structure(list(Name = c("A", "A", "A", "B", "B", "B", "C", "C",
                              "C", "D"), `0610005C13Rik` = c(0.2, 0.8, 0.6, 0.4, 0.3,
                                                             0.2, 0, 0.03, 0.06, 0.3), `0610006L08Rik` = c(0, 0, 0, 0, 0,
                                                                                0, 0, 0, 0, 0), `0610007P14Rik` = c(12.81, 11.44, 13.94, 14.26,
                                                                                14.95, 14.55, 6.61, 8.52, 7.68, 5.13), `0610009B22Rik` = c(7.53,
                                                                                6.55, 7.32, 7.12, 6.33, 7.12, 5.87, 3.57, 3.95, 3.49), `0610009E02Rik` = c(0.19,
                                                                                0.25, 0.23, 0.18, 0.28, 0.3, 0.26, 0.14, 0.11, 0.11), `0610009L18Rik` = c(1.32,
                                                                                1.61, 1.26, 0.78, 1.12, 0.95, 3.45, 1.36, 0.94, 1.31), `0610009O20Rik` = c(18.73,
                                                                                17.38, 18.56, 21.46, 22.64, 21.24, 20.85, 21.85, 17.9, 23.44),
                     `0610010B08Rik` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `0610010F05Rik` = c(5.14,
                                                                                4.49, 4.68, 4.63, 4.5, 4.15, 1.74, 2.3, 2.03, 2.28), `0610010K14Rik` = c(34.97,
                                                                                28.72, 32.6, 29.98, 29.93, 30.05, 38.07, 29.76, 28.63, 27.74
                                                                                )), row.names = c(NA, 10L), class = "data.frame")
######

ui <- fluidPage(
    mainPanel("666",
#      width = 9,
                  tabPanel("123",
                           hr(),
                           typeaheadInput(
                             "select",
                             "Select",
                             choices = NULL,
                             minLength = 1,
                             value = "0610005C13Rik"
                           ),
                           #####
                           actionButton(inputId = "plot_1", label = "123",width=80,class="btn btn-success"),
                           actionButton(inputId = "plot_2", label = "456",width=80,class="btn btn-light"),
                           actionButton(inputId = "plot_3", label = "789",width=80,class="btn btn-danger"),
                           hr(),
                           uiOutput("all"),
                           uiOutput("mydata"),
                           # dataTableOutput('myTable1'),
                           # dataTableOutput('myTable2'),
                           # dataTableOutput('myTable3')
                           #####
                  )
      ))
server <- function(input, output, session) {

  updateTypeaheadInput(session, "select", choices = colnames(data[,-1]))
  observeEvent(list(input$plot_1,input$plot_2,input$plot_3), {
    updateTabsetPanel(session, "666",
                      selected = "123"
    )
  })

  #################
  global <- reactiveValues(out = NULL,
                           data = NULL

  )

  ######
  output$all <- renderUI({               ##  picture
    global$out

  })

  output$mydata <- renderUI({            ## table

    global$data3
    global$data2
    global$data
  })

  #####
  observeEvent(input$plot_1,{
    global$out <- plotOutput("myPlot_1")
    global$data <- DTOutput("myTable_1")
#    myData_2(NULL)
#    myData_3(NULL)
  })

  observeEvent(input$plot_2, {

    global$out <- plotOutput("myPlot_2")
    global$data2 <- DTOutput("myTable_2")
    # myData_1(NULL)
    # myData_3(NULL)
  })

  observeEvent(input$plot_3, {
    global$out <- plotOutput("myPlot_3")
    global$data3 <- DTOutput("myTable_3")
    # myData_1(NULL)
    # myData_2(NULL)
  })

  #########################################
  ##
  myPlot_1 = reactiveVal()
  myPlot_2 = reactiveVal()
  myPlot_3 = reactiveVal()
  #
  myData_1 = reactiveVal()
  myData_2 = reactiveVal()
  myData_3 = reactiveVal()

  ####################################  plot_1 plot_2
  observeEvent(input$plot_1, {
    req(input$select !="")
    validate(need(sum(unique(colnames(data[,-1])) %in% input$select)>0, "The gene is not found."))
    data_cor<-data[,-1]
    tm <- corr.test(data_cor[,input$select,drop=FALSE],
                    y = data_cor, use = "pairwise", "spearman", adjust="none",
                    alpha=0.05, ci=F, minlength=5)
    res <-setNames(as.data.frame(t(do.call(rbind, tm[c("r", "p")]))), c("Correlation", "P_value"))
    res<-res[-which(rownames(res)== input$select),]
    res<-data.frame(Gene=rownames(res),res)
    res<-res[order(res$Correlation,decreasing = T),]
    rownames(res)<-NULL
    res<-na.omit(res)
    res
    ##############
    data_correlation=t(data[, -1])
    data_subset=data_correlation[c(input$select, as.vector(head(res$Gene, 9))), ]
    myPlot_1(

      if(nrow(data_subset)>1){
        pheatmap(log2(data_subset+1), show_colnames = F,fontsize_row =13,
                 #                 labels_row = as.expression(lapply(rownames(data_subset), function(a) bquote(italic(.(a))))),
                 cluster_rows = F, cluster_cols = F,
                 fontsize = 11,
                 cellwidth=4
        )
      }
    )
    myData_1(res)
  })
  ######################################## 2
  observeEvent(input$plot_2, {
    req(input$select !="")
    validate(need(sum(unique(colnames(data2[,-1])) %in% input$select)>0, "The gene is not found."))
    data_cor<-data2[,-1]
    tm <- corr.test(data_cor[,input$select,drop=FALSE],
                    y = data_cor, use = "pairwise", "spearman", adjust="none",
                    alpha=0.05, ci=F, minlength=5)
    res <-setNames(as.data.frame(t(do.call(rbind, tm[c("r", "p")]))), c("Correlation", "P_value"))
    res<-res[-which(rownames(res)== input$select),]
    res<-data.frame(Gene=rownames(res),res)
    res<-res[order(res$Correlation,decreasing = T),]
    rownames(res)<-NULL
    res<-na.omit(res)
    res
    ##############
    data_correlation=t(data2[, -1])
    data_subset=data_correlation[c(input$select, as.vector(head(res$Gene, 9))), ]
    myPlot_2(

      if(nrow(data_subset)>1){
        pheatmap(log2(data_subset+1), show_colnames = F,fontsize_row =13,
                 #                 labels_row = as.expression(lapply(rownames(data_subset), function(a) bquote(italic(.(a))))),
                 cluster_rows = F, cluster_cols = F,
                 fontsize = 11,
                 cellwidth=4
        )
      }
    )
    myData_2(res)
  })
  ########################################  3
  observeEvent(input$plot_3, {
    req(input$select !="")
    validate(need(sum(unique(colnames(data3[,-1])) %in% input$select)>0, "The gene is not found."))
    data_cor<-data3[,-1]
    tm <- corr.test(data_cor[,input$select,drop=FALSE],
                    y = data_cor, use = "pairwise", "spearman", adjust="none",
                    alpha=0.05, ci=F, minlength=5)
    res <-setNames(as.data.frame(t(do.call(rbind, tm[c("r", "p")]))), c("Correlation", "P_value"))
    res<-res[-which(rownames(res)== input$select),]
    res<-data.frame(Gene=rownames(res),res)
    res<-res[order(res$Correlation,decreasing = T),]
    rownames(res)<-NULL
    res<-na.omit(res)
    res
    ##############
    data_correlation=t(data3[, -1])
    data_subset=data_correlation[c(input$select, as.vector(head(res$Gene, 9))), ]
    myPlot_3(

      if(nrow(data_subset)>1){
        pheatmap(log2(data_subset+1), show_colnames = F,fontsize_row =13,
                 #                 labels_row = as.expression(lapply(rownames(data_subset), function(a) bquote(italic(.(a))))),
                 cluster_rows = F, cluster_cols = F,
                 fontsize = 11,
                 cellwidth=4
        )
      }
    )
    myData_3(res)
  })
  ###################################  1
  output$myPlot_1 = renderPlot({
    req(myPlot_1())
    myPlot_1()
  })

  output$myTable_1 = renderDataTable({
    req(myData_1())

    myData_1()

  })
  #################################  2
  output$myPlot_2 = renderPlot({

    req(myPlot_2())
    myPlot_2()
  })

  output$myTable_2 = renderDataTable({

    req(myData_2())

    myData_2()

  })
  #################################  3
  output$myPlot_3 = renderPlot({

    req(myPlot_3())
    myPlot_3()
  })

  output$myTable_3 = renderDataTable({

    req(myData_3())
    myData_3()

  })

}

###################

# Create Shiny app ----
shinyApp(ui = ui, server = server)

Body:
I'm glad that I find a new way to solve my problem. I'm so excited!!

Jut according to your answer

Here is my answer:

options(encoding = "UTF-8")
library(shiny)
library(shinythemes)
library(psych)              ## corr.test
library(DT)
library(pheatmap)
########

data1<-structure(list(Name = c("A", "A", "A", "B", "B", "B", "C", "C",
                              "C", "D"), `0610005C13Rik` = c(0.42, 0.28, 0.16, 0.14, 0.23,
                                                             0.12, 0, 0.06, 0.09, 0.27), `0610006L08Rik` = c(0, 0, 0, 0, 0,
                                                                                0, 0, 0, 0, 0), `0610007P14Rik` = c(12.81, 11.44, 13.94, 14.26,
                                                                                14.95, 14.55, 6.61, 8.52, 7.68, 5.13), `0610009B22Rik` = c(7.53,
                                                                                6.55, 7.32, 7.12, 6.33, 7.12, 5.87, 3.57, 3.95, 3.49), `0610009E02Rik` = c(0.19,
                                                                                0.25, 0.23, 0.18, 0.28, 0.3, 0.26, 0.14, 0.11, 0.11), `0610009L18Rik` = c(1.32,
                                                                                1.61, 1.26, 0.78, 1.12, 0.95, 3.45, 1.36, 0.94, 1.31), `0610009O20Rik` = c(18.73,
                                                                                17.38, 18.56, 21.46, 22.64, 21.24, 20.85, 21.85, 17.9, 23.44),
                     `0610010B08Rik` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `0610010F05Rik` = c(5.14,
                                                                                4.49, 4.68, 4.63, 4.5, 4.15, 1.74, 2.3, 2.03, 2.28), `0610010K14Rik` = c(34.97,
                                                                                28.72, 32.6, 29.98, 29.93, 30.05, 38.07, 29.76, 28.63, 27.74
                                                                                )), row.names = c(NA, 10L), class = "data.frame")
data
data2<-structure(list(Name = c("A", "A", "A", "B", "B", "B", "C", "C",
                              "C", "D"), `0610005C13Rik` = c(0.4, 0.2, 0.1, 0.1, 0.2,
                                                             0.1, 0, 0.01, 0.04, 0.2), `0610006L08Rik` = c(0, 0, 0, 0, 0,
                                                                                0, 0, 0, 0, 0), `0610007P14Rik` = c(12.81, 11.44, 13.94, 14.26,
                                                                                14.95, 14.55, 6.61, 8.52, 7.68, 5.13), `0610009B22Rik` = c(7.53,
                                                                                6.55, 7.32, 7.12, 6.33, 7.12, 5.87, 3.57, 3.95, 3.49), `0610009E02Rik` = c(0.19,
                                                                                0.25, 0.23, 0.18, 0.28, 0.3, 0.26, 0.14, 0.11, 0.11), `0610009L18Rik` = c(1.32,
                                                                                1.61, 1.26, 0.78, 1.12, 0.95, 3.45, 1.36, 0.94, 1.31), `0610009O20Rik` = c(18.73,
                                                                                17.38, 18.56, 21.46, 22.64, 21.24, 20.85, 21.85, 17.9, 23.44),
                     `0610010B08Rik` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `0610010F05Rik` = c(5.14,
                                                                                4.49, 4.68, 4.63, 4.5, 4.15, 1.74, 2.3, 2.03, 2.28), `0610010K14Rik` = c(34.97,
                                                                                28.72, 32.6, 29.98, 29.93, 30.05, 38.07, 29.76, 28.63, 27.74
                                                                                )), row.names = c(NA, 10L), class = "data.frame")
data3<-structure(list(Name = c("A", "A", "A", "B", "B", "B", "C", "C",
                              "C", "D"), `0610005C13Rik` = c(0.2, 0.8, 0.6, 0.4, 0.3,
                                                             0.2, 0, 0.03, 0.06, 0.3), `0610006L08Rik` = c(0, 0, 0, 0, 0,
                                                                                0, 0, 0, 0, 0), `0610007P14Rik` = c(12.81, 11.44, 13.94, 14.26,
                                                                                14.95, 14.55, 6.61, 8.52, 7.68, 5.13), `0610009B22Rik` = c(7.53,
                                                                                6.55, 7.32, 7.12, 6.33, 7.12, 5.87, 3.57, 3.95, 3.49), `0610009E02Rik` = c(0.19,
                                                                                0.25, 0.23, 0.18, 0.28, 0.3, 0.26, 0.14, 0.11, 0.11), `0610009L18Rik` = c(1.32,
                                                                                1.61, 1.26, 0.78, 1.12, 0.95, 3.45, 1.36, 0.94, 1.31), `0610009O20Rik` = c(18.73,
                                                                                17.38, 18.56, 21.46, 22.64, 21.24, 20.85, 21.85, 17.9, 23.44),
                     `0610010B08Rik` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `0610010F05Rik` = c(5.14,
                                                                                4.49, 4.68, 4.63, 4.5, 4.15, 1.74, 2.3, 2.03, 2.28), `0610010K14Rik` = c(34.97,
                                                                                28.72, 32.6, 29.98, 29.93, 30.05, 38.07, 29.76, 28.63, 27.74
                                                                                )), row.names = c(NA, 10L), class = "data.frame")
######

ui <- fluidPage(
    mainPanel("666",
#      width = 9,
                  tabPanel("123",
                           hr(),
                           typeaheadInput(
                             "select",
                             "Select",
                             choices = NULL,
                             minLength = 1,
                             value = "0610005C13Rik"
                           ),
                           #####
                           actionButton(inputId = "plot_1", label = "123",width=80,class="btn btn-success"),
                           actionButton(inputId = "plot_2", label = "456",width=80,class="btn btn-light"),
                           actionButton(inputId = "plot_3", label = "789",width=80,class="btn btn-danger"),
                           hr(),
                           uiOutput("all"),
                           uiOutput("mydata"),
                           # dataTableOutput('myTable1'),
                           # dataTableOutput('myTable2'),
                           # dataTableOutput('myTable3')
                           #####
                  )
      ))
server <- function(input, output, session) {

  updateTypeaheadInput(session, "select", choices = colnames(data[,-1]))
  observeEvent(list(input$plot_1,input$plot_2,input$plot_3), {
    updateTabsetPanel(session, "666",
                      selected = "123"
    )
  })

  #################
  global <- reactiveValues(out = NULL,
                           data = NULL

  )

  ######
  output$all <- renderUI({               ##  picture
    global$out

  })

  output$mydata <- renderUI({            ## table

    global$data3
    global$data2
    global$data
  })

  #####

  observeEvent(input$plot_1,{
    data<-data1
    global$out <- plotOutput("myPlot_1")
    global$data <- DTOutput("myTable_1")
#    myData_2(NULL)
#    myData_3(NULL)
  })

  observeEvent(input$plot_2, {
    data<-data2
    global$out <- plotOutput("myPlot_2")
    global$data <- DTOutput("myTable_2")
    # myData_1(NULL)
    # myData_3(NULL)
  })

  observeEvent(input$plot_3, {
    data<-data3
    global$out <- plotOutput("myPlot_3")
    global$data <- DTOutput("myTable_3")
    # myData_1(NULL)
    # myData_2(NULL)
  })

  #########################################
  ##
  myPlot_1 = reactiveVal()
  myPlot_2 = reactiveVal()
  myPlot_3 = reactiveVal()
  #
  myData_1 = reactiveVal()
  myData_2 = reactiveVal()
  myData_3 = reactiveVal()

  ####################################  plot_1 plot_2
  observeEvent(input$plot_1, {
    req(input$select !="")
    validate(need(sum(unique(colnames(data[,-1])) %in% input$select)>0, "The gene is not found."))
    data_cor<-data[,-1]
    tm <- corr.test(data_cor[,input$select,drop=FALSE],
                    y = data_cor, use = "pairwise", "spearman", adjust="none",
                    alpha=0.05, ci=F, minlength=5)
    res <-setNames(as.data.frame(t(do.call(rbind, tm[c("r", "p")]))), c("Correlation", "P_value"))
    res<-res[-which(rownames(res)== input$select),]
    res<-data.frame(Gene=rownames(res),res)
    res<-res[order(res$Correlation,decreasing = T),]
    rownames(res)<-NULL
    res<-na.omit(res)
    res
    ##############
    data_correlation=t(data[, -1])
    data_subset=data_correlation[c(input$select, as.vector(head(res$Gene, 9))), ]
    myPlot_1(

      if(nrow(data_subset)>1){
        pheatmap(log2(data_subset+1), show_colnames = F,fontsize_row =13,
                 #                 labels_row = as.expression(lapply(rownames(data_subset), function(a) bquote(italic(.(a))))),
                 cluster_rows = F, cluster_cols = F,
                 fontsize = 11,
                 cellwidth=4
        )
      }
    )
    myData_1(res)
  })
  ######################################## 2
  observeEvent(input$plot_2, {
    req(input$select !="")
    validate(need(sum(unique(colnames(data2[,-1])) %in% input$select)>0, "The gene is not found."))
    data_cor<-data2[,-1]
    tm <- corr.test(data_cor[,input$select,drop=FALSE],
                    y = data_cor, use = "pairwise", "spearman", adjust="none",
                    alpha=0.05, ci=F, minlength=5)
    res <-setNames(as.data.frame(t(do.call(rbind, tm[c("r", "p")]))), c("Correlation", "P_value"))
    res<-res[-which(rownames(res)== input$select),]
    res<-data.frame(Gene=rownames(res),res)
    res<-res[order(res$Correlation,decreasing = T),]
    rownames(res)<-NULL
    res<-na.omit(res)
    res
    ##############
    data_correlation=t(data2[, -1])
    data_subset=data_correlation[c(input$select, as.vector(head(res$Gene, 9))), ]
    myPlot_2(

      if(nrow(data_subset)>1){
        pheatmap(log2(data_subset+1), show_colnames = F,fontsize_row =13,
                 #                 labels_row = as.expression(lapply(rownames(data_subset), function(a) bquote(italic(.(a))))),
                 cluster_rows = F, cluster_cols = F,
                 fontsize = 11,
                 cellwidth=4
        )
      }
    )
    myData_2(res)
  })
  ########################################  3
  observeEvent(input$plot_3, {
    req(input$select !="")
    validate(need(sum(unique(colnames(data3[,-1])) %in% input$select)>0, "The gene is not found."))
    data_cor<-data3[,-1]
    tm <- corr.test(data_cor[,input$select,drop=FALSE],
                    y = data_cor, use = "pairwise", "spearman", adjust="none",
                    alpha=0.05, ci=F, minlength=5)
    res <-setNames(as.data.frame(t(do.call(rbind, tm[c("r", "p")]))), c("Correlation", "P_value"))
    res<-res[-which(rownames(res)== input$select),]
    res<-data.frame(Gene=rownames(res),res)
    res<-res[order(res$Correlation,decreasing = T),]
    rownames(res)<-NULL
    res<-na.omit(res)
    res
    ##############
    data_correlation=t(data3[, -1])
    data_subset=data_correlation[c(input$select, as.vector(head(res$Gene, 9))), ]
    myPlot_3(

      if(nrow(data_subset)>1){
        pheatmap(log2(data_subset+1), show_colnames = F,fontsize_row =13,
                 #                 labels_row = as.expression(lapply(rownames(data_subset), function(a) bquote(italic(.(a))))),
                 cluster_rows = F, cluster_cols = F,
                 fontsize = 11,
                 cellwidth=4
        )
      }
    )
    myData_3(res)
  })
  ###################################  1
  output$myPlot_1 = renderPlot({
    req(myPlot_1())
    myPlot_1()
  })

  output$myTable_1 = renderDataTable({
    req(myData_1())

    myData_1()

  })
  #################################  2
  output$myPlot_2 = renderPlot({

    req(myPlot_2())
    myPlot_2()
  })

  output$myTable_2 = renderDataTable({

    req(myData_2())

    myData_2()

  })
  #################################  3
  output$myPlot_3 = renderPlot({

    req(myPlot_3())
    myPlot_3()
  })

  output$myTable_3 = renderDataTable({

    req(myData_3())
    myData_3()

  })

}

###################

# Create Shiny app ----
shinyApp(ui = ui, server = server)

I still use "data" as the public node for the three different data,

but I add the code below into the observeEvent:

data<-data1
data<-data2
data<-data3

And it works perfectly.

Vary thankful!!


问:
Cannot show razor view in browser (ASP.NET Core MVC)
I am really new to this ASP.NET Core MVC stuff I have this problem: browser is showing "Page not found 404" error.

Here is my code:

Model class (inside Models folder):

```<code>    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    using Microsoft.AspNetCore.Http;
    using System.ComponentModel;

    namespace CasaSelectaAppWeb.Models
    {
        public class HabitatClass
        {
            [Key]
            [StringLength(100)]
            public string CodeHab { get; set; }
            [Required]
            [StringLength(30)]
            public string DesignationHab { get; set; }
            [Required]
            public int Superficie { get; set; }
            [Required]
            [Range (1,40)]
            public int NbrPieces { get; set; }

            [Required]
            [DisplayName("Prix Initial")]
            [RegularExpression("([1-9][0-9]*)", ErrorMessage = "Veuillez entrer une valeur correcte !")]
            public int prixInit { get; set; }

            [Required]
            [DisplayName("Prix négotcié")]
            [RegularExpression("([1-9][0-9]*)", ErrorMessage = "Veuillez entrer une valeur correcte !")]
            public int prixNego { get; set; }

            [Required]
            [StringLength(20)]
            public string LocalHab { get; set; }
            [Required]
            [StringLength(200)]
            public string  AdressHab { get; set; }
            [Required]
            [StringLength(400)]

            public string DescriptionHab { get; set; }
            [MaxLength]
            //public byte[] ImageUrl { get; set; }
            //[Required(ErrorMessage ="Veuillez ajouter une image !")]
            //[Display(Name ="Image")]
            //[NotMapped]
            //public IFormFile ImageHab { get; set; }
            //CleEtrngVente

            [Display(Name ="Type d'habitat")]
            [StringLength(30)]
            public virtual string TypeHpk { get; set; }
            [ForeignKey("TypeHpk")]
            public virtual TypeHabitat TypesHab { get; set; }

        }
    }
</code>```

Html markup:

<!DOCTYPE html>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - CasaSelectaAppWeb</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />

                CasaSelectaAppWeb
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">

                </button>

                            Home

                            Privacy

                            Liste des habitats

            @RenderBody()

            © 2022 - CasaSelectaAppWeb - Privacy

    @await RenderSectionAsync("Scripts", required: false)

```
</div>
</div>
</p>
<p>Controller</p>
```using CasaSelectaAppWeb.Data;
using CasaSelectaAppWeb.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CasaSelectaAppWeb.Controllers
{
    public class HabitatsController : Controller
    {
        private readonly DbContClass _context;
        public HabitatsController(DbContClass context)
        {
            _context = context;
        }
        public IActionResult Index()
        {
            List<HabitatClass> habitats;
            habitats = _context.Habitats.ToList();
            return View(habitats);
        }
    }
}
```

答:
1.check you have configured routing in startup.cs:

```<code>app.UseRouting();

            ...
            app.UseEndpoints(endpoints =>
            {

                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");

            });
</code>```

2.Check if your routing exists.For example,routing of Index in your code is

```<code>https://localhost:xxx/Habitats/Index
</code>```

3.Check if your corresponding view is in the correct location.For example the view of Index should in Views/Habitats,and the name should be Index.cshtml.

Body:
I am really new to this ASP.NET Core MVC stuff I have this problem: browser is showing "Page not found 404" error.

Here is my code:

Model class (inside Models folder):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    using Microsoft.AspNetCore.Http;
    using System.ComponentModel;

    namespace CasaSelectaAppWeb.Models
    {
        public class HabitatClass
        {
            [Key]
            [StringLength(100)]
            public string CodeHab { get; set; }
            [Required]
            [StringLength(30)]
            public string DesignationHab { get; set; }
            [Required]
            public int Superficie { get; set; }
            [Required]
            [Range (1,40)]
            public int NbrPieces { get; set; }

            [Required]
            [DisplayName("Prix Initial")]
            [RegularExpression("([1-9][0-9]*)", ErrorMessage = "Veuillez entrer une valeur correcte !")]
            public int prixInit { get; set; }

            [Required]
            [DisplayName("Prix négotcié")]
            [RegularExpression("([1-9][0-9]*)", ErrorMessage = "Veuillez entrer une valeur correcte !")]
            public int prixNego { get; set; }

            [Required]
            [StringLength(20)]
            public string LocalHab { get; set; }
            [Required]
            [StringLength(200)]
            public string  AdressHab { get; set; }
            [Required]
            [StringLength(400)]

            public string DescriptionHab { get; set; }
            [MaxLength]
            //public byte[] ImageUrl { get; set; }
            //[Required(ErrorMessage ="Veuillez ajouter une image !")]
            //[Display(Name ="Image")]
            //[NotMapped]
            //public IFormFile ImageHab { get; set; }
            //CleEtrngVente

            [Display(Name ="Type d'habitat")]
            [StringLength(30)]
            public virtual string TypeHpk { get; set; }
            [ForeignKey("TypeHpk")]
            public virtual TypeHabitat TypesHab { get; set; }

        }
    }

Html markup:

<!DOCTYPE html>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - CasaSelectaAppWeb</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />

                CasaSelectaAppWeb
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">

                </button>

                            Home

                            Privacy

                            Liste des habitats

            @RenderBody()

            © 2022 - CasaSelectaAppWeb - Privacy

    @await RenderSectionAsync("Scripts", required: false)

Controller

using CasaSelectaAppWeb.Data;
using CasaSelectaAppWeb.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CasaSelectaAppWeb.Controllers
{
    public class HabitatsController : Controller
    {
        private readonly DbContClass _context;
        public HabitatsController(DbContClass context)
        {
            _context = context;
        }
        public IActionResult Index()
        {
            List<HabitatClass> habitats;
            habitats = _context.Habitats.ToList();
            return View(habitats);
        }
    }
}

Body:
1.check you have configured routing in startup.cs:

app.UseRouting();

            ...
            app.UseEndpoints(endpoints =>
            {

                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");

            });

2.Check if your routing exists.For example,routing of Index in your code is

https://localhost:xxx/Habitats/Index

3.Check if your corresponding view is in the correct location.For example the view of Index should in Views/Habitats,and the name should be Index.cshtml.


问:
Call block method and wait for user to finish then get respond back
I am using the following block method to call a Photo picker but i want to wait for the user input and then get a response...how is this possible?

I call the method using the code below

```<code>[self callmethod:[PHPhotoLibrary sharedPhotoLibrary] withCompletion:^(BOOL success, NSError *error, id responce) {
                                       if(success)
                                       {completion(prevStatus == PHAuthorizationStatusAuthorized);
                                           NSLog(@"==%@",responce); // here i get response once user selected photos
                                       }
                                   }];
</code>```

The method i call using the previous code

```<code>-(void)callmethod:(PHPhotoLibrary*)sharedPhotoLibrary withCompletion:(void(^)(BOOL success, NSError* error, id responce))completion
{
    if (@available(iOS 14, *)) {
        [sharedPhotoLibrary presentLimitedLibraryPickerFromViewController:self];
    }

    if (completion){
        dispatch_async(dispatch_get_main_queue(), ^{
            completion(YES,nil,sharedPhotoLibrary); // here that call when method complete
        });
    }
}
</code>```

答:
In iOS 15, there is a rendition that takes a completion handler. But in my tests, there are scenarios where it is not called (i.e., no changes) or is called multiple times (!). IMHO, that violates the “completion handler” informal contract. Besides, that is iOS 15, only. So, I would not be inclined to use the completion handler pattern at all.

As suggested in WWDC 2020 video, Handle the Limited Photos Library in your app, you can use PHPhotoLibraryChangeObserver. For example, if a view controller:

@interface ViewController () <PHPhotoLibraryChangeObserver>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];

    ...
}

- (void)dealloc {
    [[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
}

- (void)photoLibraryDidChange:(nonnull PHChange *)changeInstance {
    ...
}

@end
```

Body:
I am using the following block method to call a Photo picker but i want to wait for the user input and then get a response...how is this possible?

I call the method using the code below

[self callmethod:[PHPhotoLibrary sharedPhotoLibrary] withCompletion:^(BOOL success, NSError *error, id responce) {
                                       if(success)
                                       {completion(prevStatus == PHAuthorizationStatusAuthorized);
                                           NSLog(@"==%@",responce); // here i get response once user selected photos
                                       }
                                   }];

The method i call using the previous code

-(void)callmethod:(PHPhotoLibrary*)sharedPhotoLibrary withCompletion:(void(^)(BOOL success, NSError* error, id responce))completion
{
    if (@available(iOS 14, *)) {
        [sharedPhotoLibrary presentLimitedLibraryPickerFromViewController:self];
    }

    if (completion){
        dispatch_async(dispatch_get_main_queue(), ^{
            completion(YES,nil,sharedPhotoLibrary); // here that call when method complete
        });
    }
}


问:
Android Studio Bumblebee - device manager not opening
I just updated Android Studio to:
Android Studio Bumblebee | 2021.1.1
Build #AI-211.7628.21.2111.8092744, built on January 19, 2022

But now I cannot open the device manager anymore.

If I click on the icon, nothing happens.

答:
If you are using project for Flutter, Please follow below:

File -> Project Structure -> Project Settings -> Modules -> Add -> Android -> Ok


问:
Create and download html file without saving it in filesystem with Node/Express
I have an express app that generates an HTML file in the filesystem and then downloads it.
https://node-page-generator.herokuapp.com/

Is there any way to create and download a file without relying on the filesystem to save and then retrieve it?

The app uses fs.writefile():

```<code>import fs from 'fs';

export const callFileSystem = (path: string, content:string) => {
  fs.writeFile(
    `${path}`,
    `${content}`,
    function (err: any) {
      if (err) throw err;
      console.log('file created');
    }
  )
}
</code>```

A POST HTTP request on /generate send all page properties to the generatePage function that creates an index.html file

```<code>import { pageProperties } from './pageProps';
import { callFileSystem } from '../../helpers/fileSystem';

export const generatePage = (props: pageProperties) => {
  const path = 'index.html';

  callFileSystem(
    path,
    `
  <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <link rel="icon" href="${props.logoUrl}" type="image">
            <title>${props.title}</title>
            <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
        </head>
        <body>
            <style>
                :root {
                    --navbar-color: ${props.navbar.backgroundColor};
                    --background-color: ${props.pageBackgroundColor};
                    --font-color: ${props.fontColor};
                }
                html, body, h1, h2 {
                    margin: 0;
                }
                h1, h2, h3, h4, h5, p, span {
                    text-align: center;
                    color: var(--font-color, title, logoUrl, cardContent);
                }
                html {
                    margin: 0;
                    padding: 0;
                }
                body {
                    margin: 0;
                    padding: 0;
                    background-color: var(--background-color, title, logoUrl, cardContent);
                }
                .navbar {
                    background-color: var(--navbar-color, title, logoUrl, cardContent) !important;
                }
            </style>

    <nav class="navbar navbar-light navbar-bg">
        <div class="container">
          <a class="navbar-brand" href="#">
            <img src="${props.logoUrl}" alt="" width="30" height="24">
            <h1>${props.title}</h1>
          </a>
        </div>
      </nav>
      <div class="main container d-flex justify-content-center ">

      </div>
        </body>
        </html>
  `
  );
}
</code>```

Then a GET request at /download downloads the generated file:

```<code>export const downloadPage = (response: Resp) => {
  const file = 'index.html';
  response.download(file);
}
</code>```

I tried calling fs.writefile() as the path argument for res.download(), but it didn't work.

While the app kind of works as intended, the problem with this method is that if two /generate requests are made roughly simultaneously, the first user may call /download too late and get the response intended for the second user.

答:
You can achieve this by

streaming your HTML content and

using the Content-disposition header to indicate that you want the file to be downloaded

```<code>const stream = require("stream");
const express = require("express");
const app = express();
const port = 3000;

const htmlString = `
<!DOCTYPE html>
<html lang="en">
   <head>
      <title>My Title</title>
   </head>
   <body>My HTML Content</body>
</html>`;

app.get("/", (req, res) => {
  const readStream = new stream.PassThrough();
  readStream.end(htmlString);

  res.set("Content-disposition", "attachment; filename=index.html");
  readStream.pipe(res);
});

app.listen(port, () => {
  console.log(`App listening on port ${port}`);
});
</code>```

Body:
I have an express app that generates an HTML file in the filesystem and then downloads it.
https://node-page-generator.herokuapp.com/

Is there any way to create and download a file without relying on the filesystem to save and then retrieve it?

The app uses fs.writefile():

import fs from 'fs';

export const callFileSystem = (path: string, content:string) => {
  fs.writeFile(
    `${path}`,
    `${content}`,
    function (err: any) {
      if (err) throw err;
      console.log('file created');
    }
  )
}

A POST HTTP request on /generate send all page properties to the generatePage function that creates an index.html file

import { pageProperties } from './pageProps';
import { callFileSystem } from '../../helpers/fileSystem';

export const generatePage = (props: pageProperties) => {
  const path = 'index.html';

  callFileSystem(
    path,
    `
  <!DOCTYPE html>

            <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <link rel="icon" href="${props.logoUrl}" type="image">
            <title>${props.title}</title>
            <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

${props.title}

  `
  );
}

Then a GET request at /download downloads the generated file:

export const downloadPage = (response: Resp) => {
  const file = 'index.html';
  response.download(file);
}

I tried calling fs.writefile() as the path argument for res.download(), but it didn't work.

While the app kind of works as intended, the problem with this method is that if two /generate requests are made roughly simultaneously, the first user may call /download too late and get the response intended for the second user.

Body:
You can achieve this by

streaming your HTML content and

using the Content-disposition header to indicate that you want the file to be downloaded

const stream = require("stream");
const express = require("express");
const app = express();
const port = 3000;

const htmlString = `
<!DOCTYPE html>
<html lang="en">
   <head>
      <title>My Title</title>
   </head>
   <body>My HTML Content</body>
</html>`;

app.get("/", (req, res) => {
  const readStream = new stream.PassThrough();
  readStream.end(htmlString);

  res.set("Content-disposition", "attachment; filename=index.html");
  readStream.pipe(res);
});

app.listen(port, () => {
  console.log(`App listening on port ${port}`);
});


问:
Problem to correctly obtain more solds and visited items in MySQL
I'm having trouble getting the total of most sold and total most viewed products correctly.

Case best sellers:

```<code>   SELECT p.product_id, p.title, p.description, p.specifications, p.price, p.discount, p.discount_type, p.color, p.hours, p.product_type, p.gender, p.img, p.vdo_intro, p.status, p.url, p.date_start, p.date_end, p.ip, p.product_page, p.product_code, p.brand,   p.color_url,
          c.category_id, c.category_url as urlcategory, c.parent,
          SUM(o.order_quantity) as solds
     FROM order_detail o
     JOIN product p ON o.product_id = p.product_id
     JOIN category_relations rcat ON rcat.product_id = p.product_id
LEFT JOIN `category` c on rcat.category_id = c.category_id
  WHERE p.gender = ? AND p.status=1 AND p.date_end IS NULL
 GROUP BY p.product_id
 ORDER BY SUM(o.order_quantity)
     DESC LIMIT 12;
</code>```

In Data Base i have:

```<code>id_order    order_quantity  order_price order_discount  order_discount_type product_id  order_date
    1           2               59.00        0               %                 1        2022-01-31 22:49:24
    2           5               59.00        0               %                20        2022-01-31 22:49:24
    3          12               59.00        0               %                 8        2022-01-31 22:49:24
    4           5               59.00        0               %                19        2022-01-31 22:49:24
    5          25               59.00        0               %                17        2022-01-31 22:49:24
    6           3               59.00        0               %                 1        2022-01-31 22:49:24
</code>```

Result i get :

```<code>id:1    2 + 3 = 5 solds    i get = 20 solds (wrong).
id:20   5 solds            i get = 15 solds (wrong).
id:8   12 solds            i get = 36 solds (wrong).
id:19   5 solds            i get = 5 solds (ok).
id:17  25 solds            i get = 50 solds (wrong).
</code>```

Does anyone know what I'm doing wrong?

It look like that it is multiplying the results by records.

Case most visits:

```<code>    SELECT p.product_id, p.title, p.description, p.specifications, p.price, p.discount, p.discount_type, p.color, p.hours, p.product_type, p.gender, p.img, p.vdo_intro, p.status, p.url, p.date_start, p.date_end, p.ip, p.product_page, p.product_code, p.brand, p.color_url,
           c.category_id, c.category_url as urlcategory, c.parent,
           SUM(v.total) as visits
      FROM `visits` v
      JOIN `product` p ON v.product_id = p.product_id
      JOIN category_relations rcat ON rcat.product_id = p.product_id
 LEFT JOIN `category` c on rcat.category_id = c.category_id
     WHERE p.gender = ? AND p.status=1 AND p.date_end IS NULL
  GROUP BY p.product_id
  ORDER BY SUM(v.total)
      DESC LIMIT 12
</code>```

My database:

```<code>id_visits   total      ip              today        product_id
23           5     xxxxxxxxxxxxx1   2022-02-09        1
36           1     xxxxxxxxxxxxx4   2022-02-06       13
40           1     xxxxxxxxxxxxx3   2022-02-06       13
41           1     xxxxxxxxxxxxx1   2022-02-06       21
48           1     xxxxxxxxxxxxx2   2022-02-07       13
50           1     xxxxxxxxxxxxx2   2022-02-07        1
62           1     xxxxxxxxxxxxx8   2022-02-08        1
</code>```

Result i get:

```<code>id1 :   5 + 1 + 1 = 7 visits      i get 28 visits (wrong)
id13:   1 + 1 + 1 = 3 visits      i get 9 visits (wrong)
id21:   1         = 1 visits      i get 3 visits (wrong)
</code>```

Does anyone know the error, because it does not add up correctly?

note: I can add the tables if needed

Edit:

DB Fiddle

If i do:

GROUP BY rcat.cat_relation_id

I get the calculation right, but it repeats the same products

答:
Your problem is that category_relations is a many to many table.  So when you join it you get additional results for each category that a product is in.  So product id1 is in 4 categories so you get a sum of 28 instead of 7.

Your query is producing a row for each order for each category that the product ordered is in and then grouping all rows with the same product_id together.  SUM will add all the values from all the rows grouped together.

If you want one line per product you need to group by product_id.  You could remove the category data (3 items in SELECT and 2 JOINS), but if you need that category data in that one line per product you need to aggregate the category data with an aggregate function such as GROUP_CONCAT(). Then you can fix the sum by dividing by the number of categories. You can get the number of categories with COUNT(DISTINCT category_id).

Your queries would look like this:

```<code>SELECT
    p.product_id,
    p.title,
    p.description,
    p.specifications,
    p.price,
    p.discount,
    p.discount_type,
    p.color,
    p.hours,
    p.product_type,
    p.gender,
    p.img,
    p.vdo_intro,
    p.status,
    p.url,
    p.date_start,
    p.date_end,
    p.ip,
    p.product_page,
    p.product_code,
    p.brand,
    p.color_url,
    GROUP_CONCAT(DISTINCT c.category_id) AS category_id,
    GROUP_CONCAT(DISTINCT c.category_url) AS urlcategory,
    GROUP_CONCAT(DISTINCT c.parent) AS parent,
    ROUND(SUM(o.order_quantity) / COUNT(DISTINCT c.category_id)) AS solds
FROM        order_detail o
JOIN        product AS p ON o.product_id = p.product_id
JOIN        category_relations rcat ON rcat.product_id = p.product_id
LEFT JOIN   category AS c ON rcat.category_id = c.category_id
WHERE
    p.gender = ? AND
    p.status=1 AND
    p.date_end IS NULL
GROUP BY p.product_id
ORDER BY ROUND(SUM(o.order_quantity) / COUNT(DISTINCT c.category_id)) DESC
LIMIT 12;

SELECT
    p.product_id,
    p.title,
    p.description,
    p.specifications,
    p.price,
    p.discount,
    p.discount_type,
    p.color,
    p.hours,
    p.product_type,
    p.gender,
    p.img,
    p.vdo_intro,
    p.status,
    p.url,
    p.date_start,
    p.date_end,
    p.ip,
    p.product_page,
    p.product_code,
    p.brand,
    p.color_url,
    GROUP_CONCAT(DISTINCT c.category_id) AS category_id,
    GROUP_CONCAT(DISTINCT c.category_url) AS urlcategory,
    GROUP_CONCAT(DISTINCT c.parent) AS parent,
    ROUND(SUM(v.total) / COUNT(DISTINCT c.category_id)) AS solds
FROM
            visits AS v
JOIN        product AS p ON v.product_id = p.product_id
JOIN        category_relations AS rcat ON rcat.product_id = p.product_id
LEFT JOIN   category AS c ON rcat.category_id = c.category_id
WHERE
    p.gender = ? AND
    p.status=1 AND
    p.date_end IS NULL
GROUP BY p.product_id
ORDER BY ROUND(SUM(v.total) / COUNT(DISTINCT c.category_id))
DESC LIMIT 12;
</code>```

DB Fiddle
(I commented some stuff in the fiddle to make it easier to see the results)

To have a consistent order for the category fields that were concatenated you can use an ORER BY clause in the GROUP CONCAT function:

```<code>GROUP_CONCAT(DISTINCT c.category_id ORDER BY c.category_id) AS category_id,
GROUP_CONCAT(DISTINCT c.category_url ORDER BY c.category_id) AS urlcategory,
GROUP_CONCAT(DISTINCT c.parent ORDER BY c.category_id) AS parent,
</code>```

MySQL doc for GROUP_CONCAT

If you only need one result from categories, let's say the category with the lowest id, you can use this trick:  You left join your category_relations again with a condition that category_id is lower than the first one and then in your WHERE clause only select the rows where that IS NULL - no lower category_id was found.

```<code>...
    c.category_id,
    c.category_url,
    c.parent,
    ROUND(SUM(o.order_quantity) / COUNT(DISTINCT c.category_id)) AS solds
FROM        order_detail o
JOIN        product AS p ON o.product_id = p.product_id
JOIN        category_relations rcat ON rcat.product_id = p.product_id
LEFT JOIN   category AS c ON rcat.category_id = c.category_id
LEFT JOIN   category_relations AS rcat2 ON rcat.product_id = p.product_id AND rcat2.category_id < rcat.category_id
WHERE
    p.gender = ? AND
    p.status=1 AND
    p.date_end IS NULL AND
    rcat2.category_id IS NULL
GROUP BY p.product_id, c.category_id
ORDER BY ROUND(SUM(o.order_quantity) / COUNT(DISTINCT c.category_id)) DESC
...
</code>```

Body:
I'm having trouble getting the total of most sold and total most viewed products correctly.

Case best sellers:

   SELECT p.product_id, p.title, p.description, p.specifications, p.price, p.discount, p.discount_type, p.color, p.hours, p.product_type, p.gender, p.img, p.vdo_intro, p.status, p.url, p.date_start, p.date_end, p.ip, p.product_page, p.product_code, p.brand,   p.color_url,
          c.category_id, c.category_url as urlcategory, c.parent,
          SUM(o.order_quantity) as solds
     FROM order_detail o
     JOIN product p ON o.product_id = p.product_id
     JOIN category_relations rcat ON rcat.product_id = p.product_id
LEFT JOIN `category` c on rcat.category_id = c.category_id
  WHERE p.gender = ? AND p.status=1 AND p.date_end IS NULL
 GROUP BY p.product_id
 ORDER BY SUM(o.order_quantity)
     DESC LIMIT 12;

In Data Base i have:

id_order    order_quantity  order_price order_discount  order_discount_type product_id  order_date
    1           2               59.00        0               %                 1        2022-01-31 22:49:24
    2           5               59.00        0               %                20        2022-01-31 22:49:24
    3          12               59.00        0               %                 8        2022-01-31 22:49:24
    4           5               59.00        0               %                19        2022-01-31 22:49:24
    5          25               59.00        0               %                17        2022-01-31 22:49:24
    6           3               59.00        0               %                 1        2022-01-31 22:49:24

Result i get :

id:1    2 + 3 = 5 solds    i get = 20 solds (wrong).
id:20   5 solds            i get = 15 solds (wrong).
id:8   12 solds            i get = 36 solds (wrong).
id:19   5 solds            i get = 5 solds (ok).
id:17  25 solds            i get = 50 solds (wrong).

Does anyone know what I'm doing wrong?

It look like that it is multiplying the results by records.

Case most visits:

    SELECT p.product_id, p.title, p.description, p.specifications, p.price, p.discount, p.discount_type, p.color, p.hours, p.product_type, p.gender, p.img, p.vdo_intro, p.status, p.url, p.date_start, p.date_end, p.ip, p.product_page, p.product_code, p.brand, p.color_url,
           c.category_id, c.category_url as urlcategory, c.parent,
           SUM(v.total) as visits
      FROM `visits` v
      JOIN `product` p ON v.product_id = p.product_id
      JOIN category_relations rcat ON rcat.product_id = p.product_id
 LEFT JOIN `category` c on rcat.category_id = c.category_id
     WHERE p.gender = ? AND p.status=1 AND p.date_end IS NULL
  GROUP BY p.product_id
  ORDER BY SUM(v.total)
      DESC LIMIT 12

My database:

id_visits   total      ip              today        product_id
23           5     xxxxxxxxxxxxx1   2022-02-09        1
36           1     xxxxxxxxxxxxx4   2022-02-06       13
40           1     xxxxxxxxxxxxx3   2022-02-06       13
41           1     xxxxxxxxxxxxx1   2022-02-06       21
48           1     xxxxxxxxxxxxx2   2022-02-07       13
50           1     xxxxxxxxxxxxx2   2022-02-07        1
62           1     xxxxxxxxxxxxx8   2022-02-08        1

Result i get:

id1 :   5 + 1 + 1 = 7 visits      i get 28 visits (wrong)
id13:   1 + 1 + 1 = 3 visits      i get 9 visits (wrong)
id21:   1         = 1 visits      i get 3 visits (wrong)

Does anyone know the error, because it does not add up correctly?

note: I can add the tables if needed

Edit:

DB Fiddle

If i do:

GROUP BY rcat.cat_relation_id

I get the calculation right, but it repeats the same products

Body:
Your problem is that category_relations is a many to many table.  So when you join it you get additional results for each category that a product is in.  So product id1 is in 4 categories so you get a sum of 28 instead of 7.

Your query is producing a row for each order for each category that the product ordered is in and then grouping all rows with the same product_id together.  SUM will add all the values from all the rows grouped together.

If you want one line per product you need to group by product_id.  You could remove the category data (3 items in SELECT and 2 JOINS), but if you need that category data in that one line per product you need to aggregate the category data with an aggregate function such as GROUP_CONCAT(). Then you can fix the sum by dividing by the number of categories. You can get the number of categories with COUNT(DISTINCT category_id).

Your queries would look like this:

SELECT
    p.product_id,
    p.title,
    p.description,
    p.specifications,
    p.price,
    p.discount,
    p.discount_type,
    p.color,
    p.hours,
    p.product_type,
    p.gender,
    p.img,
    p.vdo_intro,
    p.status,
    p.url,
    p.date_start,
    p.date_end,
    p.ip,
    p.product_page,
    p.product_code,
    p.brand,
    p.color_url,
    GROUP_CONCAT(DISTINCT c.category_id) AS category_id,
    GROUP_CONCAT(DISTINCT c.category_url) AS urlcategory,
    GROUP_CONCAT(DISTINCT c.parent) AS parent,
    ROUND(SUM(o.order_quantity) / COUNT(DISTINCT c.category_id)) AS solds
FROM        order_detail o
JOIN        product AS p ON o.product_id = p.product_id
JOIN        category_relations rcat ON rcat.product_id = p.product_id
LEFT JOIN   category AS c ON rcat.category_id = c.category_id
WHERE
    p.gender = ? AND
    p.status=1 AND
    p.date_end IS NULL
GROUP BY p.product_id
ORDER BY ROUND(SUM(o.order_quantity) / COUNT(DISTINCT c.category_id)) DESC
LIMIT 12;

SELECT
    p.product_id,
    p.title,
    p.description,
    p.specifications,
    p.price,
    p.discount,
    p.discount_type,
    p.color,
    p.hours,
    p.product_type,
    p.gender,
    p.img,
    p.vdo_intro,
    p.status,
    p.url,
    p.date_start,
    p.date_end,
    p.ip,
    p.product_page,
    p.product_code,
    p.brand,
    p.color_url,
    GROUP_CONCAT(DISTINCT c.category_id) AS category_id,
    GROUP_CONCAT(DISTINCT c.category_url) AS urlcategory,
    GROUP_CONCAT(DISTINCT c.parent) AS parent,
    ROUND(SUM(v.total) / COUNT(DISTINCT c.category_id)) AS solds
FROM
            visits AS v
JOIN        product AS p ON v.product_id = p.product_id
JOIN        category_relations AS rcat ON rcat.product_id = p.product_id
LEFT JOIN   category AS c ON rcat.category_id = c.category_id
WHERE
    p.gender = ? AND
    p.status=1 AND
    p.date_end IS NULL
GROUP BY p.product_id
ORDER BY ROUND(SUM(v.total) / COUNT(DISTINCT c.category_id))
DESC LIMIT 12;

DB Fiddle
(I commented some stuff in the fiddle to make it easier to see the results)

To have a consistent order for the category fields that were concatenated you can use an ORER BY clause in the GROUP CONCAT function:

GROUP_CONCAT(DISTINCT c.category_id ORDER BY c.category_id) AS category_id,
GROUP_CONCAT(DISTINCT c.category_url ORDER BY c.category_id) AS urlcategory,
GROUP_CONCAT(DISTINCT c.parent ORDER BY c.category_id) AS parent,

MySQL doc for GROUP_CONCAT

If you only need one result from categories, let's say the category with the lowest id, you can use this trick:  You left join your category_relations again with a condition that category_id is lower than the first one and then in your WHERE clause only select the rows where that IS NULL - no lower category_id was found.

...
    c.category_id,
    c.category_url,
    c.parent,
    ROUND(SUM(o.order_quantity) / COUNT(DISTINCT c.category_id)) AS solds
FROM        order_detail o
JOIN        product AS p ON o.product_id = p.product_id
JOIN        category_relations rcat ON rcat.product_id = p.product_id
LEFT JOIN   category AS c ON rcat.category_id = c.category_id
LEFT JOIN   category_relations AS rcat2 ON rcat.product_id = p.product_id AND rcat2.category_id < rcat.category_id
WHERE
    p.gender = ? AND
    p.status=1 AND
    p.date_end IS NULL AND
    rcat2.category_id IS NULL
GROUP BY p.product_id, c.category_id
ORDER BY ROUND(SUM(o.order_quantity) / COUNT(DISTINCT c.category_id)) DESC
...


Title: In R: shorten list from behind till a certain value

Body:
Suppose i have many and larger lists than in this example.

ID <- c(2,2,2,2,5,5,5,5,9,9,9,9)
Rounds <- c(0,1,2,3,0,1,2,3,0,1,2,3)
solution <- c(as.character('[[-1,1,1,-1],[1,-1,1,-1]]'),as.character('[[-1,1,-1,-1],[1,-1,1,-1]]'),as.character('[[-1,1,1,-1],[1,-1,-1,-1]]') )
solution_resp <- c(as.character('[[-1,1,1,1],[1,1,-1,1]]'),as.character('[[1,-1,1,-1],[1,-1,1,1]]'),as.character('[[-1,1,1,1],[1,-1,1,-1]]'))

dt <- data.frame(ID,Rounds,solution,solution_resp)

dt$solution <- gsub('],[',',', dt$solution, fixed=TRUE)
dt$solution <- gsub('[[',''  , dt$solution, fixed=TRUE)
dt$solution <- gsub(']]',''  , dt$solution, fixed=TRUE)

dt$solution <- as.list(strsplit(dt$solution, ","))

dt$solution_resp <- gsub('],[',',', dt$solution_resp, fixed=TRUE)
dt$solution_resp <- gsub('[[',''  , dt$solution_resp, fixed=TRUE)
dt$solution_resp <- gsub(']]',''  , dt$solution_resp, fixed=TRUE)

dt$solution_resp <- as.list(strsplit(dt$solution_resp, ","))

Looks like this:

   ID Rounds                    solution             solution_resp
1   2      0  -1, 1, 1, -1, 1, -1, 1, -1  -1, 1, 1, 1, 1, 1, -1, 1
2   2      1 -1, 1, -1, -1, 1, -1, 1, -1 1, -1, 1, -1, 1, -1, 1, 1
3   2      2 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1
4   2      3  -1, 1, 1, -1, 1, -1, 1, -1  -1, 1, 1, 1, 1, 1, -1, 1
5   5      0 -1, 1, -1, -1, 1, -1, 1, -1 1, -1, 1, -1, 1, -1, 1, 1
6   5      1 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1
7   5      2  -1, 1, 1, -1, 1, -1, 1, -1  -1, 1, 1, 1, 1, 1, -1, 1
8   5      3 -1, 1, -1, -1, 1, -1, 1, -1 1, -1, 1, -1, 1, -1, 1, 1
9   9      0 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1
10  9      1  -1, 1, 1, -1, 1, -1, 1, -1  -1, 1, 1, 1, 1, 1, -1, 1
11  9      2 -1, 1, -1, -1, 1, -1, 1, -1 1, -1, 1, -1, 1, -1, 1, 1
12  9      3 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1

In the lists dt$solution_resp I would like to drop all 1 from behind until the first -1 appears (counting from right to left).

In the example this would mean shortening the lists by taking off the last element in first row (and every third row). In the second row (and every third) the list shall be shorted by two elements. The third row remains as it is because there is a -1 at the last position already.

Then, I want the dt$solution lists to have the same length.

Solution:

   ID Rounds                    solution             solution_resp
1   2      0  -1, 1, 1, -1, 1, -1, 1     -1, 1, 1, 1, 1, 1, -1
2   2      1 -1, 1, -1, -1, 1, -1         1, -1, 1, -1, 1, -1
3   2      2 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1
4   2      3  -1, 1, 1, -1, 1, -1, 1     -1, 1, 1, 1, 1, 1, -1
5   5      0 -1, 1, -1, -1, 1, -1         1, -1, 1, -1, 1, -1
6   5      1 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1
7   5      2  -1, 1, 1, -1, 1, -1, 1     -1, 1, 1, 1, 1, 1, -1
8   5      3 -1, 1, -1, -1, 1, -1,        1, -1, 1, -1, 1, -1
9   9      0 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1
10  9      1  -1, 1, 1, -1, 1, -1, 1,    -1, 1, 1, 1, 1, 1, -1
11  9      2 -1, 1, -1, -1, 1, -1,        1, -1, 1, -1, 1, -1
12  9      3 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1

Body:
Using the original dt <- data.frame(ID,Rounds,solution,solution_resp) Do the following:

dt %>%
      mutate(across(contains('solution'),
            ~str_split(str_remove_all(.x,'\\[|\\]|(, ?1)\\1*\\]+$'), ',')))
  ID Rounds                    solution             solution_resp
1   2      0  -1, 1, 1, -1, 1, -1, 1, -1     -1, 1, 1, 1, 1, 1, -1
2   2      1 -1, 1, -1, -1, 1, -1, 1, -1       1, -1, 1, -1, 1, -1
3   2      2 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1
4   2      3  -1, 1, 1, -1, 1, -1, 1, -1     -1, 1, 1, 1, 1, 1, -1
5   5      0 -1, 1, -1, -1, 1, -1, 1, -1       1, -1, 1, -1, 1, -1
6   5      1 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1
7   5      2  -1, 1, 1, -1, 1, -1, 1, -1     -1, 1, 1, 1, 1, 1, -1
8   5      3 -1, 1, -1, -1, 1, -1, 1, -1       1, -1, 1, -1, 1, -1
9   9      0 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1
10  9      1  -1, 1, 1, -1, 1, -1, 1, -1     -1, 1, 1, 1, 1, 1, -1
11  9      2 -1, 1, -1, -1, 1, -1, 1, -1       1, -1, 1, -1, 1, -1
12  9      3 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1

in Base R:

dt[3:4] <- gsub('\\[|\\]|(, ?1)\\1*\\]+$', '', as.matrix(dt[3:4]))
dt[3:4]<-lapply(dt[3:4], strsplit, ',')
dt
   ID Rounds                    solution             solution_resp
1   2      0  -1, 1, 1, -1, 1, -1, 1, -1     -1, 1, 1, 1, 1, 1, -1
2   2      1 -1, 1, -1, -1, 1, -1, 1, -1       1, -1, 1, -1, 1, -1
3   2      2 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1
4   2      3  -1, 1, 1, -1, 1, -1, 1, -1     -1, 1, 1, 1, 1, 1, -1
5   5      0 -1, 1, -1, -1, 1, -1, 1, -1       1, -1, 1, -1, 1, -1
6   5      1 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1
7   5      2  -1, 1, 1, -1, 1, -1, 1, -1     -1, 1, 1, 1, 1, 1, -1
8   5      3 -1, 1, -1, -1, 1, -1, 1, -1       1, -1, 1, -1, 1, -1
9   9      0 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1
10  9      1  -1, 1, 1, -1, 1, -1, 1, -1     -1, 1, 1, 1, 1, 1, -1
11  9      2 -1, 1, -1, -1, 1, -1, 1, -1       1, -1, 1, -1, 1, -1
12  9      3 -1, 1, 1, -1, 1, -1, -1, -1 -1, 1, 1, 1, 1, -1, 1, -1


问:
disable reusing same file_id for media
is there a way to disable reusing file_id for media? since it's lookup on database
and I want media reuploaded every time

I use copy message client.send_meesage on telethon and it's still looks up for file_id of image or video on database

i'm looking to make telethon to upload media instead of reuse file_id from database
as client.send_file can be done with allow_cache=False

I looked on docs, couldn't find anything related, and seem allow_cache=False not works with client.send_meesage

答:
You can use telethon.sessions.string.StringSession, this doesn't cache any data.

But probably you still want to cache the entities, so you can create your own class that inherits from one telethon.sessions.sqlite.SQLiteSession or telethon.sessions.memory.MemorySession, override the get_file method and make this returns always None. If you don't want to cache the files neither, must override cache_file method and make this just pass.

from telethon import TelegramClient
from telethon.sessions import SQLiteSession

class NoFilesSession(SQLiteSession):

    def get_file(self):
        pass

    def cache_file(self):
        pass

client = TelegramClient(NoFilesSession('client'), API_ID, API_HASH)
```

This new session will mock the function client.send_message, making it trust  that there are no matches in the cache.

I recommend you to dive in the telethon.sessions package source code and the documentation for the default types of sessions.

Title: disable reusing same file_id for media

Body:
You can use telethon.sessions.string.StringSession, this doesn't cache any data.

But probably you still want to cache the entities, so you can create your own class that inherits from one telethon.sessions.sqlite.SQLiteSession or telethon.sessions.memory.MemorySession, override the get_file method and make this returns always None. If you don't want to cache the files neither, must override cache_file method and make this just pass.

from telethon import TelegramClient
from telethon.sessions import SQLiteSession

class NoFilesSession(SQLiteSession):

    def get_file(self):
        pass

    def cache_file(self):
        pass

client = TelegramClient(NoFilesSession('client'), API_ID, API_HASH)

This new session will mock the function client.send_message, making it trust  that there are no matches in the cache.

I recommend you to dive in the telethon.sessions package source code and the documentation for the default types of sessions.


问:
Python 3.8 - Unable to update dictionary
Hello I have this below dictionary that I want to update

```<code>{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Principal":{
            "Federated":"arn:aws:iam::111111111111:saml-provider/Test"
         },
         "Action":"sts:AssumeRoleWithSAML",
         "Condition":{
            "StringEquals":{
               "SAML:aud":"https://signin.aws.amazon.com/saml"
            }
         }
      }
   ]
}
</code>```

Want to update it to

```<code>{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Principal":{
            "Federated":[
            "arn:aws:iam::111111111111:saml-provider/Test",
            "arn:aws:iam::111111111111:saml-provider/Test2"
            ]
         },
         "Action":"sts:AssumeRoleWithSAML",
         "Condition":{
            "StringEquals":{
               "SAML:aud":"https://signin.aws.amazon.com/saml"
            }
         }
      }
   ]
}
</code>```

i.e. add "arn:aws:iam::111111111111:saml-provider/Test2" to "Federated" and also make it a list. Below is my code

```<code>    new_arn = "arn:aws:iam::111111111111:saml-provider/Test2"
    my_dict = {
   "Version":"2012-10-17",
   "Statement":[
      {
             "Effect":"Allow",
             "Principal":{
                "Federated":[
                "arn:aws:iam::111111111111:saml-provider/Test",
                ]
             },
             "Action":"sts:AssumeRoleWithSAML",
             "Condition":{
                "StringEquals":{
                   "SAML:aud":"https://signin.aws.amazon.com/saml"
                }
             }
          }
       ]
    }
    for b in my_dict['Statement']:
        updated_arn = f"['{b['Principal']['Federated']}', {new_arn}]"
        b['Principal']['Federated']: updated_arn

    print(my_dict)
</code>```

I am bit new to python and I am not sure what am I doing wrong the dict is not getting updated. Can someone please provide some guidance on what I may be doing wrong

答:
You can append to the current value of "Federated" as follows:</p>
```<code>for b in my_dict['Statement']:
       b['Principal']['Federated'].append(new_arn)
</code>```

Body:
Hello I have this below dictionary that I want to update

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Principal":{
            "Federated":"arn:aws:iam::111111111111:saml-provider/Test"
         },
         "Action":"sts:AssumeRoleWithSAML",
         "Condition":{
            "StringEquals":{
               "SAML:aud":"https://signin.aws.amazon.com/saml"
            }
         }
      }
   ]
}

Want to update it to

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Principal":{
            "Federated":[
            "arn:aws:iam::111111111111:saml-provider/Test",
            "arn:aws:iam::111111111111:saml-provider/Test2"
            ]
         },
         "Action":"sts:AssumeRoleWithSAML",
         "Condition":{
            "StringEquals":{
               "SAML:aud":"https://signin.aws.amazon.com/saml"
            }
         }
      }
   ]
}

i.e. add "arn:aws:iam::111111111111:saml-provider/Test2" to "Federated" and also make it a list. Below is my code

    new_arn = "arn:aws:iam::111111111111:saml-provider/Test2"
    my_dict = {
   "Version":"2012-10-17",
   "Statement":[
      {
             "Effect":"Allow",
             "Principal":{
                "Federated":[
                "arn:aws:iam::111111111111:saml-provider/Test",
                ]
             },
             "Action":"sts:AssumeRoleWithSAML",
             "Condition":{
                "StringEquals":{
                   "SAML:aud":"https://signin.aws.amazon.com/saml"
                }
             }
          }
       ]
    }
    for b in my_dict['Statement']:
        updated_arn = f"['{b['Principal']['Federated']}', {new_arn}]"
        b['Principal']['Federated']: updated_arn

    print(my_dict)

I am bit new to python and I am not sure what am I doing wrong the dict is not getting updated. Can someone please provide some guidance on what I may be doing wrong

Body:
You can append to the current value of "Federated" as follows:

for b in my_dict['Statement']:
       b['Principal']['Federated'].append(new_arn)


问:
Select from insert
Is it possible to select from an insert statement?  For example:

```<code>SELECT id FROM (INSERT INTO table (col1, col2) VALUES (val1, val2));
</code>```

Where id is an autoincrementing primary key.

答:
"SELECT id FROM mytable WHERE id IS NULL;

"id" has to be an auto_increment column to make it work.
It will return the last_insert_id just as last_insert_id() is expected to do.

example:
mysql> INSERT INTO orders (customer_cust_id, orderdatetime, message, taxrate, shippingprice)
-> SELECT '1', NOW(), null, taxrate, shippingprice FROM customer
-> WHERE cust_id='1';"

http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

ContentLicense: CC BY-SA 3.0

Title: Select from insert

Body:
Is it possible to select from an insert statement?  For example:

SELECT id FROM (INSERT INTO table (col1, col2) VALUES (val1, val2));

Where id is an autoincrementing primary key.


问:
How to fix improperly distributed lighting projected onto a model in OpenGL?
I've been trying to implement the Blinn-Phong lighting model to project lighting onto an imported Wavefront OBJ model through Assimp(github link).

The model seems to be loaded correctly, however, there seems to be a point where the lighting appears to be "cut off" near the middle of the model.

Image of the imported model with and without lighting enabled.

As you can see on the left of the image above, there is a region in the middle of the model where the light effectively gets "split up" which is not what is intended. It can be seen that there is a sort of discrepancy where the side facing towards the light source appears brighter than normal and the side away from the light source appears darker than normal without any sort of easing in between the two sides.

I believe there might be something wrong with how I've implemented the lighting model in the fragment shader but I cannot say for sure as to why this is happening.

Vertex shader:

#version 330 core

layout (location = 0) in vec3 vertPos;
layout (location = 1) in vec3 vertNormal;
layout (location = 2) in vec2 vertTexCoords;

out vec3 fragPos;
out vec3 fragNormal;
out vec2 fragTexCoords;

uniform mat4 proj, view, model;
uniform mat3 normalMat;

void main() {
    fragPos = vec3(model * vec4(vertPos, 1));
    gl_Position = proj * view * vec4(fragPos, 1);
    fragTexCoords = vertTexCoords;
    fragNormal = normalMat * vertNormal;
}
```
<p><em>Fragment shader:</em></p>
```#version 330 core

in vec3 fragPos;
in vec3 fragNormal;
in vec2 fragTexCoords;

out vec4 FragColor;

const int noOfDiffuseMaps = 1;
const int noOfSpecularMaps = 1;

struct Material {
    sampler2D diffuseMaps[noOfDiffuseMaps], specularMaps[noOfSpecularMaps];
    float shininess;
};

struct Light {
    vec3 direction;
    vec3 ambient, diffuse, specular;
};

uniform Material material;
uniform Light light;

uniform vec3 viewPos;

const float pi = 3.14159265;

uniform float gamma = 2.2;

float near = 0.1;
float far = 100;

float LinearizeDepth(float depth)
{
    float z = depth * 2 - 1;
    return (2 * near * far) / (far + near - z * (far - near));
}

void main() {
    vec3 normal = normalize(fragNormal);

    vec3 calculatedColor = vec3(0);

    for (int i = 0; i < noOfDiffuseMaps; i++) {
        vec3 diffuseTexel = texture(material.diffuseMaps[i], fragTexCoords).rgb;

        // Ambient lighting
        vec3 ambient = diffuseTexel * light.ambient;

        // Diffuse lighting
        float diff = max(dot(light.direction, normal), 0);
        vec3 diffuse = diffuseTexel * light.diffuse * diff;

        calculatedColor += ambient + diffuse;
    }

    for (int i = 0; i < noOfSpecularMaps; i++) {
        vec3 specularTexel = texture(material.specularMaps[0], fragTexCoords).rgb;
        vec3 viewDir = normalize(viewPos - fragPos);
        vec3 halfWayDir = normalize(viewDir + light.direction);
        float energyConservation = (8 + material.shininess) / (8 * pi);

        // Specular lighting
        float spec = pow(max(dot(halfWayDir, normal), 0), material.shininess);
        vec3 specular = specularTexel * light.specular * spec * energyConservation;

        calculatedColor += specular;
    }

    float depthColor = 1 - LinearizeDepth(gl_FragCoord.z) / far;
    FragColor = vec4(pow(calculatedColor, vec3(1 / gamma)) * depthColor, 1);
}
```

答:
Make sure your texture and colors are also linear(it is a simple pow 2.2) because you are doing gamma encoding at the end.

Also note, it is expected to have a harsh terminator.

http://filmicworlds.com/blog/linear-space-lighting-i-e-gamma/

Beyond that, if you expect soft falloffs, it must be coming from an area light. For that you can implement wrap lighting or area lights.

Body:
I've been trying to implement the Blinn-Phong lighting model to project lighting onto an imported Wavefront OBJ model through Assimp(github link).

The model seems to be loaded correctly, however, there seems to be a point where the lighting appears to be "cut off" near the middle of the model.

Image of the imported model with and without lighting enabled.

As you can see on the left of the image above, there is a region in the middle of the model where the light effectively gets "split up" which is not what is intended. It can be seen that there is a sort of discrepancy where the side facing towards the light source appears brighter than normal and the side away from the light source appears darker than normal without any sort of easing in between the two sides.

I believe there might be something wrong with how I've implemented the lighting model in the fragment shader but I cannot say for sure as to why this is happening.

Vertex shader:

#version 330 core

layout (location = 0) in vec3 vertPos;
layout (location = 1) in vec3 vertNormal;
layout (location = 2) in vec2 vertTexCoords;

out vec3 fragPos;
out vec3 fragNormal;
out vec2 fragTexCoords;

uniform mat4 proj, view, model;
uniform mat3 normalMat;

void main() {
    fragPos = vec3(model * vec4(vertPos, 1));
    gl_Position = proj * view * vec4(fragPos, 1);
    fragTexCoords = vertTexCoords;
    fragNormal = normalMat * vertNormal;
}

Fragment shader:

#version 330 core

in vec3 fragPos;
in vec3 fragNormal;
in vec2 fragTexCoords;

out vec4 FragColor;

const int noOfDiffuseMaps = 1;
const int noOfSpecularMaps = 1;

struct Material {
    sampler2D diffuseMaps[noOfDiffuseMaps], specularMaps[noOfSpecularMaps];
    float shininess;
};

struct Light {
    vec3 direction;
    vec3 ambient, diffuse, specular;
};

uniform Material material;
uniform Light light;

uniform vec3 viewPos;

const float pi = 3.14159265;

uniform float gamma = 2.2;

float near = 0.1;
float far = 100;

float LinearizeDepth(float depth)
{
    float z = depth * 2 - 1;
    return (2 * near * far) / (far + near - z * (far - near));
}

void main() {
    vec3 normal = normalize(fragNormal);

    vec3 calculatedColor = vec3(0);

    for (int i = 0; i < noOfDiffuseMaps; i++) {
        vec3 diffuseTexel = texture(material.diffuseMaps[i], fragTexCoords).rgb;

        // Ambient lighting
        vec3 ambient = diffuseTexel * light.ambient;

        // Diffuse lighting
        float diff = max(dot(light.direction, normal), 0);
        vec3 diffuse = diffuseTexel * light.diffuse * diff;

        calculatedColor += ambient + diffuse;
    }

    for (int i = 0; i < noOfSpecularMaps; i++) {
        vec3 specularTexel = texture(material.specularMaps[0], fragTexCoords).rgb;
        vec3 viewDir = normalize(viewPos - fragPos);
        vec3 halfWayDir = normalize(viewDir + light.direction);
        float energyConservation = (8 + material.shininess) / (8 * pi);

        // Specular lighting
        float spec = pow(max(dot(halfWayDir, normal), 0), material.shininess);
        vec3 specular = specularTexel * light.specular * spec * energyConservation;

        calculatedColor += specular;
    }

    float depthColor = 1 - LinearizeDepth(gl_FragCoord.z) / far;
    FragColor = vec4(pow(calculatedColor, vec3(1 / gamma)) * depthColor, 1);
}


问:
Android Text Views overflowing and blocking each other out
I am having issues with 2 text views next to each other in a constraint layout. One is a title and one is a dummy "description" and the description ends up displaying off the right edge of the screen and also pushing the title off the left edge of the screen. What am I getting wrong with the layout, the description text needs to do more linebreaks to give the title and itself space to fit within the screen.

```<code><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/fillerContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toEndOf="@+id/label"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"/>

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:text="TextView"
    android:textSize="20sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/fillerContent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>
</code>```

edit 1: This view is an item in a recyclerlist.

答:
your problem is cause by a miss understood on the way that constraints layout work but more important like the attributes of width and height must be used to had a more responsive layout.

When you are using constraints layouts the ideal approach is that views attributes  width and height has been set as 0dp, meaning that they will grow respecting the available space and the constraints assigned.

Here is a example of two approaches:

1. You could set the width to 0dp:

```<code> <TextView
        android:id="@+id/fillerContent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="DESCRIPTION"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/label"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"/>

    <TextView
        android:id="@+id/label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="TextView"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/fillerContent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</code>```

2. Or the equivalent with a little refactor:

```<code>     <TextView
        android:id="@+id/fillerContent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="DESCRIPTION DESCRIPTION"
        app:layout_constraintStart_toEndOf="@id/label"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

    <TextView
        android:id="@+id/label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="Title "
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />
</code>```

Body:
I am having issues with 2 text views next to each other in a constraint layout. One is a title and one is a dummy "description" and the description ends up displaying off the right edge of the screen and also pushing the title off the left edge of the screen. What am I getting wrong with the layout, the description text needs to do more linebreaks to give the title and itself space to fit within the screen.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/fillerContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toEndOf="@+id/label"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"/>

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:text="TextView"
    android:textSize="20sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/fillerContent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

edit 1: This view is an item in a recyclerlist.

Body:
your problem is cause by a miss understood on the way that constraints layout work but more important like the attributes of width and height must be used to had a more responsive layout.

When you are using constraints layouts the ideal approach is that views attributes  width and height has been set as 0dp, meaning that they will grow respecting the available space and the constraints assigned.

Here is a example of two approaches:

1. You could set the width to 0dp:

 <TextView
        android:id="@+id/fillerContent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="DESCRIPTION"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/label"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"/>

    <TextView
        android:id="@+id/label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="TextView"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/fillerContent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

2. Or the equivalent with a little refactor:

     <TextView
        android:id="@+id/fillerContent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="DESCRIPTION DESCRIPTION"
        app:layout_constraintStart_toEndOf="@id/label"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

    <TextView
        android:id="@+id/label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="Title "
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />