我是新手,我正在尝试使用虹膜数据集将一个简单的应用程序放在一起,这是R中的一个包:

时间:2015-02-06 10:13:51

标签: r shiny shiny-server

我收到以下错误:

Error in $.shinyoutput(*tmp*, X) : 
  Reading objects from shinyoutput object not allowed

使用以下脚本时。 ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("Dynamic user interface-RenderUI"),
  sidebarLayout(
    sidebarPanel(
      selectInput("data", "Select the Database of your choice",
                  c("iris"="Iris","mtcars"="mt","trees"="tree")),
      br(),
      helpText("The folowing SelectInput drop down choices are dynamically polulated based on dataset selected"),
      br(),
      uiOutput("X-Axis"),#X-Axis is coming from renderui inserver
      br(),
      uiOutput("Y-Axis")#Y-Axis is coming from renderui inserver
    ),
       mainPanel(
      plotOutput("p")
    )
  )
))

和server.R

library(shiny)
shinyServer(function(input, output) {
  var <-reactive({
    switch(input$data,
    "iris"=names(iris),
    "mtcars"=names(mtcars),
    "trees"=names(trees)
    )

  })

  output$X-Axis <- renderUI({
            selectInput("x-axis", "Select the X-Axis variable",choices = var())


  })


  output$Y-Axis <- renderUI({

    selectInput("y-axis", "Select the Y-Axis variable",choices = var())


  })
    output$p <- renderPlot({

    attach(get(input$data))

    plot(x=get(input$x-axis),y=get(input$y-axis),xlab =input$x-axis,ylab = input$y-axis )

    })
 })

1 个答案:

答案 0 :(得分:1)

您使用的名称不合适。如果您使用x-axis等名称,则需要将其称为input$'x-axis'或更简单input[["x-axis"]]。在selectInput中,您的名字是您的对象,反之亦然。

# UI.r

library(shiny)
shinyUI(fluidPage(
  titlePanel("Dynamic user interface-RenderUI"),
  sidebarLayout(
    sidebarPanel(
      selectInput("data", "Select the Database of your choice",
                  c("Iris"="iris","mt"="mtcars","tree"="trees")),
      br(),
      helpText("The folowing SelectInput drop down choices are dynamically polulated based on dataset selected"),
      br(),
      uiOutput("X-Axis"),#X-Axis is coming from renderui inserver
      br(),
      uiOutput("Y-Axis")#Y-Axis is coming from renderui inserver
    ),
    mainPanel(
      plotOutput("p")
    )
  )
))

server.R

library(shiny)
shinyServer(function(input, output) {
  var <-reactive({
    switch(input$data,
           "iris"=names(iris),
           "mtcars"=names(mtcars),
           "trees"=names(trees)
    )

  })

  output[["X-Axis"]] <- renderUI({
    selectInput("x-axis", "Select the X-Axis variable",choices = var())


  })


  output[["Y-Axis"]] <- renderUI({

    selectInput("y-axis", "Select the Y-Axis variable",choices = var())


  })
  output$p <- renderPlot({

    attach(get(input$data))

    plot(x=get(input[["x-axis"]]),y=get(input[["y-axis"]]),xlab =input[["x-axis"]],ylab = input[["y-axis"]] )

  })
})