无法从ui.R访问Server.R中的对象

时间:2016-08-20 04:56:37

标签: r shiny rstudio shinydashboard

我无法访问表格对象" chartMatrix"来自 server.R 中的 ui.R 。运行时的输出是:找不到对象。我认为它无法访问server.R中的对象。 此外,如果可以访问该对象,我将如何根据滑块访问每个行元素。请帮我。

ui.R文件:

# shinydashboard makes it easy to use Shiny to create dashboards
# shinydashboard requires Shiny 0.11 or above
#First Selecting the shiny Dashboard
library(shiny)
library(shinydashboard)
library(openxlsx)
library(ggplot2)

FileNames <- list.files("ExcelSheets/")
countDays <- length(FileNames)

positive = 0
neutral = 0
negative = 0
countTweets = 0

positiveTweets = ""
negativeTweets = ""
neutralTweets = ""

p = 1
nu = 1
ng = 1


for (i in seq(1, length(FileNames)))
{
  excelSheetData = read.xlsx(paste0("ExcelSheets/", FileNames[i]), startRow = 0, colNames = TRUE, detectDates = TRUE)
  countRows <- dim(excelSheetData)
  countRows <- countRows[1]

  rows <- countRows
  countTweets = countTweets + rows
  data = excelSheetData[, c("polarity", "polarity_confidence", "Text")]
  positiveCount = 0
  negativeCount = 0 
  neutralCount = 0

  for (j in seq(1, rows))
  {
    if(data[j, 1] == "positive")
    {
      positive = positive + data[j, 2]
      positiveTweets = paste0(positiveTweets, paste0(paste(paste0(p, ":"), data[j,3]), "<br><br>"))
      positiveCount = positiveCount + 1
      p = p + 1
    }
    else if(data[j, 1] == "negative")
    {
      negative = negative + data[j, 2]
      negativeTweets = paste0(negativeTweets, paste0(paste(paste0(ng, ":"), data[j,3]), "<br><br>"))
      negativeCount = negativeCount + 1
      ng = ng + 1
    }
    else if(data[j, 1] == "neutral")
    {
      neutral = neutral + data[j, 2]
      neutralTweets = paste0(neutralTweets, paste0(paste(paste0(nu, ":"), data[j,3]), "<br><br>"))
      neutralCount = neutralCount + 1
      nu = nu + 1
    }
  }

  if(!exists("chartMatrix"))
    chartMatrix <- c(positiveCount, neutralCount, negativeCount)
  else
    chartMatrix <- c(chartMatrix, c(positiveCount, neutralCount, negativeCount))
}
total <- (positive + negative) + neutral

positivePercent <- round((positive * 100) / total)
negativePercent <- round((negative * 100) / total)
neutralPercent <- round((neutral * 100) / total)

countVect = c(positive, neutral, negative)

chartMatrix <- matrix(chartMatrix, ncol=3, byrow=TRUE)
colnames(chartMatrix) <- c("Positive", "Neutral", "Negative")

chartMatrix <- as.table(chartMatrix)


shinyUI(dashboardPage(
  dashboardHeader(title = "Sentiment Analysis"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
      menuItem("Tweets", icon = icon("twitter"),
               menuSubItem("Positive Tweets", tabName = "pTweets", icon = icon("thumbs-up")),
               menuSubItem("Neutral Tweets", tabName = "neuTweets", icon = icon("hand-spock-o")),
               menuSubItem("Negative Tweets", tabName = "negTweets", icon = icon("thumbs-down"))
      ),
      menuItem("Charts", tabName = "Charts", icon = icon("twitter"))
    )
  ),
  ## Body content
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "dashboard",
              div(class = "titleText", h2(HTML("<center><b>Sentiment Analysis</b> of Twitter Tweets using <i>RapidMinor</i> and <i>Shiny Dashboard</i>.</center>"))),
              div(class = "Author", h5(HTML("<center>- By Rushabh Wadkar & Ankit Agarwal.</center><br>"))),
              fluidRow(
                valueBox(countTweets, "Total Number of Tweets Analyzed in the competition", icon = icon("twitter"), width = 6),
                valueBox(countDays, "Number of Days ", icon = icon("calendar-check-o"), width = 6, color = "yellow")
              ),
              fluidRow(
                infoBox("Positive", paste(positivePercent, "%"), icon = icon("thumbs-up"), width = 4, fill = TRUE, color = "green"),
                infoBox("Neutral", paste(neutralPercent, "%"), icon = icon("hand-spock-o"), width = 4, fill = TRUE, color = "light-blue"),
                infoBox("Negative", paste(negativePercent, "%"), icon = icon("thumbs-down"), width = 4, fill = TRUE, color = "red")
              )
      ),

      # Positive Tweets tab content
      tabItem(tabName = "pTweets",
              h2(HTML("<center><b><span style='color: green'>Positive</span> Tweets <i>#Brexit</i></b></center>")),
              h4(HTML(positiveTweets))
      ),
      # Neutral Tweets tab content
      tabItem(tabName = "neuTweets",
              h2(HTML("<center><b><span style='color: blue'>Neutral</span> Tweets <i>#Brexit</i></b></center>")),
              h4(HTML(neutralTweets))
      ),
      # Negative Tweets tab content
      tabItem(tabName = "negTweets",
              h2(HTML("<center><b><span style='color: red'>Negative</span> Tweets <i>#Brexit</i></b></center>")),
              h4(HTML(negativeTweets))
      ),

      #charts
      tabItem(tabName = "Charts",
              h2(HTML("<center><b><span style='color: orange; font-size: 40px;'>C</span>harts</b></center>")),
              HTML("<hr style='border-width: 3px; border-color: black'>"),
              br(),
              br(),
              br(),
              fluidRow(
                column(3, offset = 1,
                       h4(HTML("<b>Select the Date</b>")),
                       #sliderInput('dateSlider', 'Dates',min=1, max=nrow(dataset), value=min(1000, nrow(dataset)), step=500, round=0),
                       #sliderInput('dateSlider', 'Dates', min=1, max = nrow())
                       br(),
                       sliderInput(inputId = 'dateSlider', "Date Slider", 09, 20, 09, step=1, animate=TRUE),
                       br(),
                       tableOutput('tableChart')
                )
              )
      )
    )
  )
))

Server.R文件:

# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#


library(shiny)
library(shinydashboard)

shinyServer(function(input, output) {

    output$tableChart <- renderTable(chartMatrix)
})

1 个答案:

答案 0 :(得分:0)

无法在server.Rui.R之间直接访问R对象。有3个选项:

  1. 如果您的对象在整个会话期间没有变化,那么您可以放置 他们在global.R
  2. 如果他们根据用户输入进行更改,您可以使用input$output$函数
  3. 通过renderObjectobjectOutput来回传递它们
  4. 使用相同的电话在server.Rui.R内单独创建。
  5. 我很少使用第三种选择,因为它不是很优雅,但可以胜任。