R闪亮无效的配方

问题描述 投票:1回答:1

我有个问题。我想使用侧边栏来限制时间序列图,但是,当我尝试绘制图时,我得到了无效的公式错误。有人可以帮忙吗?

server.r

library(shiny)
library(BCA)

data(Eggs)
# Define server logic required to plot 
shinyServer(function(input, output) {

  formulaX <- reactive({
    tmp <- paste(input$range,collapse = ":")
    paste("Eggs[",tmp,",1]")
  })
  formulaY <- reactive({
    tmp1 <- paste(input$range,collapse = ":")
    paste("Eggs[",tmp1,",5]")
  })

  # Return the formula text for printing as a caption
  output$caption <- renderText({
    paste(formulaX(),formulaY(),sep = " ")
  })

  #creating plot -ERROR
  output$mpgPlot <- renderPlot({
    plot(as.formula(formulaX()),as.formula(formulaY()))
  })
})

长子。 [R

library(shiny)

# Define UI 
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Eggs"),

  sidebarPanel(
    # Specification of range within an interval
    sliderInput("range", "Range:",
                min = 1, max = 105, value = c(20,50))
  ),

  mainPanel(
    h3(textOutput("caption")),
    plotOutput("mpgPlot")
  )
))
r shiny
1个回答
1
投票

"Eggs[1:10,1]"不是一个公式,它是一个子集的character表示。由于您总是选择第1列和第5列,因此您的“公式”始终是"Cases ~ Week"(我没有安装BCA,我认为这是正确的),并且您打算使用数据中的行子集。

也许这会起作用(仓促,一些编程保障措施是合适的):

# no need for formulaX(), formulaY()
# not certain what you want/need from output$caption
dataX <- reactive({ Eggs[input$range[1]:input$range[2],,drop = FALSE] })

和你的情节:

output$mpgPlot <- renderPlot({
  plot(Cases ~ Week, data = dataX())
})

要么

output$mpgPlot <- renderPlot({
  x <- dataX()
  plot(x$Week, x$Cases)
})
© www.soinside.com 2019 - 2024. All rights reserved.