R闪亮的动作按钮来影响无功控制
我想绘制一个步骤数据的直方图,显示应用程序第一次运行时该位是否工作。 所以箱的变化和密度的颜色也是如此。 我希望包括的一个功能是用户可以单击一个actionButton或其他某个动作 - 在直方图上重叠一个dnorm-line。 打开或关闭将是可取的。
不过,我只能在这时获得一次绘制覆盖线的代码。 我尝试在这里使用'isolate'功能,但是无法弄清楚我的代码中的实现。
理想情况下,该图将看起来像(忽略绘图控制的更精细的点),其中用户单击“切换”来添加或移除曲线:
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
server.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
问题
感谢您的时间和考虑
这是一个概念证明。
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)
                        链接地址: http://www.djcxy.com/p/89141.html
                        
                        
                    