简体   繁体   中英

Writing my first shiny app, How do I plot by selected variables, why does this script not work?

I'm trying to write my first shiny app that will give some statistical analysis between two variables and a plot that shows a relationship between them. My data will be predetermined. For the graph, I've decided to use highcharter but how do I write the code for this? The above code is my first try but I can't seem to figure out why it doesn't work. I've tried the example code for shiny from the highcharter guide too but it doesn't seem to work either.

library(shiny)
library(highcharter)
library(tidyverse)

data(iris)

ui <- fluidPage(

  selectInput("First", label = "First Variable", width = "100%",
              choices = colnames(iris)), 
  selectInput("Second", label = "Second", width = "100%",
              colnames(iris)),
  highchartOutput("hchartcont")

)

server = function(input, output) {

  output$hchartcont <- renderHighchart({
    hc <- highchart() %>% 
      hc_chart(type="line") %>% 
      hc_xAxis(input$First) %>% 
      hc_yAxis_multiples(input$Second)
    hc
  }

  )

}

shinyApp(ui = ui, server = server)

Although not familiar with highcharter , your code doesn't seem to provide any data to the plot. You need to get the input data first and then draw a plot.

For example, this could work (I tried to preserve as much of your code as possible... the line plot does not make sense here, however; consider this to be only a quick working example:-)):

library(highcharter)
library(tidyverse)
library(shiny)

ui <- fluidPage(

    selectInput("First", label = "First Variable", width = "100%",
                choices = colnames(iris)), 
    selectInput("Second", label = "Second", width = "100%",
                choices = colnames(iris)),
    highchartOutput("hchartcont")

)

server = function(input, output) {

    output$hchartcont <- renderHighchart({
        df <- iris %>% select(x = input$First, y = input$Second)
        hchart(df, "line", hcaes(x, y))
    })

}

shinyApp(ui = ui, server = 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