将ui.R的闪亮变量传递给server.R

我从头开始重新发布这个消息,希望有人能够通过这个学习机会让我获得。

我在下面的Shiny应用程序中将ui.R的变量传递给server.R时遇到了问题。

我也包括global.R。 该文件的一部分会ping我的基于云的MySQL数据库。 我不想在这里分享这个密码; 您可以在这里将查询结果作为CSV文件(其中2个)。

问题在于server.R的第22行。 用代码原样( y = n.emp, ),它按预期工作。 当我用( y = input$quant, )代替它时,代码会中断。 错误在那一行。 我已经隔离了这一点。

如前所述,我试过aes_string。 它不起作用。 (也许我没有正确使用它?)

谁可以帮我这个事? 谢谢!

server.R

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  output$distPlot <- renderPlot({

### ----- MANIPULATE DATA ----- 

    colors17 <- c("#a7dfb9","#d0a0d4","#fde096","#96bbf1","#ecb489","#6eceea","#eaa99e","#8adbd3","#ddb9f1","#9cc18d","#ebaec8","#dceeb6","#b6bee4","#c5c88f","#dfb89b","#e9cf9d","#c8c09a")
    colors6 <- c("#74d5e0", "#e5b197", "#93c1ed", "#cfd6a0", "#dfb1d8", "#9adabe")

    naics_jll$market <- factor(naics_jll$m.mkt,
                          levels = as.character(MKT))
    naics_jll <- naics_jll %>%
      filter(m.mkt %in% input$markets
             # , (other), (filters), (here)
      )
### ----- PLOT -----    
    g <- ggplot(naics_jll)
    g + geom_bar(stat = "identity",
                 position = input$geom_bar_pos,
                 aes(x = m.mkt, 
                     y = n.emp,
                     fill = c1.name),
                 color = "lightgrey") +
      scale_fill_manual (values=colors17) +
#      facet_wrap(~ m.mkt) +
      labs( y = input$quant, title = "Market Structure", subtitle = "by market & industry") + 
      theme(strip.text.x = element_text(size = 8),
            axis.text.x  = element_text(angle=90, size=6))
  })
})

ui.R

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  title = "Company Data Explorer",
  plotOutput('distPlot'),
  hr(),

  fluidRow(
    column(3,
           radioButtons("geom_bar_pos", "",
                        c("Stacked Bars" = "stack",
                          "Grouped Bars" = "dodge"),selected = "dodge")
    ),
    column(4, offset = 1,
           checkboxGroupInput("markets", "Include Markets:",
                                      c("Boston" = "BOS",
                                        "NYC" = "NYC",
                                        "Chicago" = "CHI",
                                        "San Francisco" = "SF",
                                        "Los Angeles" = "LA",
                                        "Washington, DC" = "DC"),
                      selected = c("BOS","NYC","CHI","SF","LA","DC"))),
    column(4,
           selectInput('quant', 'Y-Values', names(y_vals),names(y_vals)[[4]]))
  )
))

global.R

library(shiny)
library(RNeo4j)
library(tidyverse)
library(stringr)
library(ggplot2)

### GET DATA

## MySQL SERVER CONNECT
con <- dbConnect(MySQL(),
                 user = 'shiny_apps',
                 password = '****',
                 host = 'mysql.mvabl.com',
                 dbname='sandbox191')

qmain <- dbSendQuery(con, "SELECT * FROM naics_jll;")
naics_jll <- as.data.frame(dbFetch(qmain,n=-1),na.rm=TRUE)

dbHasCompleted(qmain)
dbClearResult(qmain)
dbDisconnect(con)

## LOAD CSV
naics_jll <- select(naics_jll,-n.msa_naics,-c1.id,-q.level,-q.qtr,-q.nbrhd,-N.BldgClass)
y_vals <- subset(naics_jll,select = which(sapply(naics_jll,is.numeric)))
dropdown <- c("m.mkt","c1.name","q.nbrhd")

### "LEVELS" VARIABLES (currently unused)
IND <- naics_jll %>% distinct(c1.name)
MKT <- naics_jll %>% distinct(m.mkt)

在Joe Cheng的要求帮助下,我终于解决了这个问题。 我需要将我的数据源定义为反应性的。 猜猜这是一个需要阅读的新主题!

链接地址: http://www.djcxy.com/p/89145.html

上一篇: passing Shiny variables from ui.R to server.R

下一篇: changing the font color of the sidebar in shiny dashboard