我有一个简单的数据,我需要为renderDatatable(不是renderTable)应用过滤器。但是我无法执行以下代码
as <- data.frame(ColA = c("India","USA","Canada","India","USA","Canada","Australia"),ColB=c("A","B","C","D","E","F","G"),ColC=c("Jan","Jan","Mar","Feb","Jan","Apr","Apr"))
library(shiny)
library(DT)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(selectInput("x","Operations",choices =
c("table"),
multiple=FALSE,selectize = TRUE),
(selectInput("ColB","Choices from ColB", choices = as$ColB,
multiple=TRUE,selectize = TRUE))),
mainPanel(h6("Here it is"),
dataTableOutput("message")
)
)
)
server <- function(input, output, session) {
r1 <- reactive({
if(input$x == "table")
{
tab <- as.data.frame.matrix(table(as$ColC, as$ColA))
tab <- tab[rownames(tab) %in% input$ColB, ]
}
})
output$message <- renderDataTable({
datatable(r1(), rownames = TRUE)
})
}
shinyApp(ui, server)
答案 0 :(得分:0)
如评论中所述,我将server
中的ColC选择设置为响应式。为此,您需要在ColC
部分中创建一个输入ui
。然后,我在ColB
中删除了数据框(server
)的第二列。
我认为您的问题的解决方案是:
as <- data.frame(ColA = c("India","USA","Canada","India","USA","Canada","Australia"),ColB=c("A","B","C","D","E","F","G"),ColC=c("Jan","Jan","Mar","Feb","Jan","Apr","Apr"))
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(selectInput("x","Operations",choices =
c("table"),
multiple=FALSE,selectize = TRUE),
(selectInput("ColC","Choices from ColC", choices = as$ColC,
multiple=TRUE,selectize = TRUE))),
mainPanel(h6("Here it is"),
dataTableOutput("message")
)
)
)
server <- function(input, output, session) {
r1 <- reactive({
if(input$x == "table")
{
tab <- as.data.frame(as[as$ColC %in% input$ColC, ])
tab <- tab[, -2]
}
})
output$message <- renderDataTable({
r1()
})
}
shinyApp(ui, server)
编辑:您需要转换数据框中的表。为此,您需要使用as.data.frame.matrix
函数,如此处所述(How to convert a table to a data frame)。最终的代码是:
as <- data.frame(ColA = c("India","USA","Canada","India","USA","Canada","Australia"),ColB=c("A","B","C","D","E","F","G"),ColC=c("Jan","Jan","Mar","Feb","Jan","Apr","Apr"))
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(selectInput("x","Operations",choices =
c("table"),
multiple=FALSE,selectize = TRUE),
(selectInput("ColC","Choices from ColC", choices = as$ColC,
multiple=TRUE,selectize = TRUE))),
mainPanel(h6("Here it is"),
dataTableOutput("message")
)
)
)
server <- function(input, output, session) {
r1 <- reactive({
if(input$x == "table")
{
tab <- as.data.frame.matrix(table(as$ColC, as$ColA))
tab <- tab[as$ColC %in% input$ColC, ]
}
})
output$message <- renderDataTable({
r1()
})
}
shinyApp(ui, server)
编辑#2:我将as$ColC
替换为rownames(tab)
。另外,我添加了library(DT)
,以便将选项rownames = TRUE
放在renderDataTable
中。这是最终的解决方案(希望如此):
as <- data.frame(ColA = c("India","USA","Canada","India","USA","Canada","Australia"),ColB=c("A","B","C","D","E","F","G"),ColC=c("Jan","Jan","Mar","Feb","Jan","Apr","Apr"))
library(shiny)
library(DT)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(selectInput("x","Operations",choices =
c("table"),
multiple=FALSE,selectize = TRUE),
(selectInput("ColC","Choices from ColC", choices = as$ColC,
multiple=TRUE,selectize = TRUE))),
mainPanel(h6("Here it is"),
dataTableOutput("message")
)
)
)
server <- function(input, output, session) {
r1 <- reactive({
if(input$x == "table")
{
tab <- as.data.frame.matrix(table(as$ColC, as$ColA))
tab <- tab[rownames(tab) %in% input$ColC, ]
}
})
output$message <- renderDataTable({
datatable(r1(), rownames = TRUE)
})
}
shinyApp(ui, server)
答案 1 :(得分:0)
如评论中所述,我将server
中的ColC选择设置为响应式。为此,您需要在ColC
部分中创建一个输入ui
。然后,我在ColB
中删除了数据框(server
)的第二列。
此外,您需要转换数据框中的表。为此,您需要按照此处(How to convert a table to a data frame)的说明使用as.data.frame.matrix
。
最后,我将as$ColC
部分中的rownames(tab)
替换为server
。另外,我添加了library(DT)
,以便将选项rownames = TRUE
放在renderDataTable
中。这是最终的解决方案:
as <- data.frame(ColA = c("India","USA","Canada","India","USA","Canada","Australia"),ColB=c("A","B","C","D","E","F","G"),ColC=c("Jan","Jan","Mar","Feb","Jan","Apr","Apr"))
library(shiny)
library(DT)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(selectInput("x","Operations",choices =
c("table"),
multiple=FALSE,selectize = TRUE),
(selectInput("ColC","Choices from ColC", choices = as$ColC,
multiple=TRUE,selectize = TRUE))),
mainPanel(h6("Here it is"),
dataTableOutput("message")
)
)
)
server <- function(input, output, session) {
r1 <- reactive({
if(input$x == "table")
{
tab <- as.data.frame.matrix(table(as$ColC, as$ColA))
tab <- tab[rownames(tab) %in% input$ColC, ]
}
})
output$message <- renderDataTable({
datatable(r1(), rownames = TRUE)
})
}
shinyApp(ui, server)