簡體   English   中英

我是Shiny的新手,我想使用Iris數據集(這是R中的一個包)將一個簡單的應用程序放在一起:

[英]I am new to shiny and I am trying to put a simple app together using the iris data set, which is a package in R:

我收到以下錯誤:

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 )

    })
 })

您使用的名稱不合適。 如果您使用諸如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"]] )

  })
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM