在 Reshape2 dcast() 中使用来自 Shiny 的多重输入
Using an multiple input from Shiny in Reshape2 dcast()
参考Using an input from Shiny Widget in Reshape Cast(),
我可以通过带有单行变量的闪亮输入创建动态摘要table。
但是,当我想创建多个行层列时,我发现找不到第二个或第三个对象。
我试图打印出公式,这似乎是正确的。比如在Shiny中选择了"vs"和"hp",那么公式就变成了"vs + hp ~ gear",看起来是正确的。但是出现错误"object 'hp' not found"。
你能帮我解决这个问题吗?
谢谢!
UI:
library(shiny)
library(dplyr)
library(reshape2)
library(DT)
ui <- fluidPage(
headerPanel("Summary Table"),
sidebarPanel(selectInput("row", "Row variable", names(mtcars), selected = "vs", multiple = T),
selectInput("col", "Column variable", names(mtcars), selected = "gear")),
mainPanel(DT::dataTableOutput('table'))
)
服务器:
server <- function(input, output) {
output$table <- renderDataTable({
formula <- as.formula(paste(paste(input$row, collapse = "+"), "~", input$col))
print(formula)
car0 <- mtcars %>%
group_by_(input$row, input$col) %>%
summarize(n = n_distinct(cyl)) %>%
dcast(formula, value.var = "n")
datatable(car0)
})
}
shinyApp(ui, server)
类似的东西?
library(shiny)
library(dplyr)
library(reshape2)
library(tidyr)
library(DT)
ui <- fluidPage(
headerPanel("Summary Table"),
sidebarPanel(selectInput("row", "Row variable", names(mtcars), selected = "vs", multiple = T),
selectInput("col", "Column variable", names(mtcars), selected = "gear")),
mainPanel(DT::dataTableOutput('table'))
)
server <- function(input, output) {
output$table <- renderDataTable({
car0 <- mtcars %>%
select(input$row,input$col,cyl)%>%
group_by_at(., vars(input$row, input$col))%>%
summarize(n = n_distinct(cyl))%>%
tidyr::spread(input$col,n)
datatable(car0)
})
}
shinyApp(ui, server)
公式和reshape2
library(shiny)
library(dplyr)
library(reshape2)
library(tidyr)
library(DT)
ui <- fluidPage(
headerPanel("Summary Table"),
sidebarPanel(selectInput("row", "Row variable", names(mtcars), selected = "vs", multiple = T),
selectInput("col", "Column variable", names(mtcars), selected = "gear")),
mainPanel(DT::dataTableOutput('table'))
)
server <- function(input, output) {
output$table <- renderDataTable({
formula <- as.formula(paste(paste(input$row, collapse = "+"), "~", input$col))
print(formula)
car0 <- mtcars %>%
select(input$row,input$col,cyl)%>%
group_by_at(., vars(input$row, input$col))%>%
summarize(n = n_distinct(cyl))%>%
#tidyr::spread(input$col,n)
dcast(formula, value.var = "n")
datatable(car0)
})
}
shinyApp(ui, server)
参考Using an input from Shiny Widget in Reshape Cast(), 我可以通过带有单行变量的闪亮输入创建动态摘要table。
但是,当我想创建多个行层列时,我发现找不到第二个或第三个对象。 我试图打印出公式,这似乎是正确的。比如在Shiny中选择了"vs"和"hp",那么公式就变成了"vs + hp ~ gear",看起来是正确的。但是出现错误"object 'hp' not found"。
你能帮我解决这个问题吗? 谢谢!
UI:
library(shiny)
library(dplyr)
library(reshape2)
library(DT)
ui <- fluidPage(
headerPanel("Summary Table"),
sidebarPanel(selectInput("row", "Row variable", names(mtcars), selected = "vs", multiple = T),
selectInput("col", "Column variable", names(mtcars), selected = "gear")),
mainPanel(DT::dataTableOutput('table'))
)
服务器:
server <- function(input, output) {
output$table <- renderDataTable({
formula <- as.formula(paste(paste(input$row, collapse = "+"), "~", input$col))
print(formula)
car0 <- mtcars %>%
group_by_(input$row, input$col) %>%
summarize(n = n_distinct(cyl)) %>%
dcast(formula, value.var = "n")
datatable(car0)
})
}
shinyApp(ui, server)
类似的东西?
library(shiny)
library(dplyr)
library(reshape2)
library(tidyr)
library(DT)
ui <- fluidPage(
headerPanel("Summary Table"),
sidebarPanel(selectInput("row", "Row variable", names(mtcars), selected = "vs", multiple = T),
selectInput("col", "Column variable", names(mtcars), selected = "gear")),
mainPanel(DT::dataTableOutput('table'))
)
server <- function(input, output) {
output$table <- renderDataTable({
car0 <- mtcars %>%
select(input$row,input$col,cyl)%>%
group_by_at(., vars(input$row, input$col))%>%
summarize(n = n_distinct(cyl))%>%
tidyr::spread(input$col,n)
datatable(car0)
})
}
shinyApp(ui, server)
公式和reshape2
library(shiny)
library(dplyr)
library(reshape2)
library(tidyr)
library(DT)
ui <- fluidPage(
headerPanel("Summary Table"),
sidebarPanel(selectInput("row", "Row variable", names(mtcars), selected = "vs", multiple = T),
selectInput("col", "Column variable", names(mtcars), selected = "gear")),
mainPanel(DT::dataTableOutput('table'))
)
server <- function(input, output) {
output$table <- renderDataTable({
formula <- as.formula(paste(paste(input$row, collapse = "+"), "~", input$col))
print(formula)
car0 <- mtcars %>%
select(input$row,input$col,cyl)%>%
group_by_at(., vars(input$row, input$col))%>%
summarize(n = n_distinct(cyl))%>%
#tidyr::spread(input$col,n)
dcast(formula, value.var = "n")
datatable(car0)
})
}
shinyApp(ui, server)