r语言 - 如何在服务器中使用反应变量 Shiny



我想创建一个如下所示的应用程序,只是在开始时选择柱面的数量。下面的例子选择一个数据库,我想放弃,直接去选择圆柱体。我可以在此处使用过滤器选项吗?

我的想法:

df_mtcars <- reactive({
cylinder_selected <- as.numeric(input$si_cylinders[1])        
df <- mtcars %>% filter(cyl == cylinder_selected)        
return(df)
})

我的代码:

用户界面:

library(radarchart)
shinyUI(pageWithSidebar(      
headerPanel("Car Comparison Radar"),      
sidebarPanel(
uiOutput("choose_dataset"),
uiOutput("choose_car"),
uiOutput("choose_columns")
),          
mainPanel(
chartJSRadarOutput('radar', height = '350px')    
)
))

服务器:

library(shiny)
library(radarchart)
shinyServer(function(input, output) {
# choose dataset but I want choose cyl
output$choose_dataset <- renderUI({
data_sets <- "mtcars"
selectInput("dataset", "Data set", data_sets)
})
# select a car
output$choose_car <- renderUI({
selectInput("car","car",as.list(rownames(get(input$dataset))))
})
# Check boxes
output$choose_columns <- renderUI({
# If missing input, return to avoid error later in function
if(is.null(input$dataset))
return()
# Get the data set with the appropriate name
dat <- get(input$dataset)
colnames <- names(dat)
# Create the checkboxes and select them all by default
checkboxGroupInput("columns", "Choose columns", 
choices  = colnames,
selected = colnames)
})            
output$radar <- renderChartJSRadar({
# Get the data set
dat <- get(input$dataset)
# Make sure columns are correct for data set (when data set changes, the
# columns will initially be for the previous data set)
if (is.null(input$columns) || !(input$columns %in% names(dat)))
return()
# Keep the selected columns
dat <- dat[, input$columns, drop = FALSE]
#reform data for plot
dat <- as.data.frame(t(dat), stringsAsFactors = FALSE)
dat$labs <- row.names(dat)
dat <- dat[, c('labs', input$car)]
chartJSRadar(dat)
})
})

因为它是uiOutput("choose_car")为您提供所有汽车的可能性。如果您添加用于选择气缸的selectInput,您将遇到问题,因为每个气缸编号都有一些特定的汽车。

因此,您可以以气缸selectInput为条件对汽车selectInput

您可以自行删除uiOutput("choose_dataset"),因此根据您的示例,您可以尝试以下操作:

ui = pageWithSidebar(      
headerPanel("Car Comparison Radar"),      
sidebarPanel(
uiOutput("choose_dataset"),
uiOutput("choose_cyclinder"),
uiOutput("choose_car"),
uiOutput("choose_columns")
),          
mainPanel(
chartJSRadarOutput('radar', height = '350px')    
)
)

server = function(input, output) {

output$choose_cyclinder <- renderUI({
temp <-  mtcars %>% group_by(cyl) %>% summarise(Counts = n()) 
cyl <- levels(as.factor(temp$cyl))
selectInput("select_cyl", "Choose a cylinder", as.list(cyl), selected=TRUE, multiple = FALSE)
})

# choose dataset but I want choose cyl
output$choose_dataset <- renderUI({
data_sets <- "mtcars"
selectInput("dataset", "Data set", data_sets)
})
# select a car
output$choose_car <- renderUI({
dat <- get(input$dataset)
dat <- dat %>% tibble::rownames_to_column('carnames') %>%
filter(cyl %in% c(input$select_cyl)) %>%
tibble::column_to_rownames('carnames')
selectInput("car","car",as.list(rownames(dat)))
})

# Check boxes
output$choose_columns <- renderUI({
# If missing input, return to avoid error later in function
if(is.null(input$dataset))
return()
# Get the data set with the appropriate name
dat <- get(input$dataset)
colnames <- names(dat)
# Create the checkboxes and select them all by default
checkboxGroupInput("columns", "Choose columns", 
choices  = colnames,
selected = colnames)
})            
output$radar <- renderChartJSRadar({
# Get the data set
dat <- get(input$dataset)
# dat <- mtcars
# Make sure columns are correct for data set (when data set changes, the
# columns will initially be for the previous data set)
if (is.null(input$columns) || !(input$columns %in% names(dat)))
return()
# Keep the selected columns
dat <- dat[, input$columns, drop = FALSE]
# dat <- dat %>% filter(cyl %in% c(input$select_cyl))
#reform data for plot
dat <- as.data.frame(t(dat), stringsAsFactors = FALSE)
dat$labs <- row.names(dat)
dat <- dat[, c('labs', input$car)]
chartJSRadar(dat)
})
}
shinyApp(ui, server)

编辑:

ui = pageWithSidebar(      
headerPanel("Car Comparison Radar"),      
sidebarPanel(
#uiOutput("choose_dataset"),
uiOutput("choose_cyclinder"),
uiOutput("choose_car"),
uiOutput("choose_columns")
),          
mainPanel(
chartJSRadarOutput('radar', height = '350px')    
)
)

server = function(input, output) {

output$choose_cyclinder <- renderUI({
temp <-  mtcars %>% group_by(cyl) %>% summarise(Counts = n()) 
cyl <- levels(as.factor(temp$cyl))
selectInput("select_cyl", "Choose a cylinder", as.list(cyl), selected=TRUE, multiple = FALSE)
})

# choose dataset but I want choose cyl
#  output$choose_dataset <- renderUI({
#    data_sets <- "mtcars"
#    selectInput("dataset", "Data set", data_sets)
#  })
# select a car
output$choose_car <- renderUI({
# dat <- get(mtcars)
dat <- mtcars
dat <- dat %>% tibble::rownames_to_column('carnames') %>%
filter(cyl %in% c(input$select_cyl)) %>%
tibble::column_to_rownames('carnames')
selectInput("car","car",as.list(rownames(dat)))
})

# Check boxes
output$choose_columns <- renderUI({
# If missing input, return to avoid error later in function
#  if(is.null(input$dataset))
#   return()
# Get the data set with the appropriate name
# dat <- get(input$dataset)
dat <- mtcars
colnames <- names(dat)
# Create the checkboxes and select them all by default
checkboxGroupInput("columns", "Choose columns", 
choices  = colnames,
selected = colnames)
})            
output$radar <- renderChartJSRadar({
# Get the data set
#dat <- get(input$dataset)
dat <- mtcars
# Make sure columns are correct for data set (when data set changes, the
# columns will initially be for the previous data set)
if (is.null(input$columns) || !(input$columns %in% names(dat)))
return()
# Keep the selected columns
dat <- dat[, input$columns, drop = FALSE]
# dat <- dat %>% filter(cyl %in% c(input$select_cyl))
#reform data for plot
dat <- as.data.frame(t(dat), stringsAsFactors = FALSE)
dat$labs <- row.names(dat)
dat <- dat[, c('labs', input$car)]
chartJSRadar(dat)
})
}
shinyApp(ui, server)

相关内容

  • 没有找到相关文章

最新更新