【问题标题】:I cannot seem to produce a graph我似乎无法生成图表
【发布时间】:2021-12-23 01:50:57
【问题描述】:

我正在尝试创建一个闪亮的应用程序。 我的数据集在一个名为 location 的变量中有 45 个不同的国家。我正在尝试将数据子集到每个国家/地区,同时从侧边栏面板中选择一个变量来创建散点图;当我运行应用程序时,图表不会出现。你能帮我解决我哪里出错了吗?

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)
library(plotly)
library(DT)
covid <- read.csv("D:/R/EuropeIndia.csv")
title <- tags$a(href='https://ourworldindata.org/covid-vaccinations?country=OWID_WRL',
                'COVID 19 Vaccinations')

# Define UI for application that draws a histogram
ui <- fluidPage(
  headerPanel(title = title),

    # Application title
    titlePanel("COVID vaccinations: Deaths Vs All variables"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
          selectInput("location", "1. Select a country",
                      choices = covid$location, selectize = TRUE, multiple = FALSE),
          br(),
          helpText("Select variables to plot"),
          selectInput(inputId = "y", label = "Y-axis:",
                      choices = c("total_deaths", "new_deaths"),
                      selected = "Deaths"),
          br(),
          selectInput(inputId = "x", label = "X- axis:",
                      choices = names(c(covid)),
                      selectize = TRUE,
                      selected = "Comparator variables"),
         
          br(),
          helpText("Select the Download Format"),
          radioButtons("type", "2. Format type:",
                       choices = c("Excel (csv)", "Text(tsv)", "Doc")),
          br(),
          helpText("Click on the download button to download dataset"),
          downloadButton("downloadData", "Download"),
          helpText("READ ME: Click on the title to open data source")
            
        ),

        # Show a plot of the generated distribution
        mainPanel(
          textOutput("location"),
          plotlyOutput("scatterplot"),
          tabsetPanel(
            type = "tabs",
            tabPanel("Summary of COVID data", verbatimTextOutput("summary")),
            tabPanel("Dataset", DTOutput("dataset"))
            )
            
        )
    )
)

# Define server logic 
server <- function(input, output) {
  output$location <- renderPrint({
    locationfilter <- subset(covid, covid$location == input$location)
  })
  
  output$scatterplot <- renderPlotly({
    #ggplot(subset(covid, covid$location == input$location),aes(y= input$y,x=input$x))+geom_point()
  plot_ly(subset(covid, covid$location == input$location), y= input$y,x=input$x,
          type = 'scatter', mode = "markers")
    })
  output$summary <- renderPrint({
    summary(covid)
  })
  datasetinput <- reactive({covid})
  fileExt <- reactive({
    switch(input$type,
           "Excel (csv)" = "csv", "Text (tsv)" = "tsv", "Doc" = "doc")
  })
  output$dataset <- renderDT(
    covid, options = list(
      pageLength = 50,
      initComplete = JS('function(setting, json) { alert("done"); }')
    )
  )
  
  output$downloadData <- downloadHandler(
    filename = function(){
      paste("covid", fileExt(),sep = ".")
    },
    content = function(file){
      sep <- switch(input$type,
                    "Excel (csv)" = ",", "Text (tsv)" = "\t", "Doc" = " ")
      write.table(datasetinput(), file, sep = sep, row.names = FALSE)
    }
    
  )
}

# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r graph shiny plotly scatter-plot


    【解决方案1】:

    看看plotly documentationplot_ly 要求您将变量定义为 x = ~variable_name,其中 variable_name 是符号而不是字符串,但 input$x 是字符串。

    如果你第一次使用ggplot,你可以使用.data[[]] 符号,它接受一个字符串。然后你可以使用类似的东西

    output$scatterplot <- renderPlotly({
      ggplotly(
        ggplot(subset(covid, covid$location == input$location),
               aes(y = .data[[input$y]], x = .data[[input$x]])) + geom_point()
      )
    })
    

    【讨论】:

    • 非常感谢,现在完全有道理了,图表也出现了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 2020-11-18
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多