这是我在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)
这是我到目前为止所做的:
我正在尝试使用“ if”放置整个矩阵或与所选输入关联的矩阵。
我要去哪里错了?
我该如何将矩阵列名称与所选输入等效?
答案 0 :(得分:1)
一种执行此操作的方法是在输入上设置列名的向量。像这样:
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)]
})
}