Not able to access object in Server.R from ui.R

r shiny rstudio shinydashboard

277 просмотра

1 ответ

I am not able to access table object "chartMatrix" from ui.R in server.R. The output at run time is : Object not found. I think it is not able to access the object in server.R. Also, if it is possible to access the object, how will i be able to access each row element according to the slider. Please help me.

ui.R File :

# 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 File :

# 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)
})
Автор: Rushabh Wadkar Источник Размещён: 08.11.2019 10:54

Ответы (1)


0 плюса

R objects cannot be directly accessed between server.R and ui.R. There are 3 options:

  1. If your objects do not change across the sessions then you can put them in global.R
  2. If they change based on user input you can pass them back and forth via input$ and output$ using renderObject and objectOutput functions
  3. Create them separately within the server.R and ui.R using the same call.

I rarely use the 3rd option as it is not very elegant but does the job.

Автор: earthlink Размещён: 29.09.2016 05:38
Вопросы из категории :
32x32