【问题标题】:Numeric input don't work in apriori in R shiny数字输入在 R 闪亮中的先验中不起作用
【发布时间】:2016-05-03 18:48:08
【问题描述】:

我在 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 没有显示任何散点图。为什么它不起作用? 请帮帮我。

【问题讨论】:

    标签: r shiny numeric interactive


    【解决方案1】:

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

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

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

    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)
    

    【讨论】:

      【解决方案2】:

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

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

      top.support &lt;- 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
      

      (我用于先验输入的数据集称为“d”)

      我必须对代码做什么?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-11
        • 2021-09-29
        • 1970-01-01
        • 1970-01-01
        • 2018-08-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多