Shiny 闪亮-可以';无法显示条形图

Shiny 闪亮-可以';无法显示条形图,shiny,bar-chart,Shiny,Bar Chart,为什么不显示条形图?如果在较大的程序外运行相同的代码,将显示条形图。我得到单选按钮和图表的轮廓,但里面没有数据。我在下面介绍了服务器、用户界面和数据。我是个新手 我在多处尝试了括号、逗号和括号。我还没有找到解决办法 UI.r #UI Program library(shiny) library(shinydashboard) library(ggplot2) library(ggthemes) library(DT) # my data my_data=read.table("hack.csv

为什么不显示条形图?如果在较大的程序外运行相同的代码,将显示条形图。我得到单选按钮和图表的轮廓,但里面没有数据。我在下面介绍了服务器、用户界面和数据。我是个新手

我在多处尝试了括号、逗号和括号。我还没有找到解决办法

UI.r
#UI Program
library(shiny)
library(shinydashboard)
library(ggplot2)
library(ggthemes)
library(DT)

# my data
my_data=read.table("hack.csv", header=T, sep=",")
# changing date to categorical data 
#my_data$Year=factor(my_data$Year)

## Preparing sidebar items
sidebar <- dashboardSidebar(
  width = 300,
  sidebarMenu(
    menuItem(h3("Dashboard"), tabName = "dashbd"),
    menuItem(h3("Data"), tabName = "datafile"),
    menuItem(h3("Visualization of Data"), tabName = "graphs", 
         menuSubItem(h4("- Barplot"), tabName = "crime")    ),

    br(),
    br(),
    hr()
  )
)
## Preparing for the body items
body <- dashboardBody(
  tabItems(
    tabItem(tabName = "dashbd",
        fluidRow(
          valueBoxOutput("vbox1", width = 6),
          valueBoxOutput("vbox2", width = 6)),
        h2("Introduction",  align = "center", style = "font-family: 'times'; color:blue"),
        h3("Cyber crime damage costs to hit $6 trillion annually by 2021. It all begins and ends with cyber crime. Without it, there's nothing to cyber-defend. The cybersecurity community and major media have largely concurred on the prediction that cyber crime damages will cost the world $6 trillion annually by 2021, up from $3 trillion in 2015. This represents the greatest transfer of economic wealth in history, risks the incentives for innovation and investment, and will be more profitable than the global trade of all major illegal drugs combined"),
        fluidPage(
          fluidRow(
            column(
              h2("About this app ...", align = "center", style = "font-family: 'times'; color:blue"),
              h3("This app helps you to explore and visualize the motivation behind cyber attacks
                 I have used the database available",  a("here.",href="https://www.hackmageddon.com/about/"), 
                 style = "font-family: 'times'"),
              width = 4,
              align = "left"

            ),
            column(
              h2("How to use!", style = "font-family: 'times'; color:blue"),
              h3("This app contains multiple sections;  the database and several visual graphs. ", 
                 style = "font-family: 'times'"),              
              width = 8,
              align = "left"
            ),
            br(),
            br()
            )
        ),
        p()
    ),  
    tabItem(tabName = "datafile",
        box(title = "Motivation of Cyber Attacks in Italy",
            width = 12, 
            DT::dataTableOutput('da.tab'))  
    ),

#the select for barplot
tabItem(tabName = "crime",
        titlePanel(title = h4("Cyber Attacks in Italy by Year", align="center")),
        sidebarPanel(

          radioButtons("YEAR", "Select the Census Year",
                       choices = c("2017", "2016", "2015","2014"),
                       selected = "2017")),


        sidebarPanel(
          plotOutput("MyBar"))
    )  

  )  )

# Show a plot of the generated distribution
## Putting them together into a dashboardPage

ui <- dashboardPage( 
  skin="blue",
  # add this -> navbarMenu()
  dashboardHeader(
    title="MOTIVATION BEHIND CYBER ATTACKS IN ITALY",
    titleWidth = 550,
    tags$li(class = "dropdown"
    )
  ),
  sidebar,
  body
)

SERVER
    # Reading data set
my_data=read.table("hack.csv", header=T, sep=",")
#number of row of data
my_data$Year=as.factor(my_data$Year)
server <- function(input, output) {
  ## Information for dashboard tab 
  # Reading data set
  my_data=read.table("hack.csv", header=T, sep=",")
  #number of row of data
  my_data$Year=as.factor(my_data$Year)

  server <- function(input, output) {



## Information for data tab
# data table output


output$da.tab <- DT::renderDataTable(datatable(my_data, extensions = 'Buttons',
                                               style = "bootstrap",
                                               filter = list(position = 'top', clear = T, plain = F),
                                               options = list(pageLength = 1500, dom = 'Bfrtip', 
                                                              buttons = 
                                                                list('copy', 'print', list(
                                                                  extend = 'collection',
                                                                  buttons = c('csv', 'excel', 'pdf'), 
                                                                  text = 'Download')
                                                                )
                                               )
    )    )


  }
  ## Information for data tab
  # data table output


  output$da.tab <- DT::renderDataTable(datatable(my_data, extensions = 'Buttons',
                                             style = "bootstrap",
                                             filter = list(position = 'top', clear = T, plain = F),
                                             options = list(pageLength = 1500, dom = 'Bfrtip', 
                                                            buttons = 
                                                              list('copy', 'print', list(
                                                                extend = 'collection',
                                                                buttons = c('csv', 'excel', 'pdf'), 
                                                                text = 'Download')
                                                              )
                                             )  )  )


  #This is used to create the BarPlot
  server <- function(input,output){

    reactive_data = reactive({
  #Reading from the datbase for year selected
  selected_year = as.numeric(input$YEAR)
  return(data[data$year==selected_year,])

    })
    #outputting the bar data
    output$bar <- renderPlot({
      color <- c("blue", "red", "yellow")

      our_data <- reactive_data()

      barplot(colSums(our_data[,c("CyberCrime","CyberWar","CyberHacks")]),
          ylab="Total",
          xlab="Census Year",
          names.arg = c("CyberCrime","CyberWar","CyberHacks"),
          col = color)
            })
      }}


DATA
#This is the data for the query
Year,CyberCrime,CyberWar,CyberHacks,CyberEspionage
2017,60,45,12,16
2016,65,40,16,14
2015,55,38,10,9
2014,50,26,9,6
UI.r
#用户界面程序
图书馆(闪亮)
图书馆(shinydashboard)
图书馆(GG2)
图书馆(主题)
图书馆(DT)
#我的数据
my_data=read.table(“hack.csv”,header=T,sep=“,”)
#将日期更改为分类数据
#my_数据$Year=系数(my_数据$Year)
##准备边栏项

边栏正如奥雷尔在评论中指出的那样,您确实存在命名问题,但更令人担忧的是,您定义了嵌套的
server
函数。。。我认为这是一个糟糕的复制粘贴工作,但这里有一个工作版本。我添加了一个
shinny::validate
,以确保它没有在没有数据时尝试绘制条形图

library(shiny)
library(shinydashboard)
library(ggplot2)
library(ggthemes)
library(DT)

