上传后没有显示输出的闪亮read.transactions

时间:2017-03-15 15:46:17

标签: r shiny rstudio reactive-programming

我的代码根本不工作,上传文件后没有错误。 ui.r

library(shiny)

shinyUI(fluidPage(

  titlePanel("APRIORI"),

  sidebarLayout(
    sidebarPanel(
      h2("Davin", align = "center", style = "color:blue"),
      fileInput('gg', 'Choose CSV File', accept=c('.csv')),
      plotOutput("imagegro")

    ),

    mainPanel(
      dataTableOutput("apriori"),
      plotOutput("tops")
    )
  )
))

服务器server.r

library(shiny)

shinyServer(function(input, output) {

  library(arules)
  output$tops <- renderPrint({
  dong <- input$gg

  if (is.null(dong)) return(NULL)

  # grocer <- read.csv(CSSSV$datapath)
  groceries <- read.transactions(dong$datapath, sep = ",")

  # groceries <- read.transactions("jual.csv", sep = ",")
  # default settings result in zero rules learned
  apriori(groceries)

  # set better support and confidence levels to learn more rules
  groceryrules <- apriori(groceries, parameter = list(support =
                                                        0.2, confidence = 0.76))
  # writing the rules to a CSV file
  write(groceryrules, file = "groceryrules.csv",
        sep = ",", quote = TRUE, row.names = FALSE)

  # converting the rule set to a data frame
  groceryrules_df <- as(groceryrules, "data.frame")
  output$imagegro <- renderPlot(image(groceries))
  topconfidence <- sort(groceryrules, decreasing = TRUE, na.last = NA, by = "confidence")
  topconfidence_df <- as(topconfidence, "data.frame")
  output$apriori <- renderDataTable(topconfidence_df)
  })

})

Running in Web

Rstudio, no error. no respond

是闪亮的上传不支持read.transactions? 有人可以告诉上传后使用read.transactions的代码... 我的档案jual.csv

broccoli,corn,green pepper
asparagus,corn,squash
bean,corn,squash,tomato
bean,corn,green pepper,tomato
asparagus,bean,broccoli
asparagus,bean,squash,tomato
corn,tomato
broccoli,green pepper,tomato
asparagus,bean,squash
bean,corn
bean,broccoli,green pepper,squash
asparagus,bean,squash
asparagus,bean,corn,squash
bean,broccoli,corn,green pepper,tomato

请帮助......

1 个答案:

答案 0 :(得分:1)

我认为你需要研究Shiny是如何工作的。它是一种被动编程语言,与传统编程非常不同。您基本上必须设置一组相互依赖的节点,并在需要时相互更新。一个好的开始是Joe Cheng(Shiny的作者)演讲 - 这里是一个介绍Intro to Shiny,但他也有一些更新的更高级的。

无论如何,这可能就是你要找的东西:

library(shiny)
library(arules)

u <- shinyUI(fluidPage(
  titlePanel("APRIORI"),
  sidebarLayout(
    sidebarPanel(
      h2("Davin", align = "center", style = "color:blue"),
      fileInput('gg', 'Choose CSV File', accept=c('.csv')),
      plotOutput("imagegro")
    ),
    mainPanel(
      dataTableOutput("apriori"),
      verbatimTextOutput("aprioritxt")
    )
)))
s <- shinyServer(function(input, output) {
  groceries <- reactive({
    req(input$gg)
    read.transactions(input$gg$datapath, sep = ",")
  })
  groceryrules <- reactive({
    grules <- apriori(groceries(), parameter = list(support = 0.2, confidence = 0.76))
    write(grules, file = "groceryrules.csv", sep = ",", quote=TRUE, row.names=FALSE)
    grules
  })
  groceryrules_df <- reactive({
    as(groceryrules(), "data.frame")
  })
  topconfidence_df <- reactive({
    topconfidence <- sort(groceryrules(), decreasing=TRUE, na.last=NA, by="confidence")
    topconfidence_df <- as(topconfidence, "data.frame")
  })
  output$aprioritxt <- renderPrint({  print(apriori(groceries()))  })
  output$imagegro <- renderPlot({image(groceries())})
  output$apriori <- renderDataTable({topconfidence_df()})
})
# options(shiny.reactlog = TRUE) # see visualizer notes below
shinyApp(u,s)

看起来像这样:

enter image description here

可视化节点可能会有所帮助 - 这就是Shiny反应式日志可视化工具,它可以显示日志回放结束时显示的节点:

enter image description here

有关使用此工具的说明,请访问:Reactive Log Visualizer

关于此工具的说明:

  • 启用options(shiny.reactlog = TRUE)调用
  • 之前插入的行shinyApp
  • 它会记录日志如何相互激活的日志。
  • 然后可以使用Ctrl-F3(或Command-F3)
  • 启用日志可视化工具
  • 然后,您可以使用箭头键在日志中向前移动(基本上您在日志中移动)。
  • 此处解释了反应符号:Shiny Reactivity Overview
  • 它暴露了许多对用户不可见的反应活动,因此可能会造成混淆。
  • 它在大型Shiny应用程序上效果不佳 - 此工具的许多方面根本无法扩展。