简体   繁体   中英

How do I complete this code for Shiny App?

I am struggling in writing this shinyApp. It's main aim is to investigate on the variables of a dataset. First it produces the summary statistics on the selected variable.

In the second part; I want this app to give me the pairplot of the varibles that I have selected in the checkbox in the UI. I have used the dataset IRIS which is available to everyone, but I need the code to be adaptable to other datasets.

Can someone please help me?

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)

Maybe this little example can help you. It illustrates how to plot a normal R-Plot and a Plotly-Plot in a ShinyApp:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM