首页 > 解决方案 > R 闪亮的大窗户

问题描述

你们能帮我吗,如何不需要滚动,或者让应用程序的窗口更大,这样你就可以一次看到滑块和绘图?或者那个滑动条和情节将并排。看图片

这是代码:

library(deSolve)
library(shiny)


ui <- verticalLayout(

  titlePanel("Zvolte údaje"),


  sliderInput(inputId = "time_values", label = "Počet dnů", value = 10, min = 1, max = 100),
  sliderInput(inputId = "beta", label ="Míra nákazy", value = 0.05, min = 0, max = 1, step = 0.01),
  sliderInput(inputId = "gamma", label ="Míra uzdravení", value = 0.5, min = 0, max = 1, step = 0.1),


  (plotOutput("plot"))
)

server <- function(input, output) {
  sir_equations <- function(time, variables, parameters) {
    with(as.list(c(variables, parameters)), {
      dS <- -beta * I * S
      dI <-  beta * I * S - gamma * I
      dR <-  gamma * I
      return(list(c(dS, dI, dR)))
    })
  }

  initial_values <-  c(S = 1000, I = 1, R = 0)

  sir_values_1 <- reactiveValues(val = data.frame())

  observe({
    sir_values_1$val <- as.data.frame(ode(
      y = initial_values,
      times = seq(0, input$time_values),
      func = sir_equations,
      parms = c(beta=input$beta, gamma=input$gamma) 
    ))
  })

  output$plot <- renderPlot({
    with(sir_values_1$val, {
    plot(sir_values_1$val$time, sir_values_1$val$S, type = "l", col = "blue",
         xlab = "Doba (dny)", ylab = "Počet lidí")
    lines(sir_values_1$val$time, sir_values_1$val$I, col = "red")
    lines(sir_values_1$val$time, sir_values_1$val$R, col = "green")
    legend("right", c("zdraví", "nakažení", "uzdravení"),
           col = c("blue", "red", "green"), lty = 1, bty = "n")
    })
  })
}

shinyApp(ui = ui, server = server)

谢谢

标签: rshiny

解决方案


我不熟悉,verticalLayout()但如果你使用fluidPage()你可以达到这个结果。

我还将您的代码简化为每个人都可以运行的示例。当你尽可能地简化你的代码时,你会得到更多的帮助;尽可能多地删除库,简化数据集和反应值等。

library(shiny)


ui <- fluidPage(

  titlePanel("Zvolte údaje"),
  fluidRow(
    column(width = 4,
      sliderInput(inputId = "bins", label = "Number of bins:", min = 1, max = 50, value = 30),
      sliderInput(inputId = "beta", label ="Slider 2", value = 0.05, min = 0, max = 1, step = 0.01),
      sliderInput(inputId = "gamma", label ="Slider 3", value = 0.5, min = 0, max = 1, step = 0.1)
    ),
    column(width = 8,
      plotOutput("plot")
    )
  )
)


server <- function(input, output) {

  output$plot <- renderPlot({

    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")

  })
}

shinyApp(ui = ui, server = server)

推荐阅读