在R中的表中显示选定的变量
我只想从selectInput(我的开关)中选择表中的数据,因为现在表中显示了值和金额。我尝试使用在R中的表中显示选定的变量,r,datatable,shiny,dt,R,Datatable,Shiny,Dt,我只想从selectInput(我的开关)中选择表中的数据,因为现在表中显示了值和金额。我尝试使用DT::datatable(tab\u input1()),但它不起作用。我怎样才能改变它 我的代码: library(plotly) library(dplyr) library(shiny) library(shinyWidgets) library(readxl) library(tidyr) library(DT) df1 <- data.frame(Month = rep(mont
DT::datatable(tab\u input1())
,但它不起作用。我怎样才能改变它
我的代码:
library(plotly)
library(dplyr)
library(shiny)
library(shinyWidgets)
library(readxl)
library(tidyr)
library(DT)
df1 <- data.frame(Month = rep(month.abb[1:12],10,replace = TRUE), Product = paste0('Szafa ', rep(LETTERS[1:10], each = 12)),
Value = sample(c(0:300),120, replace = T), Amount = sample(c(1000:10000),120, replace = T),stringsAsFactors = F)
df2 <- data.frame(Month = rep(month.abb[1:12],10,replace = TRUE), Product = paste0('Fotel ', rep(LETTERS[1:10], each = 12)),
Value = sample(c(0:300),120, replace = T),Amount = sample(c(1000:10000),120, replace = T), stringsAsFactors = F)
# UI
ui <- fluidPage(
column(
6,fluidRow(column(6, selectizeInput("All", "Year: 2018", multiple = T,choices = unique(df1$Product),
options = list(maxItems = 5, placeholder = 'Choose a product:'))),
column(6, selectizeInput("All2", "Year: 2019", multiple = T,choices = unique(df2$Product),
options = list(maxItems = 5, placeholder = 'Choose a product:'))),
column(6, selectInput("y_axis1", "What you want to analyze?", choices = c("Value", "Amount")))
)),
column(
12,fluidRow(column(12, plotlyOutput('plot'),
12, DT::dataTableOutput('tbl2'))
)
)
)
# Server code
server <- function(input, output) {
tab_input1 <- reactive({
switch(input$y_axis1,
Value = "Value",
Amount = "Amount")
})
outVar <- reactive({
df1 %>%
filter(Product %in% input$All) %>%
mutate(Product = paste(Product, "2018", sep = " ")) %>%
arrange(Month) %>%
droplevels()
})
outVar2 <- reactive({
df2 %>%
filter(Product %in% input$All2) %>%
mutate(Product = paste(Product, "2019", sep = " ")) %>%
arrange(Month) %>%
droplevels()
})
output$plot <- renderPlotly({
plot_ly(data=outVar(), x=~Month, y = outVar()[,tab_input1()],
type = 'scatter', mode = 'lines', legendgroup = "1",
color = ~Product , colors = c('red','blue', 'yellow', 'green', "orange")) %>%
add_trace(data=outVar2(), x=~Month, y = outVar2()[,tab_input1()],
type = 'scatter', mode = 'lines', legendgroup = "2",
color = ~Product , colors = c('red','blue', 'yellow', 'green', "orange")) %>%
layout(legend = list(orientation = 'h'))
})
output$tbl2 <- DT::renderDataTable({
DT::datatable(rbind(outVar(),outVar2()))
#DT::datatable(tab_input1())
})
}
# Return a Shiny app object
shinyApp(ui = ui, server = server)
library(plotly)
图书馆(dplyr)
图书馆(闪亮)
图书馆(shinyWidgets)
图书馆(readxl)
图书馆(tidyr)
图书馆(DT)
df1%
安排(月)%>%
液滴()
})
输出$plot%
添加跟踪(data=outVar2(),x=~Month,y=outVar2()[,tab\u input1()],
类型='scatter',模式='lines',legendgroup=“2”,
颜色=~产品,颜色=c('红色'、'蓝色'、'黄色'、'绿色'、'橙色”))%>%
布局(图例=列表(方向='h'))
})
输出$tbl2您可以在全局中定义c(“值”、“金额”)
,以便使用它知道要保留哪一列
library(plotly)
library(dplyr)
library(shiny)
library(shinyWidgets)
library(readxl)
library(tidyr)
library(DT)
df1 <- data.frame(Month = rep(month.abb[1:12],10,replace = TRUE), Product = paste0('Szafa ', rep(LETTERS[1:10], each = 12)),
Value = sample(c(0:300),120, replace = T), Amount = sample(c(1000:10000),120, replace = T),stringsAsFactors = F)
df2 <- data.frame(Month = rep(month.abb[1:12],10,replace = TRUE), Product = paste0('Fotel ', rep(LETTERS[1:10], each = 12)),
Value = sample(c(0:300),120, replace = T),Amount = sample(c(1000:10000),120, replace = T), stringsAsFactors = F)
analyze <- c("Value", "Amount")
# UI
ui <- fluidPage(
column(
6,fluidRow(column(6, selectizeInput("All", "Year: 2018", multiple = T,choices = unique(df1$Product),
options = list(maxItems = 5, placeholder = 'Choose a product:'))),
column(6, selectizeInput("All2", "Year: 2019", multiple = T,choices = unique(df2$Product),
options = list(maxItems = 5, placeholder = 'Choose a product:'))),
column(6, selectInput("y_axis1", "What you want to analyze?", choices = analyze))
)),
column(
12,fluidRow(column(12, plotlyOutput('plot'),
12, DT::dataTableOutput('tbl2'))
)
)
)
# Server code
server <- function(input, output) {
tab_input1 <- reactive({
switch(input$y_axis1,
Value = "Value",
Amount = "Amount")
})
outVar <- reactive({
df1 %>%
filter(Product %in% input$All) %>%
mutate(Product = paste(Product, "2018", sep = " ")) %>%
arrange(Month) %>%
droplevels()
})
outVar2 <- reactive({
df2 %>%
filter(Product %in% input$All2) %>%
mutate(Product = paste(Product, "2019", sep = " ")) %>%
arrange(Month) %>%
droplevels()
})
output$plot <- renderPlotly({
plot_ly(data=outVar(), x=~Month, y = outVar()[,tab_input1()],
type = 'scatter', mode = 'lines', legendgroup = "1",
color = ~Product , colors = c('red','blue', 'yellow', 'green', "orange")) %>%
add_trace(data=outVar2(), x=~Month, y = outVar2()[,tab_input1()],
type = 'scatter', mode = 'lines', legendgroup = "2",
color = ~Product , colors = c('red','blue', 'yellow', 'green', "orange")) %>%
layout(legend = list(orientation = 'h'))
})
output$tbl2 <- DT::renderDataTable({
rbind_tab <- rbind(outVar(),outVar2())
del_column <- analyze[analyze != tab_input1()] # get which column to delete
rbind_tab[[del_column]] <- NULL
DT::datatable(rbind_tab)
#DT::datatable(tab_input1())
})
}
# Return a Shiny app object
shinyApp(ui = ui, server = server)
library(plotly)
图书馆(dplyr)
图书馆(闪亮)
图书馆(shinyWidgets)
图书馆(readxl)
图书馆(tidyr)
图书馆(DT)
df1%
液滴()
})
输出$plot%
添加跟踪(data=outVar2(),x=~Month,y=outVar2()[,tab\u input1()],
类型='scatter',模式='lines',legendgroup=“2”,
颜色=~产品,颜色=c('红色'、'蓝色'、'黄色'、'绿色'、'橙色”))%>%
布局(图例=列表(方向='h'))
})
输出$tbl2,您只需使用input$y\u axis1
而不是tab\u input1()测试开关中的更多定量变量,我得到错误:警告:我删除了一个方括号中的错误[[del\u column]]
,代码正常工作。