简体   繁体   中英

Variable values inside selectInput in Shiny

Here is my R shiny code:

ui.R

shinyUI(bootstrapPage(
uiOutput("GD"),
hr(),
fluidRow(column(3, verbatimTextOutput("value")))
))

server.R

shinyServer(function(input, output) {

  output$GD <- renderUI({ 
  nBads <- sum(tab[nrow(tab),3:ncol(tab)])
  nGoods <- sum(tab[(nrow(tab)-1),3:ncol(tab)])
  nTotal <- nBads + nGoods


  options(warn=-1) 
  Outcome_color <- 0
  G <- 0  
  st <-0
  ldate <- 0
  for (k in (1:(nrow(tab)/2))) {

  tabela <- data.frame(tab[1:2,3:(ncol(tab))])
  rownames(tabela) <- c("Goods", "Bads")

  wr <- (sum(tabela)^2 - (rowSums(tabela)[[1]]^2 + rowSums(tabela)[[2]]^2))
x <- which(tabela >= 0)
y <- x[lapply(x,"%%",2)==0]
y1 <- x[lapply(x,"%%",2)!=0]
n <-length(y)


a<-0; a1<-0; Nc<-0; A1x<-0; Nd<-0; D2x<-0;


for (i in n:2 ) {

  c <- a
  a <- tabela[2, (y[i]/2)] + c
  Nc[i-1] <- a * tabela[1, i-1]
  A1x[i-1] <- a

  c1 <- a1
  a1 <- tabela[1, ((y1[i]+1)/2)] + c1
  Nd[i-1] <- a1 * tabela[2, i-1]
  D2x[i-1] <- a1
}

A1x[length(y)] <- 0
D2x[length(y)] <- 0
NC <- sum(Nc)
ND <- sum(Nd)


a2 <- 0; A2x <- 0; a3 <- 0; D1x <- 0; A2x[1] <- 0; D1x[1] <- 0;

for (i in 1:(n-1)) {
  c2 <- a2
  a2 <- tabela[1, ((y1[i]+1)/2)] + c2
  A2x[i+1] <- a2 

  c3 <- a3
  a3 <- tabela[2, (y[i]/2)] + c3
  D1x[i+1] <- a3

}

A <- t(cbind(A1x, A2x))
D <- t(cbind(D1x,D2x))
d <- A-D
PQ <- 2*(NC-ND)

SD <- PQ / wr;




var <- 0

for (i in 1:2) {
  for (j in i:n) {

    var <- (tabela[i,j] * ((wr * d[i,j] - (PQ * (sum(tabela) - rowSums(tabela)[[i]])))^2)) + var
  } 
}


sterr <- sqrt(var * 4/(wr)^4)

G1 <- sprintf("%.2f%%", 100 * SD)
St1 <- sprintf("%.2f%%", 100 * sterr)

ldate[k] <-  paste(tab[[1]][[1]]) 
G[[k]] <- SD
st[[k]] <- sterr

tab <- tab[-c(1,2),]
  }

selectInput("gin", "Choose date", ldate)
  })

txt <- reactive({ input$gin }) 
output$value <- renderText({  
  paste("G is ", "\n", txt())  })


})  

"ldate" variable is a list of dates formatted as characters, while "G" and "st" are lists of numeric values.

I get a select list with dates as a result, but when I select each date I get only date and "G is" in a sentence.

What I want to get as a result when I choose a date from select list is for example:

2009-12-05

"G is 0.55"

"st is 0.08"

I don't know how to come up to with a solution. How to put G and st inside output$value part of the code?

Edit

Here is the sample data for tab:

head(tab, 2)

        Date Category 1 2  3  4   5   6 7
1 2008-11-28    Goods 1 3 28 47 132 123 1
2                Bads 0 0  1  4   9  27 2

Here is the modified code with reactive values. Hope it helps!

