在闪亮的应用程序中使用dplyr函数和data.table

时间:2017-06-20 06:01:28

标签: r shiny data.table dplyr

我在我的闪亮应用中使用排列,过滤和选择来过滤我的数据集。 首先,我根据用户指定的列安排我的数据集。我正在过滤用户指定的行,然后我选择相关的列。

sorted<-reactive(
obstacle %>%
  arrange_(order, input$obs) %>%
  filter(Subgroup.Level == input$sublevel) %>%
  select(Economy, input$obs))

使用刚刚安排给我一个错误说:

错误:无法在第1位安排类'function'的列

在线搜索我发现我可以使用SE版本arrange_。使用它我收到错误:

错误:无法将函数转换为quosure

我对R来说相当新,所以我真的很困惑我现在应该做些什么。之前我使用子集函数来过滤数据并且它工作但它使我的应用程序非常慢。没有别的办法吗?

1 个答案:

答案 0 :(得分:0)

以下是一个带有dplyr转换和绘图工作的闪亮应用示例(因为您没有提供可重现的示例,数据,我都使用过mtcars数据集):

library(shiny)
library(dplyr)
library(data.table)
library(ggplot2)

ui= fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectizeInput(inputId= "obs", label= "Obs", 
                     choices= names(mtcars), 
                     selected= names(mtcars)[1],
                     multiple=F),
      selectizeInput(inputId= "sublevel", label= "Sublevel", 
                     choices= sort(unique(mtcars$cyl)), 
                     selected= sort(unique(mtcars$cyl))[1],
                     multiple=F)
    ),
    mainPanel(
      tableOutput("tab"),
      plotOutput('plot')
    )
  )
)

server= function(input, output,session) {

  data <- data.table(mtcars)
  print(str(data))

  sorted <- reactive({data %>% arrange_(input$obs) %>% filter(cyl == input$sublevel) %>% select_("cyl", input$obs)})

  output$tab= renderTable(sorted())

  output$plot <- renderPlot({
    ggplot(sorted(), aes_string(x="cyl", y=input$obs)) + geom_point(size=5)
  })
}

shinyApp(ui, server)

正如您所看到的,我已将数据首先转换为data.table,然后使用dplyr进行排列和过滤,我还使用str()检查了列类(并且即使使用data.table ,它们也是正确的数字,因为您的观点是它们已转换为char类型)并且没有任何问题地绘制它。