【问题标题】:Not able to access object in Server.R from ui.R无法从 ui.R 访问 Server.R 中的对象
【发布时间】:2016-12-27 07:09:55
【问题描述】:

我无法从 server.R 中的 ui.R 访问表对象“chartMatrix”。运行时的输出是:找不到对象。我认为它无法访问 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)
})

【问题讨论】:

标签: r shiny rstudio shinydashboard


【解决方案1】:

server.Rui.R 之间不能直接访问R 对象。有 3 个选项:

  1. 如果你的对象在会话中没有改变,那么你可以把 他们在global.R
  2. 如果它们根据用户输入发生变化,您可以使用 renderObjectobjectOutput 函数通过 input$output$ 来回传递它们
  3. 使用相同的调用在server.Rui.R 中分别创建它们。

我很少使用第 3 个选项,因为它不是很优雅,但能胜任。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2014-07-13
    • 2020-01-20
    • 2019-10-30
    • 1970-01-01
    相关资源
    最近更新 更多