Использование If для работы с входными данными и именами с использованием Shiny R - PullRequest
2 голосов
/ 01 ноября 2019

Это мой код в Shiny:

my.table<-structure(list(Dates = structure(c(18184, 18183, 18180, 18179,18178, 18177, 18176, 18173, 18172, 18171), class = "Date"), B = c(0.9257, 
                                                                                                                                          0.9237, 0.9233, 0.9203, 0.9254, 0.9152, 0.9107, 0.9189, 0.9246,0.926), C = c(0.5692, 0.5688, 0.569, 0.5686, 0.5688, 0.5681, 
                                                                                                                                                                                                                       0.5692, 0.5687, 0.5695, 0.5684), D = c(0.6715, 0.6722, 0.6645, 0.6666, 0.6709, 0.675, 0.6718, 0.6731, 0.6662, 0.6563), E = c(0.9541, 
                                                                                                                                                                                                                                                                                                                                                    0.9512, 0.9472, 0.9416, 0.9385, 0.9521, 0.9347, 0.9478, 0.9432,0.9364)), 
                    row.names = c(NA, 10L), class = "data.frame")

colnames(my.table)<-c("Dates","B","C","D","E")

library(shiny)
ui <- fluidPage(


    titlePanel("Old Faithful Geyser Data"),


    sidebarLayout(
        sidebarPanel(
            selectInput("first", "Letters:", 
                        choices = c("ALL",colnames(my.table)))),


        mainPanel(
            tableOutput("ratios")) 
    )
)

server <- function(input, output) {

    output$ratios <- renderTable({

        matrix.my<-cbind(as.character(my.table$Dates),as.character(my.table[,input$first]))
        #colnames(matrix)<-c("Dates","")
        matrix.my

        # if (input$first = "ALL") {
        #     my.table<-my.table
        # }
        # 


    })
}

shinyApp(ui = ui, server = server)

Это то, что я уже сделал:

Я пытаюсь использовать «если», чтобы поместить всю матрицу или матрицусвязан с выбранным входом.

Где я иду не так?

Также, как мне поставить имя столбца матрицы, чтобы оно было эквивалентно выбранному входу?

1 Ответ

1 голос
/ 01 ноября 2019

Один из способов сделать это - задать вектор имен столбцов на входе. Как то так:

server <- function(input, output) {

  # this line will make dates show up as date format in the output
  my.table$Dates <- as.character(as.Date(my.table$Dates, origin='1970-01-01'))

  output$ratios <- renderTable({
    if (input$first == 'ALL'){
      cols <- c("B", "C", "D", "E")
    }else{
      cols <- input$first
    }
    # not using cbind anymore, as it drops column names
    my.table[, c('Dates', cols)]
  })
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...