【问题标题】:shiny plot doesn't show up闪亮的情节没有出现
【发布时间】:2019-06-19 13:07:44
【问题描述】:

我正在尝试制作我的第一个闪亮的应用程序,其中接受了许多值,执行了许多计算(这取决于输入中的值和服务器函数中的值),然后绘制的输出。但是,我可以在输出中根本没有绘图(如下面的示例所示),或者只能获得 1:1 线而不显示我的数据。我不完全确定从哪里开始进行故障排除,但我认为我在进行计算和将它们输入到绘图函数中都有问题。如果您有任何指点,将不胜感激。

这是我的应用程序的简化版本:

library(shiny)
require(ggplot2)

ui<-fluidPage(
  sidebarLayout(
    sidebarPanel(
      titlePanel("mytitle"),
      sliderInput(inputId= "min", label="minratio", value=0, min=0, max=0.499),
      sliderInput(inputId= "max", label="maxratio", value=1, min=0.5, max=1)
    ),
    mainPanel(
      textOutput("valoutput"),
      plotOutput("distPlot",width="100%"))
  )
)

server<-function(input, output){
  BS = function(x) {
    mini=x[1]; maxi=x[2]
    ratio <-seq(from=mini,to=maxi, by=0.01) 
    total<-30*ratio+3
    res = c(ratio,total)
  }
  data<-reactive({as.data.frame("mini"=as.numeric(input$min), "maxi"=as.numeric(input$max))})

  output$valoutput <- renderText({BS(data())[1]})
  output$distplot <- renderPlot({
    d1=BS(data())[1]
    d2=BS(data())[2]
       ggplot()+geom_abline(intercept = 0, slope=1, colour="grey50")+geom_point(aes(x=d1, y=d2))
  }, height = 350, width = 600)

}

shinyApp(ui=ui, server=server)

非常感谢!

【问题讨论】:

  • }, height = 350, width = 600) 这里为什么有}
  • 那是renderPlot的右括号。

标签: r shiny


【解决方案1】:

您的 BS 函数不正确。用这个改变它(用as.numeric)。否则 x[1]/x[2] 将是 data.frames 并且会在seq() 中抛出错误。或者,您也可以使用双括号,例如 x[[1]]。

  BS = function(x) {
    mini=x[1]; maxi=x[2]
    ratio <-seq(from=as.numeric(mini),to=as.numeric(maxi), by=0.01) 
    total<-30*ratio+3
    res = c(ratio,total)
  }

在您的用户界面中,您的绘图输出名称不正确。应该是distplot 而不是distPlot。

而且您不需要调用as.data.frame,只需data.frame 做正确的工作,因为您想创建一个data.frame 而不是转换一个对象。

  data <- reactive({
    data.frame("mini"=as.numeric(input$min), 
               "maxi"=as.numeric(input$max))
    })

【讨论】:

  • 非常感谢您的帮助!这解决了问题。它在我的绘图上只给了我一个点,我期望 100(mini 和 maxi 之间的每个值一个),但我通过创建一个循环来从 plot 命令中的 BS() 函数生成新数据来解决这个问题。跨度>
  • 我认为您不需要 for 循环,但我不知道您要绘制什么。
  • 谢谢 - 是的 for 循环似乎没有必要,而且肯定非常慢。我正在尝试绘制比率与总比率,它应该有 100 个 x-y 对用于绘图。但是,使用我放在这里的代码,图中只出现了一个点。
猜你喜欢
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 2021-05-10
  • 2014-09-12
相关资源
最近更新 更多