problem with selecting variables/columns using radioButtons and selectInput in Shiny

非 Y 不嫁゛ 提交于 2019-12-13 03:35:20

问题


I'm unable to select/unselect different columns of mtcars dataset using both radioButtons and selectInput function in Shiny. Can someone please help me out as i'm stuck on it since last 2 days. I shall be extremely grateful.

Regards

data(mtcars)
#Ui
ui <- fluidPage(
sidebarLayout( 
 sidebarPanel(
  column(width = 10,
         radioButtons(inputId="variables", label="Select variables:",
                      choices = c("All","mpg","cyl","disp"),
                      selected = "All", inline = TRUE )),

  column(width = 10,
         selectInput(inputId = "level", label = "Choose Variables to 
                     display", multiple = TRUE, choices =  names(mtcars)[4:11]))),


mainPanel ( 
  h2("mtcars Dashboard"),
  DT::dataTableOutput("table"))))


#server
server<-function(input, output) {

output$table <- DT::renderDataTable(DT::datatable(filter='top', editable = TRUE, caption = 'mtcars',
                                                {  

                                    data <- mtcars
                                    data<-data[,input$variables,drop=FALSE]

                                      column = names(mtcars)
                                      if (!is.null(input$level)) {
                                          column = input$level  }

                                       data

                                                })) }
shinyApp(ui = ui, server = server)

回答1:


library(shiny)
library(DT)
data(mtcars)
#Ui
ui <- fluidPage(
sidebarLayout( 
  sidebarPanel(
      column(width = 10,
      radioButtons(inputId="variables", label="Select variables:",
      choices = c("All","mpg","cyl","disp"),
      selected = "All", inline = TRUE )),
      column(width = 10,
      selectInput(inputId = "level", label = "Choose Variables to 
      display", multiple = TRUE, choices =  names(mtcars)[4:11]))),
      mainPanel ( 
      h2("mtcars Dashboard"),
      DT::dataTableOutput("table"))
  ))

#server
server<-function(input, output, session) {
  data <- mtcars
  tbl <- reactive({
    if(input$variables=='All'){
      data
    }else{
      data[,c(input$variables,input$level),drop=FALSE]
    }
  })
output$table <- DT::renderDataTable(DT::datatable(filter='top', caption='mtcars', tbl()))  
}
shinyApp(ui = ui, server = server)

Here is what I understand from your requirements, I hope it what you are looking for. Always try to avoid calculations inside render*.



来源:https://stackoverflow.com/questions/52961740/problem-with-selecting-variables-columns-using-radiobuttons-and-selectinput-in-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!