【发布时间】:2016-12-30 15:34:57
【问题描述】:
我有一个更大的数据表输出,其中包含不同数量的列,这是我在小部件中选择的。我想动态右对齐我的列,但只有在列数固定时才找到解决方案。我希望我可以调整 target= 命令中的引用以使其动态化。不知何故,这不起作用,当列数小于默认引用时,我没有得到输出。我在某处读到反应式语句不适用于数据表选项。我附上了 MWE。
rm(list=ls())
library(shiny)
library(datasets)
library(datatable)
DT<-data.table(matrix(abs(rnorm(100,sd=100000)),nrow=10))
server<-shinyServer(function(input, output) {
# Return the requested dataset
columns <- reactive({
switch(input$columns,
all= c("V1","V2","V3","V4","V5","V6","V7","V8","V9","V10"),
left= c("V1","V2","V3","V4","V5"),
right= c("V6","V7","V8","V9","V10"))
})
# Show table
output$view <- DT::renderDataTable(
format(DT[,.SD,.SDcols=columns()],digits = 0,scientific=F),
option=list(columnDefs=list(list(targets=0:(length(columns())-1), class="dt-right")))
)
})
library(shiny)
# Define UI for dataset viewer application
ui<-shinyUI(fluidPage(
# Application title
titlePanel("Shiny Text"),
# Sidebar with controls to select a dataset and specify the
# number of observations to view
sidebarLayout(
sidebarPanel(
selectInput("columns", label = h3("Select Columns"),
choices = list("All columns" = "all", "Left side" = "left",
"Right side" = "right"), selected = "all")
),
# Show a summary of the dataset and an HTML table with the
# requested number of observations
mainPanel(
DT::dataTableOutput("view")
)
)
))
runApp(list(ui=ui,server=server))
【问题讨论】: