如何正确输出闪亮的 Plotly 图?
How to correctly output Plotly plots in shiny?
我正在尝试根据纽约犯罪历史数据制作一款闪亮的应用程序。我正在使用单个闪亮页面方法。这是数据:https://data.world/data-society/nyc-crime-data
出于某种原因,当我 select 输出犯罪统计数据的年份时,我的输出仅在 RStudio 的查看器中输出,而不在 Shiny 弹出窗口的主面板上输出。完整代码如下:
# Shiny App exploring New York City Crime Data between 2006-2016
# Data Source: https://data.world/data-society/nyc-crime-data
#########################Global Data######################
# Data Reading
set.seed(123)
library("shiny")
library("lubridate")
library("plotly")
nypd<-read.csv("NYPD_Complaint_Data_Historic.csv")
#Data Massaging
nypd$year<-year(as.Date(nypd$RPT_DT,'%m/%d/%Y'))
nypd$month<-month(as.Date(nypd$RPT_DT,'%m/%d/%Y'))
nypd<-nypd[nypd$OFNS_DESC != "",]
nypd2<-nypd[,c(1,6,8,14,16,17,22,23,25,26)]
ui<-fluidPage(
titlePanel("New York City Crime Data from 2006-2016"),
sidebarLayout(
sidebarPanel(
sliderInput("year","Year of Crime",min=2006,max=2016,value=2008,step = 1)
),
mainPanel(plotOutput("crimeplot"))
)
)
server<-function(input,output){
output$crimeplot<-renderPlot({
nypd_yr_sorted<-nypd2[nypd2$year==input$year,]
agg_data<- aggregate(nypd_yr_sorted$CMPLNT_NUM,by=list(nypd_yr_sorted$OFNS_DESC),FUN=functi on(x)length(unique(x)))
colnames(agg_data)<-c("Crime","Crime count")
bar_data<-agg_data[order(agg_data$`Crime count`, decreasing = TRUE),][1:5,]
plot_ly(bar_data,x=~Crime,y=~`Crime count`,type="bar",color = ~Crime) %>% layout(xaxis= list(showticklabels = FALSE))
})
}
shinyApp(ui = ui, server = server)
Plotly 图表使用 plotlyOutput()
和 renderPlotly()
呈现。需要对代码进行两处更改:
- 将
mainPanel()
更改为mainPanel(plotlyOutput("crimeplot"))
- 将
output$crimeplot
更改为output$crimeplot<-renderPlotly({
...输出:
我正在尝试根据纽约犯罪历史数据制作一款闪亮的应用程序。我正在使用单个闪亮页面方法。这是数据:https://data.world/data-society/nyc-crime-data
出于某种原因,当我 select 输出犯罪统计数据的年份时,我的输出仅在 RStudio 的查看器中输出,而不在 Shiny 弹出窗口的主面板上输出。完整代码如下:
# Shiny App exploring New York City Crime Data between 2006-2016
# Data Source: https://data.world/data-society/nyc-crime-data
#########################Global Data######################
# Data Reading
set.seed(123)
library("shiny")
library("lubridate")
library("plotly")
nypd<-read.csv("NYPD_Complaint_Data_Historic.csv")
#Data Massaging
nypd$year<-year(as.Date(nypd$RPT_DT,'%m/%d/%Y'))
nypd$month<-month(as.Date(nypd$RPT_DT,'%m/%d/%Y'))
nypd<-nypd[nypd$OFNS_DESC != "",]
nypd2<-nypd[,c(1,6,8,14,16,17,22,23,25,26)]
ui<-fluidPage(
titlePanel("New York City Crime Data from 2006-2016"),
sidebarLayout(
sidebarPanel(
sliderInput("year","Year of Crime",min=2006,max=2016,value=2008,step = 1)
),
mainPanel(plotOutput("crimeplot"))
)
)
server<-function(input,output){
output$crimeplot<-renderPlot({
nypd_yr_sorted<-nypd2[nypd2$year==input$year,]
agg_data<- aggregate(nypd_yr_sorted$CMPLNT_NUM,by=list(nypd_yr_sorted$OFNS_DESC),FUN=functi on(x)length(unique(x)))
colnames(agg_data)<-c("Crime","Crime count")
bar_data<-agg_data[order(agg_data$`Crime count`, decreasing = TRUE),][1:5,]
plot_ly(bar_data,x=~Crime,y=~`Crime count`,type="bar",color = ~Crime) %>% layout(xaxis= list(showticklabels = FALSE))
})
}
shinyApp(ui = ui, server = server)
Plotly 图表使用 plotlyOutput()
和 renderPlotly()
呈现。需要对代码进行两处更改:
- 将
mainPanel()
更改为mainPanel(plotlyOutput("crimeplot"))
- 将
output$crimeplot
更改为output$crimeplot<-renderPlotly({
...输出: