数字输入在 R shiny 中先验无效

Numeric input don't work in apriori in R shiny

我在 ui.R 中有此代码:

tabPanel("Experiment 1",sidebarPanel(numericInput("supp", "Vložte hodnotu support", 0.0001, min = 0.0001, max = 0.8, step = 0.0001),
                        numericInput("conf", "Vložte hodnotu confidence", 0.0001, min = 0.0001, max = 0.8, step = 0.0001)),

和 server.R 中的代码:

rules.all <- apriori(d, parameter=list(support=input$supp, confidence=input$conf))

library(arulesViz)

output$scatterPlot = renderPlot(
plot(rules.all, method = 'scatterplot')
)

我有这个错误: 当我更改 numericInput 中的支持值和置信度值时,R shiny 没有显示任何散点图。为什么它不起作用? 请帮帮我。

从这几行代码中我可以看出您尝试在非反应性环境中访问来自 UI 的输入。这是不允许的,闪亮会产生错误。您必须创建一个反应性数据集(例如 rules.all),您应该在其中放置

apriori(d, parameter=list(support=input$supp, confidence=input$conf))

每次您与单个小部件交互时,数据集都会更新,然后您可以在每个 render* 函数中使用 rules.all()

访问它
plot(rules.all(), method = 'scatterplot')

由于您没有提供数据,我使用了来自 plot.rules

的参考资料中的示例数据集 Groceries
library(shiny)
library(arulesViz)

ui <- shinyUI(fluidPage(

   titlePanel(""),

     tabsetPanel(
       tabPanel("Experiment 1",
          sidebarPanel(
            # Changed values of the widgets
              numericInput("supp", "Vložte hodnotu support", 0.01, 
                           min = 0.01, max = 0.8, step = 0.01),
              numericInput("conf", "Vložte hodnotu confidence", 0.01, 
                           min = 0.01, max = 0.8, step = 0.01))
      )
     ), 
      mainPanel(
         plotOutput("scatterPlot")
     )
  )
)


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

  ## You can't access inputs from UI in a not reactive environment. 
  ## rules.all <- apriori(d, parameter=list(support=input$supp, confidence=input$conf))

  data("Groceries")

  # Create a reactive dataset which you can access in all render* functions 
  # via rules.all()
  rules.all <- reactive({
    apriori(Groceries, parameter=list(support=input$supp, confidence=input$conf))
  })

  output$scatterPlot = renderPlot({ 
    plot(rules.all(), method = 'scatterplot')
  })
})

shinyApp(ui = ui, server = server)

非常感谢...但是我必须在先验之后进行这些操作,然后在屏幕上绘制散点图:

quality(rules.all) <- round(quality(rules.all), digits=3)

top.support <- sort(rules.all, decreasing = TRUE, na.last = NA, by = "support") rules.sorted = sort(rules.all, by="lift")

subset.matrix = is.subset(rules.sorted, rules.sorted)

subset.matrix[lower.tri(subset.matrix, diag=T)] = NA

redundant = colSums(subset.matrix, na.rm=T) >= 1

rules.pruned = rules.sorted[!redundant]

rules.all = rules.pruned

rules.sorted = sort(rules.all, by="lift") 

rules.all = rules.sorted

(我输入到 apriori 的数据集称为 "d")

我必须用代码做什么?