我一直在努力尝试掌握如何使 DT::dataTableProxy在我的应用程序中使用 DT in Shiny工作,但我无法通过返回所选列。
按照此处的示例,我尝试修改代码以打印描述性统计数据(使用pastecs)。但唯一呈现的是所选的文字列。
#modified UI, adding another verbatimTextOutput
ui =
...
verbatimTextOutput('foo2')
server =
...
output$foo2= renderPrint({
x<-input$foo_columns_selected
stat.desc(x)
})
Run Code Online (Sandbox Code Playgroud)
我想做的是获取所选列的值,然后运行该函数
stat.desc。在上面的示例中,它将在第 2 列 (Sepal.Width) 上运行,并呈现描述性统计数据。像这样:
展望未来,我希望对stat.desc来自多个 selectInput 的渲染数据表执行操作。但是..一次一步。我想首先了解如何获取实际值来执行功能。
所以,我想我知道如何成功地完成我需要的事情。如果其他用户可以验证它是否正常工作,我会很高兴:
更新的脚本:
require(DT)
library(dplyr)
library(tibble)
ui<-
fluidPage(
selectInput('obj','Choose Name:', choices = c('',my.data$Name), selectize = TRUE),
dateRangeInput('daterange',"Date range:",
start= min(my.data$Date),
end = max(my.data$Date)),
mainPanel(
dataTableOutput('filteredTable'),
dataTableOutput('filteredTable2'),
tableOutput('table')
)
)
server<-function(input,output, session){
filteredTable_data <- reactive({
my.data %>% rownames_to_column() %>% ##dplyr's awkward way to preserve rownames
filter(., Name == input$obj) %>%
filter(., between(Date ,input$daterange[1], input$daterange[2])) %>%
column_to_rownames()
})
##explicit assignment to output ID
DT::dataTableOutput("filteredTable")
output$filteredTable <- DT::renderDataTable({
datatable(
filteredTable_data(),
selection = list(mode = "multiple"),
caption = "Filtered Table (based on cyl)"
)
})
filteredTable_selected <- reactive({
ids <- input$filteredTable_rows_all
filteredTable_data()[sort(ids),] ##sort index to ensure orig df sorting
})
##anonymous
output$filteredTable2<-DT::renderDataTable({
x<-filteredTable_selected() %>% select(starts_with("Value"))
x<-as.data.frame(stat.desc(x))
datatable(
x)
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
结果:
以下答案非常有帮助:从闪亮的输出对象读取对象不允许?&如何从筛选数据表 (DT) 的选定行中获取数据?