如何将此功能作为反应性闪亮应用程序工作?

时间:2017-03-20 21:32:24

标签: r shiny data-visualization

我正在尝试将我编写的函数转换为反应性闪亮应用。但是我对闪亮的经验很少。我浏览了教程并试图调整我的功能但到目前为止还没有成功。

原始函数会创建一个密度图,显示您希望在一段长度为y的DNA中观察到的突变数,如果您有z个个体,并且突变率为x。

x = 1/1000
y = 100
z = 100
mutation_counter <- function(x,y,z) {
  dist_expect_muts <- replicate(1000, {
    sum(observed_muts <- sample(0:1, (y*z), prob=c(1-x,x), replace = TRUE)>0)
  })
  dist_muts <- as.data.frame(dist_expect_muts)
  ggplot(dist_muts, aes(dist_expect_muts)) + geom_density() + xlab('Number of frameshift/gain of stop codon mutations observed')
}

我尝试将其变成一个闪亮的应用程序,我可以在其中使用x,y和z的值来查看它如何影响分布,但到目前为止它不起作用。我认为我的代码可能有几个基本的错误,因为我对闪亮的应用程序的结构还不熟悉。

不幸的是,当我运行应用程序时,我没有看到任何错误消息,因此很难调试。滑块似乎有效,但没有生成绘图。

ui.R:

library(shiny)

# Define UI for application that draws a histogram
fluidPage(

  # Application title
  titlePanel("Mutation probability"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("x", "Probability of mutation:", 
                  min=1/10000000, max=1/10, value=1/100),

      sliderInput("y", "Size of region:", 
                  min = 10, max = 1000000, value = 10000, step= 100),

      sliderInput("z", "Number of individuals:", 
                  min = 1, max = 100000, value = 100, step= 100)

    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

server.R:

library(shiny)
library(ggplot2)

# Define server logic required to draw a histogram
function(input, output) {


  output$distPlot <- renderPlot({

    x <- input$x
    y <- input$y
    z <- input$z
    dist_expect_muts <- replicate(1000, {
      sum(observed_muts <- sample(0:1, (y*z), prob=c(1-x,x), replace = TRUE)>0)
    })
    dist_muts <- as.data.frame(dist_expect_muts)


    # draw the density plot
    ggplot(dist_muts, aes(dist_expect_muts)) + geom_density() + xlab('Number of mutations')
  })

}

提前感谢您提供任何帮助或建议。

0 个答案:

没有答案