passing Shiny variables from ui.R to server.R

I'm re-posting this from scratch in hopes someone can get me through this learning opportunity.

I'm having trouble passing a variable from ui.R to server.R in the following Shiny app.

I'm also including global.R. One section of that file pings my cloud-based MySQL db. I didn't want to share the password for that on here; you can get the query results as CSV files (2 of them) here.

The problem is with Line 22 of server.R. With the code as-is ( y = n.emp, ), it works as expected. When I replace that with ( y = input$quant, ), the code breaks. The error is in that line. I have isolated that.

I've tried aes_string, as previously suggested. It did not work. (Maybe I didn't use it properly?)

Can anyone help me on this? Thanks!

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)

I finally solved it, with help from Joe Cheng's gist. I needed to define my data source as reactive. Guess that's a new subject to read up on!!

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

上一篇: 更改闪亮应用程序的背景颜色

下一篇: 将ui.R的闪亮变量传递给server.R