my_data <- read.table(text = "
Year,CyberCrime,CyberWar,CyberHacks,CyberEspionage
2017,60,45,12,16
2016,65,40,16,14
2015,55,38,10,9
2014,50,26,9,6", sep = ",", header = TRUE)


## Preparing sidebar items
sidebar <- dashboardSidebar(
  width = 300,
  sidebarMenu(
    menuItem(h3("Dashboard"), tabName = "dashbd"),
    menuItem(h3("Data"), tabName = "datafile"),
    menuItem(h3("Visualization of Data"), tabName = "graphs", 
             menuSubItem(h4("- Barplot"), tabName = "crime")    ),

    br(),
    br(),
    hr()
  )
)
## Preparing for the body items
body <- dashboardBody(
  tabItems(
    tabItem(tabName = "dashbd",
            fluidRow(
              valueBoxOutput("vbox1", width = 6),
              valueBoxOutput("vbox2", width = 6)),
            h2("Introduction",  align = "center", style = "font-family: 'times'; color:blue"),
            h3("Cyber crime damage costs to hit $6 trillion annually by 2021. It all begins and ends with cyber crime. Without it, there's nothing to cyber-defend. The cybersecurity community and major media have largely concurred on the prediction that cyber crime damages will cost the world $6 trillion annually by 2021, up from $3 trillion in 2015. This represents the greatest transfer of economic wealth in history, risks the incentives for innovation and investment, and will be more profitable than the global trade of all major illegal drugs combined"),
            fluidPage(
              fluidRow(
                column(
                  h2("About this app ...", align = "center", style = "font-family: 'times'; color:blue"),
                  h3("This app helps you to explore and visualize the motivation behind cyber attacks
                     I have used the database available",  a("here.",href="https://www.hackmageddon.com/about/"), 
                     style = "font-family: 'times'"),
                  width = 4,
                  align = "left"

                ),
                column(
                  h2("How to use!", style = "font-family: 'times'; color:blue"),
                  h3("This app contains multiple sections;  the database and several visual graphs. ", 
                     style = "font-family: 'times'"),              
                  width = 8,
                  align = "left"
                ),
                br(),
                br()
                )
            ),
            p()
    ),  
    tabItem(tabName = "datafile",
            box(title = "Motivation of Cyber Attacks in Italy",
                width = 12, 
                DT::dataTableOutput('da.tab'))  
    ),

    #the select for barplot
    tabItem(tabName = "crime",
            titlePanel(title = h4("Cyber Attacks in Italy by Year", align="center")),
            sidebarPanel(

              radioButtons("YEAR", "Select the Census Year",
                           choices = c("2017", "2016", "2015","2014"),
                           selected = "2017")),


            sidebarPanel(
              plotOutput("MyBar"))
    )  

  )  )

# Show a plot of the generated distribution
## Putting them together into a dashboardPage

ui <- dashboardPage( 
  skin="blue",
  # add this -> navbarMenu()
  dashboardHeader(
    title="MOTIVATION BEHIND CYBER ATTACKS IN ITALY",
    titleWidth = 550,
    tags$li(class = "dropdown"
    )
  ),
  sidebar,
  body
)


server <- function(input, output) {

  output$da.tab <- DT::renderDataTable(
    datatable(
      data = my_data, 
      extensions = 'Buttons',
      style = "bootstrap",
      filter = list(position = 'top', clear = T, plain = F),
      options = list(
        pageLength = 1500,
        dom = 'Bfrtip', 
        buttons = list(
          'copy', 
          'print',
          list(
            extend = 'collection',
            buttons = c('csv', 'excel', 'pdf'), 
            text = 'Download')
          ) #/ buttonList
        ) #/ options 
      ) #/ datatable
    ) #/ renderDataTable

  reactive_data = reactive({
    #Reading from the datbase for year selected
    my_data[my_data$Year == input$YEAR,]

  })

  #outputting the bar data
  output$MyBar <- renderPlot({
    color <- c("blue", "red", "yellow")

    our_data <- reactive_data()

    shiny::validate(
      need(nrow(our_data) > 0, "No data for that year!")
    )

    barplot(colSums(our_data[,c("CyberCrime","CyberWar","CyberHacks")]),
            ylab="Total",
            xlab="Census Year",
            names.arg = c("CyberCrime","CyberWar","CyberHacks"),
            col = color)
  })

}

shinyApp(ui, server)
库(闪亮)
图书馆(shinydashboard)
图书馆(GG2)
图书馆(主题)
图书馆(DT)
my_data@mlegge的答案很好(应该是公认的答案)——嵌套的服务器功能是主要问题。但您可以进一步简化服务器功能。由于
renderPlot
是一个反应式环境,因此可以将调用
renderPlot
中的数据子集如下:

output$MyBar <- renderPlot({
  our_data <- my_data[my_data$Year==input$YEAR,]
  color <- c("blue", "red", "yellow")

  barplot(colSums(our_data[,c("CyberCrime","CyberWar","CyberHacks")]),
          ylab="Total",
          xlab="Census Year",
          names.arg = c("CyberCrime","CyberWar","CyberHacks"),
          col = color)
})

output$MyBar命名是否不一致?
output$bar
plotOutput(“MyBar”)
中的不同ID不是这样的-我可能在尝试不同的方法解决问题时混淆了名称。当我进行匹配时,条形图不会显示。我不知道为什么不。非常感谢你们两位——没有你们我是做不到的@@如果这解决了您的问题,请务必接受此答案(单击复选标记),以向其他用户显示这解决了您的问题。