闪亮的反应情节失败

Posted

技术标签:

【中文标题】闪亮的反应情节失败【英文标题】:Shiny Reactive Plot Fail 【发布时间】:2017-06-14 21:39:50 【问题描述】:

我想根据用户选择的犯罪和年份(反应性)显示一系列(100 多个)情节

情节已经命名,格式如下:“CrimeDataA01”代表 2001 年的突击,“CrimeDataM02”代表 2002 年的谋杀,等等。

地块已经被定义并且它们的代码被粘贴在服务器文件中的 shinyServer(function(input, output) 之前。

我正在尝试让 Shiny 打印正确的情节,具体取决于用户选择的犯罪和年份。这是我的 server.r 代码:

shinyServer(function(input, output) 
crime2 <- switch(input$select,
               "Assault" = "A",
               "Burglary" = "B",
               "Car Theft" = "MT",
               "Grand Larceny" = "G",
               "Murder" = "M",
               "Rape" = "R",
               "Robbery" = "RO")
year2 <- switch(input$Slider,
               "2000" = "00",
               "2001" = "01",
               "2002" = "02",
               "2003" = "03",
               "2004" = "04",
               "2005" = "05",
               "2006" = "06",
               "2007" = "07",
               "2008" = "08",
               "2009" = "09",
               "2010" = "10",
               "2011" = "11",
               "2012" = "12",
               "2013" = "13",
               "2014" = "14",
               "2015" = "15")
output$plot <- plot(paste0("CrimeData", crime2, year2))
  )
)

这是我的 ui.R 代码:

library(shiny)
shinyUI(fluidPage(
  sidebarLayout( position = "right",
sidebarPanel(
      selectInput("select", label = h3("Select Crime"),
              choices = list("Assault" = 1, "Burglary" = 2, "Car Theft" = 3, "Grand Larceny" = 4, "Murder" = 5, "Rape" = 6, "Robbery" = 7))
),
    mainPanel(
  textOutput("text1"),
  h1("NYC Crime Over Time", align = "center"),
  sliderInput("Slider", "Year", 2000, 2015, 2000),
print(plot)
  ))))

我已经把它搞砸了,我不知道什么是上升或下降了。我只是希望它调用正确的情节并显示它

【问题讨论】:

我一回到家就试试。如果它工作 lmk 你的 venmo output$plot 重命名为output$MyPlot 并使用renderImage() 而不是plot()。然后将 print(plot) 更改为 plotOutput("MyPlot")。请参阅?renderImage中的示例 【参考方案1】:

如果我的问题是正确的,您正在寻找以下代码: 请注意,我只定义了犯罪数据 A00 和犯罪数据 A01,您似乎在本地保存了其他地块。 除了 Brandon 建议研究 renderImage() 或 renderPlot() 之外,您还应该熟悉 reactives() 和 get()。祝你好运!

plot(1:9)
CrimeDataA00 <- recordPlot()

server <- shinyServer(function(input, output) 
  crime2 <- reactive(
    switch(input$select,
           "Assault" = "A",
           "Burglary" = "B",
           "Car Theft" = "MT",
           "Grand Larceny" = "G",
           "Murder" = "M",
           "Rape" = "R",
           "Robbery" = "RO")  
  )

  year2 <- reactive(
    switch(as.character(input$Slider),
           "2000" = "00",
           "2001" = "01",
           "2002" = "02",
           "2003" = "03",
           "2004" = "04",
           "2005" = "05",
           "2006" = "06",
           "2007" = "07",
           "2008" = "08",
           "2009" = "09",
           "2010" = "10",
           "2011" = "11",
           "2012" = "12",
           "2013" = "13",
           "2014" = "14",
           "2015" = "15")  
  )

  output$plot <- renderPlot(
    get(paste0("CrimeData", crime2(), year2()))
  )
)



library(shiny)
ui <- shinyUI(fluidPage(
  sidebarLayout( position = "right",
                 sidebarPanel(
                   selectInput("select", label = h3("Select Crime"),
                               choices = list("Assault" = "Assault", "Burglary" = "Burglary", "Burglary" = "Burglary", "Grand Larceny" = "Grand Larceny", "Murder" = "Murder", "Rape" = "Rape", "Robbery" = "Robbery"))
                 ),
                 mainPanel(
                   textOutput("text1"),
                   h1("NYC Crime Over Time", align = "center"),
                   sliderInput("Slider", "Year", 2000, 2015, 2000),
                   plotOutput("plot")
                 ))))

shinyApp(ui, server)

【讨论】:

当我运行它时,它显示“2015="15"时输入意外结束” 我无法重现此错误。我将上面的代码 c&p 到一个带有空工作区的空文件中,它可以工作。也许清理你的工作区,然后再试一次。 如果你喜欢这个答案,我将不胜感激:)

以上是关于闪亮的反应情节失败的主要内容,如果未能解决你的问题,请参考以下文章

观察事件中闪亮的反应数据集

闪亮的两个情节和串扰问题

闪亮的:如何在闪亮的应用程序中添加反应栏

闪亮:更新输入而不触发反应?

R中闪亮的反应选择输入

R闪亮过滤反应输入