首页 > 解决方案 > 如何为 Shiny App 完成此代码?

问题描述

我正在努力编写这个闪亮的应用程序。它的主要目的是研究数据集的变量。首先,它生成所选变量的汇总统计信息。

在第二部分;我希望这个应用程序给我我在 UI 的复选框中选择的变量的配对图。我使用了每个人都可以使用的数据集 IRIS,但我需要代码能够适应其他数据集。

有人可以帮帮我吗?

library(shiny)
library(plotly)

data(iris)

ui<-fluidPage(
  titlePanel("Iris"),
  sidebarLayout(
    sidebarPanel(
      selectInput("var",label="Choose a variable",
                  choice=list("Sepal.Length"=1, "Sepal.Width"=2, "Petal.Length"=3, "Petal.Width"=4, "Species"=5), selectize=FALSE),
      checkboxGroupInput(inputId ="independent",label = "Select independent variables", choices = names(iris)),

      mainPanel(
        verbatimTextOutput("sum"),
        plotlyOutput('plot_id_in_ui ', height = "900px")
      )
    ))
)

server<-function(input,output){
  output$sum <- renderPrint({

    summary(iris[, as.numeric(input$var)])
  })
  output$plot_id_in_ui <- renderplot( { "DON'T KNOW HOW TO WRITE THIS PART"

    pairplot(iris, varnames, type = "both", penalty.par.val = "lambda.1se",

             nvals = c(20, 20), pred.type = "response") } )

})

shinyApp(ui, server)

标签: rshinyshiny-servershiny-reactivity

解决方案


也许这个小例子可以帮助你。它说明了如何在 ShinyApp 中绘制正常的 R-Plot 和 Plotly-Plot:

library(shiny)
library(plotly)

ui <- fluidPage(
  titlePanel("Iris"),
  sidebarLayout(
    sidebarPanel(
      selectInput("var",label="Choose a variable",
                  choice=list("Sepal.Length"=1, "Sepal.Width"=2, "Petal.Length"=3, "Petal.Width"=4, "Species"=5), selectize=FALSE),
      checkboxGroupInput(inputId ="independent",label = "Select independent variables", choices = names(iris))
      ),
    mainPanel(
      verbatimTextOutput("sum"),
      plotOutput("plot"),
      plotlyOutput("plotly")
    )
  )
)

server <- function(input,output) {

  output$sum <- renderPrint({  
    summary(iris[, as.numeric(input$var)])
  })

  output$plot <- renderPlot({
    plot(iris)
  })

  output$plotly <- renderPlotly({
    plot_ly(iris) %>% 
      add_trace(x=iris$Sepal.Length, y=iris$Sepal.Width, type="scatter", mode="markers")
  })

}

shinyApp(ui, server)

推荐阅读