tab <- data.frame(Date = c("2008-11-28","", "2008-11-29",""), Category = c("Goods", "Bads", "Goods", "Bads"), 
                    "1"=c(1, 0, 2, 0), "2"=c(3,0,4, 0), "3"=c(28, 1, 29, 2), "4"=c(47,4, 48, 5), 
                    "5"=c(132, 9, 133, 10), "6"=c(123, 27, 124, 28), "7"=c(1, 2, 1, 2))

  ui <-  shinyUI(bootstrapPage(
    uiOutput("GD"),
    hr(),
    fluidRow(column(3, verbatimTextOutput("value")))
  ))

  server <- shinyServer(function(input, output) {

    vals <- reactiveValues(G = 0, st = 0, ldate = 0)


    output$GD <- renderUI({ 
      nBads <- sum(tab[nrow(tab),3:ncol(tab)])
      nGoods <- sum(tab[(nrow(tab)-1),3:ncol(tab)])
      nTotal <- nBads + nGoods


      options(warn=-1) 
      Outcome_color <- 0
      G <- 0  
      st <-0
      ldate <- 0
      for (k in (1:(nrow(tab)/2))) {

        tabela <- data.frame(tab[1:2,3:(ncol(tab))])
        rownames(tabela) <- c("Goods", "Bads")

        wr <- (sum(tabela)^2 - (rowSums(tabela)[[1]]^2 + rowSums(tabela)[[2]]^2))
        x <- which(tabela >= 0)
        y <- x[lapply(x,"%%",2)==0]
        y1 <- x[lapply(x,"%%",2)!=0]
        n <-length(y)


        a<-0; a1<-0; Nc<-0; A1x<-0; Nd<-0; D2x<-0;


        for (i in n:2 ) {

          c <- a
          a <- tabela[2, (y[i]/2)] + c
          Nc[i-1] <- a * tabela[1, i-1]
          A1x[i-1] <- a

          c1 <- a1
          a1 <- tabela[1, ((y1[i]+1)/2)] + c1
          Nd[i-1] <- a1 * tabela[2, i-1]
          D2x[i-1] <- a1
        }

        A1x[length(y)] <- 0
        D2x[length(y)] <- 0
        NC <- sum(Nc)
        ND <- sum(Nd)


        a2 <- 0; A2x <- 0; a3 <- 0; D1x <- 0; A2x[1] <- 0; D1x[1] <- 0;

        for (i in 1:(n-1)) {
          c2 <- a2
          a2 <- tabela[1, ((y1[i]+1)/2)] + c2
          A2x[i+1] <- a2 

          c3 <- a3
          a3 <- tabela[2, (y[i]/2)] + c3
          D1x[i+1] <- a3

        }

        A <- t(cbind(A1x, A2x))
        D <- t(cbind(D1x,D2x))
        d <- A-D
        PQ <- 2*(NC-ND)

        SD <- PQ / wr;




        var <- 0

        for (i in 1:2) {
          for (j in i:n) {

            var <- (tabela[i,j] * ((wr * d[i,j] - (PQ * (sum(tabela) - rowSums(tabela)[[i]])))^2)) + var
          } 
        }


        sterr <- sqrt(var * 4/(wr)^4)

        G1 <- sprintf("%.2f%%", 100 * SD)
        St1 <- sprintf("%.2f%%", 100 * sterr)

        vals$ldate[k] <-  paste(tab[[1]][[1]]) 
        vals$G[[k]] <- SD
        vals$st[[k]] <- sterr

        tab <- tab[-c(1,2),]
      }

      selectInput("gin", "Choose date", vals$ldate)
    })

    txt <- reactive({ input$gin }) 
    output$value <- renderText({  
      paste0(txt(), "\n G is" ,vals$G[which(vals$ldate == input$gin )], "\n st is", vals$st[which(vals$ldate == input$gin)])  
    })


  })  

  shinyApp(ui=ui, server=server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM