在Shiny中使用radioButtons和selectInput选择变量/列的问题 [英] problem with selecting variables/columns using radioButtons and selectInput in Shiny

查看:400
本文介绍了在Shiny中使用radioButtons和selectInput选择变量/列的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Shiny中的radioButtons和selectInput函数来选择/取消选择mtcars数据集的不同列。
有人可以帮帮我吗,因为最近两天以来我一直坚持下去。
我将非常感谢。

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.

致谢

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)


推荐答案

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)

这是我的理解根据您的要求,希望它是您想要的。始终尝试避免在render *内部进行计算。

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

这篇关于在Shiny中使用radioButtons和selectInput选择变量/列的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