Я делаю блестящее приложение R с несколькими переменными. Поскольку я в основном плохо знаком с R Блестящий, мне было интересно, может ли кто-нибудь помочь мне со следующим:
При открытии приложения Shiny работает только одна из двух моих реактивных переменных. Первый или верхний работает отлично, но другие, определенные как «Команды», не реагируют, когда я выбираю другой вариант в приложении. Я предполагаю, что я написал код сервера неправильно, кто-нибудь может мне помочь?
library(shiny)
library(AmesHousing)
library(dplyr)
library(rlang)
library(ggplot2)
library(scales)
library(tidyverse)
Alleteams <- rbind(PSVscore, Ajaxscore, FCgroningenscore, FCUscore)
deteams <- unique(Alleteams$team_thuis.x)
#' Variable selection for plot user interface
#'
#' @param id, character used to specify namespace, see \code{shiny::\link[shiny]{NS}}
#'
#' @return a \code{shiny::\link[shiny]{tagList}} containing UI elements
varselect_mod_ui <- function(id) {
ns <- NS(id)
# define choices for Y variable selection
var_choices <- list(
`Max temp` = "Max temp" ,
`Maximale uurgemiddelde windsnelheid` = "max uurgem.windsnelheid",
`% zonneschijn` = "Zonneschijn",
`Neerslag` = "Neerslag"
)
# assemble UI elements
tagList(
selectInput(
ns("yvar"),
"Select Y variable",
choices = var_choices,
selected = "Sale_Price"
)
)
}
#' Variable selection module server-side processing
#'
#' @param input,output,session standard \code{shiny} boilerplate
#'
#' @return list with following components
#' \describe{
#' \item{xvar}{reactive character indicating x variable selection}
#' \item{yvar}{reactive character indicating y variable selection}
#' }
varselect_mod_server <- function(input, output, session) {
return(
list(
yvar = reactive({ input$yvar })
)
)
}
#' Scatterplot module user interface
#'
#' @param id, character used to specify namespace, see \code{shiny::\link[shiny]{NS}}
#'
#' @return a \code{shiny::\link[shiny]{tagList}} containing UI elements
#' @export
#'
#' @examples
scatterplot_mod_ui <- function(id) {
ns <- NS(id)
tagList(
fluidRow(
column(
width = 6,
plotOutput(ns("plot1"))
),
column(
width = 6,
plotOutput(ns("plot2"))
)
)
)
}
#' Scatterplot module server-side processing
#'
#' This module produces a scatterplot with the sales price against a variable selected by the user.
#'
#' @param input,output,session standard \code{shiny} boilerplate
#' @param dataset data frame (non-reactive) with variables necessary for scatterplot
#' @param plot1_vars list containing reactive x-variable name (called `xvar`) and y-variable name (called `yvar`) for plot 1
#' @param plot2_vars list containing reactive x-variable name (called `xvar`) and y-variable name (called `yvar`) for plot 2
scatterplot_mod_server <- function(input,
output,
session,
dataset,
plot1vars,
plot2vars) {
plot1_obj <- reactive({
p <- scatter_sales(dataset, doelpunten, yvar = plot1vars$yvar())
return(p)
})
plot2_obj <- reactive({
p <- scatter_sales(dataset, doelpunten, yvar = plot2vars$yvar())
return(p)
})
output$plot1 <- renderPlot({
plot1_obj()
})
output$plot2 <- renderPlot({
plot2_obj()
})
}
#' Produce scatterplot with variables selected by the user
#'
#' @param data data frame with variables necessary for scatterplot
#' @param xvar variable (string format) to be used on x-axis
#' @param yvar variable (string format) to be used on y-axis
#'
#' @return {\code{ggplot2} object for the scatterplot
#' @export
#'
#' @examples
#' plot_obj <- scatter_sales(data = ames, xvar = "Lot_Frontage", yvar = "Sale_Price")
#' plot_obj
scatter_sales <- function(dataset, xvar, yvar) {
y <- rlang::sym(yvar)
p <- ggplot(dataset, aes(x=doelpunten, y = !!y)) +
geom_point() +
scale_x_continuous(labels = function(l) plot_labeller(l, varname = xvar)) +
scale_y_continuous(labels = function(l) plot_labeller(l, varname = yvar)) +
theme(axis.title = element_text(size = rel(1.2)),
axis.text = element_text(size = rel(1.1)))
return(p)
}
# load packages
library(shiny)
library(AmesHousing)
library(dplyr)
library(rlang)
library(ggplot2)
library(scales)
# load separate module and function scripts
source("modules.R")
source("helpers.R")
# user interface
ui <- fluidPage(
titlePanel("Ames Housing Data Explorer"),
fluidRow(
column(
width = 3,
wellPanel(
varselect_mod_ui("plot1_vars"),
selectInput(inputId = "Teams",
label = "Select team:",
choices = deteams,
selected = "PSV",
multiple=FALSE)
)
),
column(
width = 6,
scatterplot_mod_ui("plots")
),
column(
width = 3,
wellPanel(
varselect_mod_ui("plot2_vars"),
selectInput(inputId = "Teams",
label = "Select team:",
choices = deteams,
selected = "AFC Ajax",
multiple=FALSE)
)
)
)
)
# server logic
server <- function(input, output, session) {
**#HERE IT PROBABLY GOES WRONG - PLEASE HELP**
# prepare dataset
Alleteams_selected <- reactive({
#Make sure the input element is required
req(input$Teams)
#filter the dataset on the chosen city
Alleteams %>%
filter(team_thuis.x %in% input$Teams) %>%
select(c(Datum,team_thuis.x,team_uit,doelpunt1,doelpunten,`max uurgem.windsnelheid`,`Max temp`,Zonneschijn,Neerslag))
})
# execute plot variable selection modules
plot1vars <- callModule(varselect_mod_server, "plot1_vars")
plot2vars <- callModule(varselect_mod_server, "plot2_vars")
# execute scatterplot module
res <- callModule(scatterplot_mod_server,
"plots",
dataset = Alleteams_selected(),
plot1vars = plot1vars,
plot2vars = plot2vars)
}
# Run the application