R闪亮的动作按钮影响反应控制

  • 本文关键字:影响 控制 按钮 r shiny
  • 更新时间 :
  • 英文 :


我想绘制一个步骤数据的直方图,该直方图在应用程序首次运行时显示,并且该位正在工作。箱子的变化和绘图密度的颜色也是如此。我想包括的一个功能是,用户可以单击actionButton或其他一些操作切换,在直方图上覆盖dnorm行。打开或关闭会更好。

然而,在这一点上,我只能得到一次绘制覆盖线的代码。我尝试在这里使用"隔离"函数,但无法在代码中找到实现。

理想情况下,绘图看起来像(忽略绘图控制的精细点)用户点击"切换"来添加或删除曲线:

w<-rnorm(1000) 
hist(w,col="red",freq=F,xlim=c(-5,5))
curve(dnorm,-5,5,add=T,col="blue")

ui.R

    library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
  # Application title
  titlePanel("Data Products - Final Project"),
  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      helpText("Select some of the features of the histogram."),
      sliderInput("bins", label = h4("Number of bins: ")
                  , min = 5
                  , max = 50
                  , value = 10),
      radioButtons("radio-color", helpText("Select a color for density plot."),
                   choices = list("Salmon" = "salmon", "Black" = "black"
                                  ,"Red" = "red", "Dark Blue" = "darkblue"
                                  , "Dark Grey" = "darkgrey")
                   ,selected = "salmon"),
      actionButton("hist-dnorm", label = "Add Curve")
    ),#end sideBarPanel
    # Show a plot of the generated distribution
    mainPanel(
      h1("Lorem Ipsum", align = "left"),
      p("Some paragraph text", align = "left"),
      plotOutput("histPlot"),
      plotOutput("histLine")
    )#End mainPanel
    )#End sidebarLayout
  )#End fluidPage
)#End ShinyUI

服务器.R

希望您能看到我的曲线的scratch代码,因为我不知道如何在代码中实现它们,所以它的元素目前已被注释掉。它们是(m,s,xfit,yfit,yfit2和hline)

library(shiny)
library(caret)
library(ggplot2)
library(dplyr)
library(lubridate)
dat <- read.csv("data/fitbit_data.csv", stringsAsFactors = FALSE)
dat$Day <- weekdays(x = as.Date(dat$Date, "%m/%d/%Y", label = TRUE, abbr = FALSE))
dat$Steps <- as.numeric(sub(",","",dat$Steps))
dat$Steps[dat$Steps == 0 & is.numeric(dat$Steps)] <- NA
dat$Calories.Burned <- as.numeric(sub(",","",dat$Calories.Burned))
dat$Calories.Burned[dat$Calories.Burned == 0 
                    & is.numeric(dat$Calories.Burned)] <- NA
dat$Minutes.Sedentary <- as.numeric(sub(",","",dat$Minutes.Sedentary))
dat$Minutes.Sedentary[dat$Minutes.Sedentary == 0 
                      & is.numeric(dat$Minutes.Sedentary)] <- NA
dat$Activity.Calories <- as.numeric(sub(",","",dat$Activity.Calories))
dat$Activity.Calories[dat$Activity.Calories == 0 
                      & is.numeric(dat$Activity.Calories)] <- NA
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
  output$histPlot <- renderPlot({
    steps <- dat$Steps
    bins <- seq(min(steps, na.rm = TRUE), max(steps, na.rm = TRUE)
                , length.out = input$bins + 1)
    h <- hist(dat$Steps, breaks = bins, density = 10, col = input$`radio-color`
         , xlim = c(500, 25000)
         , xlab = "# of Steps"
         , ylab = "Frequency"
         , main = "Histogram of Steps")
    isolate(
      output$histLine <- renderPlot({
        m <- mean(dat$Steps, na.rm = TRUE)
        s <- sqrt(var(dat$Steps, na.rm = TRUE))
        xfit <- seq(min(dat$Steps, na.rm = TRUE)
                    , max(dat$Steps, na.rm = TRUE), length = 40)
        yfit <- dnorm(xfit, mean = m, sd = s)
        yfit2 <- yfit*diff(h$mids[1:2])*length(dat$Steps)
        lines(xfit, yfit2, col = "darkblue", lwd = 2)
      })
    )
  })
   #end isolate

})#end shinyServer

问题

  1. 如何在ui中实现切换。允许用户切换的R在服务器中计算的dnorm曲线的叠加。R(打开或关闭;默认情况下关闭)
  2. 前几行代码中的所有数据子集你有什么建议吗那上面

感谢您的时间和考虑

这是一个概念验证。

library(shiny)
# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
   # Application title
   titlePanel("Toggle line"),
   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
        actionButton("button", "Toggle line")
      ),
      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
))
# Define server logic required to draw a histogram
server <- shinyServer(function(input, output) {
   w<-rnorm(1000) 
   output$distPlot <- renderPlot({
     hist(w,col="red",freq=F,xlim=c(-5,5))
     if (input$button%%2 == 0) {
      curve(dnorm,-5,5,add=T,col="blue")
     }
   })
})
# Run the application 
shinyApp(ui = ui, server = server)

最新更新