如何在Shiny应用程序中按输入列名排列数据表?

时间:2018-11-27 03:40:48

标签: r shiny

我试图在一周内为我的数据可视化项目编写一个应用程序,但是我似乎无法让我的应用程序提供所需的输出。该应用程序应该接受一些输入(一个人的教育程度(这对输出没有影响)和他们拥有的三种技能),并排列给定的数据表,从而使排列后的表具有最匹配的职业,该职业列在降序。

我已经向教授和TA寻求帮助,TA向我指出了arrange()函数和desc(mydata[, input$first])的作用,但这仅调用了相关列中的第一个值,而不是实际值具有该列名称的列。

感谢您的帮助。谢谢!我的代码在下面。

mydata <- iris
skillz <- names(mydata)

# Define UI for application
ui <- fluidPage(

# Application title
titlePanel("Weighted App"),

# Sidebar with little paragraph with inputs from person 
sidebarLayout(
 sidebarPanel(
  selectInput("nothing", "Hi, I am trying to get my app to work. Here are ome options" = c("GED", "Bachelor's Degree", "Master's Degree", "PhD", "Trade School Certification", "Other"), selectize = FALSE),
     selectInput("first", "I want to look at these 3 traits of irises 1:", choices = skillz, multiple = FALSE),
     selectInput("second", "2:", choices = skillz, multiple = FALSE),
     selectInput("third", "3:", choices = skillz, multiple = FALSE)
  ),
  # Show a table of the recommended occupations
  mainPanel(
     tableOutput("results")
     #verbatimTextOutput('values')
     #Professor:"Look at more examples on Shiny to see if you have an error. Think error in output here"

      )
   )
)


# Define server logic required to give weighted table
server <- function(input, output) {

       output$results <- reactive({
      # generate table based on inputs from the above 3 options
     filtered <- arrange(mydata, desc(mydata[,input$first]), desc(mydata[,input$second]), desc(mydata[,input$third])) 
     filtered

#If data table fails, maybe just print?   
#  output$values <- reactivePrint(
#   {
 #   list(x1 = input$first, x2 = input$second, x3 = input$third) 
  # } 
 #)

   })
}

# Run the application 
shinyApp(ui = ui, server = server)

同样,我认为我的错误出在我的arrange()函数中,但是我不确定如何使用我的输入名称实际调用该列。

编辑:我尝试使用deparse(),但这也只是返回“没有列'input $ first'” ...

skill1 <- deparse(input$first)
skill2 <- deparse(input$second)
skill3 <- deparse(input$third)
filtered <- arrange(mydata, desc(mydata[,skill1]), desc(mydata[,skill2]), desc(mydata[,skill3])) 
filtered

编辑2:我使数据更通用。谢谢。

1 个答案:

答案 0 :(得分:0)

通常,arrange和其他dplyr动词使用整洁的评估语义。这意味着,在使用包含列名的变量时,我们需要将字符串转换为符号,然后在评估时使用!!。下面是一个简单的示例。

data(iris)
library(magrittr)

var1 <- "Species"
var2 <- "Sepal.Length"
var3 <- "Petal.Length"

iris %>% dplyr::arrange(dplyr::desc(!!rlang::sym(var1)),
                        dplyr::desc(!!rlang::sym(var2)), 
                        dplyr::desc(!!rlang::sym(var3)))

编辑1:为可重现的示例添加了解决方案:

library(shiny)
library(magrittr)

ui <- fluidPage(
  titlePanel("Weighted App"),
# Sidebar with little paragraph with inputs from person 
  sidebarLayout(
    sidebarPanel(
      selectInput("education", , label = "education", 
          choices = c("GED", "Bachelor's Degree", "Master's Degree", 
                      "PhD", "Trade School Certification", "Other"), 
          selectize = FALSE),
      selectInput("first", "I want to look at these 3 traits of irises 
                 1:", choices = skillz, multiple = FALSE),
      selectInput("second", "2:", choices = skillz, multiple = FALSE),
      selectInput("third", "3:", choices = skillz, multiple = FALSE)
), 
# Show a table of the recommended occupations
mainPanel(
   tableOutput("results")
  )
 )
)


#Define server logic required to give weighted table
server <- function(input, output) {

  mydata <- iris
  skillz <- names(mydata)

  #Use renderTable instead of reactive
   output$results <- renderTable({
       # Arranging the rows according to the selected variables
       filtered <- mydata %>% 
            dplyr::arrange(dplyr::desc(!!rlang::sym(input$first)),
                          dplyr::desc(!!rlang::sym(input$second)), 
                          dplyr::desc(!!rlang::sym(input$third))) 
       unselectedCols <-setdiff(colnames(filtered), 
                                         unique(c(input$first,
                                                 input$second,
                                                 input$third)))

       #Reordering columns according to the selections
       filtered <- filtered %>% 
                        dplyr::select(!!rlang::sym(input$first),
                                      !!rlang::sym(input$second),
                                      !!rlang::sym(input$third), 
                                      !!unselectedCols)
       filtered
    })
 }

 #Run the application 
 shinyApp(ui = ui, server = server)
  • 在包含列名的变量上使用sym!!运算符,以将其传递给ranging函数
  • 我添加了额外的几行代码,这些行将使用dplyr::select对数据框进行重新排序,以便按选择的顺序显示列。使用!!sym
  • 类似地传递变量
  • 您可以直接使用renderTable在输出中显示反应式表,而不必使用reactiverenderTable具有反应性上下文。