我似乎无法制作图表
I cannot seem to produce a graph
我正在尝试创建一个闪亮的应用程序。
我的数据集在一个名为 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)
看看plotly documentation。 plot_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()
)
})
我正在尝试创建一个闪亮的应用程序。 我的数据集在一个名为 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)
看看plotly documentation。 plot_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()
)
})