如何导入 2 个数据集,让它们在我的环境中持久存在,并将其中一个列名称作为参数传递到我的模块化绘图函数中?
How can I import 2 datasets, have them persist in my environment, and pass one of the column names as a parameter in my modular plot function?
问题
我已将我的代码修改为一个文件,而不是组织在多个文件中。
我相信通过调用我的数据帧我无法在另一个模块中再次调用它们
某种原因,我不确定为什么。此外,在调用我的 plotFactorOfValue_server 模块时,我试图在导入硬编码为参数的列名称之前获取已知的
。
我修改了这个模块里面的ggplot
使用 mtcars 数据框(使用权重因子作为 y 变量)
1. My mod_plotFactorOfValue_server function does not recognize my
dataset and does not see my parameter (which is a column name in
the dataset)
2. Are my datasetComparables <- mod_import_server("import_1") and
datasetWholeHood <- mod_import_server("import_2") reactive objects
when called like this? Or will they only exist while being called?
3. Is there just a better way to do this? I don't want to have the user selecting the x
variable (that would mean many selectors for each plot(calling plot module 7 times for
different column names). I want to keep this modular, I have tried this without modules,
and the code is way too long and cumbersome.
代码 - 模块 - UI - 服务器
Modules in order for importing data, exporting data table,
and plotting with ggplot (which is where I am having trouble).
mod_import_ui <- function(id){
ns <- NS(id)
tagList(
fileInput(ns("file1"), label = "Choose CSV File", accept = ".csv")
#, checkboxInput(ns("header"), label = "Header", TRUE)
)
}
mod_import_server <- function(id){
moduleServer( id, function(input, output, session){
ns <- session$ns
dtreact <- reactive({
file <- input$file1
if(is.null(file))
return(NULL)
read.csv(file$datapath,
# header = input$header
)
})
# Return the reactive that yields the data frame
return(dtreact)
})
}
```
Module for displaying imported data as a table, this used the dataframe datasetComparables or datasetWholeHood when called.
```
mod_importedDataTable_ui <- function(id){
ns <- NS(id)
tagList(
DTOutput(ns("contents"))
)
}
#' importedDataTable Server Functions----
#'
#' @noRd
mod_importedDataTable_server <- function(id, dataset){
moduleServer( id, function(input, output, session){
ns <- session$ns
output$contents <- renderDT({
req(dataset())
df1 <- dataset()
return(datatable(df1))
})
})
}
```
A shiny Module that uses ggplot to plot a parameter(factorOfValue) from an imported
dataset.The user should NOT be selecting the factor to be plotted.
```
mod_plotFactorOfValue_ui <- function(id){
ns <- NS(id)
tagList(
plotOutput(ns("plotFactorOfValue"))
)
}
NEED HELP HERE CREATE THE FACTOROFVALUE VARIABLE TO PASS THROUGH AS PARAMETER IN THIS
FUNCTION
mod_plotFactorOfValue_server <- function(id, dataset, factorOfValue){
moduleServer( id, function(input, output, session){
ns <- session$ns
output$plotFactorOfValue <- renderPlot({
req(dataset())
mtdf <- dataset()
x <- mtdf[[factorOfValue]]
df2 <- dataset() %>%
ggplot(aes(x, mpg))+
geom_point(aes(color = mpg, size = 1,))+
geom_smooth(method = lm, se = F)+
theme( axis.line = element_line(colour = "darkblue",
size = 1, linetype = "solid"))
return(plot(df2))
})
})
}
```
UI and Server Sections of App
==============
```
ui <- fluidPage(theme = shinytheme("darkly"),
navbarPage(
theme = "cerulean",
"Market Analysis Tool",
# Import Tab----
tabPanel("Import",
sidebarPanel(
tags$h3("Input Comparables Data:"),
mod_import_ui("import_1"),
tags$h3("Input Whole Hood Data:"),
mod_import_ui("import_2")
),
mainPanel(
mod_importedDataTable_ui("importedDataTable_1"),
mod_importedDataTable_ui("importedDataTable_2")
), #main panel Import
), #tab panel import
# Comparables Graphs Tab----
tabPanel("Comparables Graphs",
sidebarPanel(
tags$h3("Check out these trends!"),
),
mainPanel(
mod_plotFactorOfValue_ui("plotFactorOfValue_1")
), #main panel Comparables Graphs
)
) #navbar page
) #fluid page
server <- function(input, output, session) {
####Import the Data----
datasetComparables <- mod_import_server("import_1")
datasetWholeHood <- mod_import_server("import_2")
#### Output the Data Tables----
mod_importedDataTable_server("importedDataTable_1", dataset = dtreact)
mod_importedDataTable_server("importedDataTable_2", dataset = datasetWholeHood)
######## STARTING THE PLOTS HERE----
```
#I am unable to get the dataframe to be recognized, I am also unable to get the
xvariable(factorOfValue) hardcoded as a parameter in my call function.
# Can you please help with this? THis is still part of the server section.
```
mod_plotFactorOfValue_server("plotFactorOfValue_1", dataset = datasetComparables,
factorOfValue = "SqFtTotal")
}
shinyApp(ui = ui, server = server)
```
您不需要绘制 ggplot 对象。试试这个
library(shinythemes)
library(DT)
mod_import_ui <- function(id){
ns <- NS(id)
tagList(
fileInput(ns("file1"), label = "Choose CSV File", accept = ".csv")
#, checkboxInput(ns("header"), label = "Header", TRUE)
)
}
mod_import_server <- function(id){
moduleServer( id, function(input, output, session){
ns <- session$ns
dtreact <- reactive({
file <- input$file1
if(is.null(file))
return(NULL)
read.csv(file$datapath
# header = input$header
)
})
# Return the reactive that yields the data frame
return(dtreact)
})
}
### Module for displaying imported data as a table, this used the dataframe datasetComparables or datasetWholeHood when called.
mod_importedDataTable_ui <- function(id){
ns <- NS(id)
tagList(
DTOutput(ns("contents"))
)
}
mod_importedDataTable_server <- function(id, dataset){
moduleServer( id, function(input, output, session){
ns <- session$ns
output$contents <- renderDT({
req(dataset())
df1 <- dataset()
return(datatable(df1))
})
})
}
# A shiny Module that uses ggplot to plot a parameter(factorOfValue) from an imported
# dataset.The user should NOT be selecting the factor to be plotted.
mod_plotFactorOfValue_ui <- function(id){
ns <- NS(id)
tagList(
plotOutput(ns("plotFactorOfValue"))
)
}
### NEED HELP HERE CREATE THE FACTOROFVALUE VARIABLE TO PASS THROUGH AS PARAMETER IN THIS FUNCTION
mod_plotFactorOfValue_server <- function(id, dataset, factorOfValue){
moduleServer( id, function(input, output, session){
ns <- session$ns
output$plotFactorOfValue <- renderPlot({
req(dataset())
mtdf <- dataset()
x <- mtdf[[factorOfValue]]
df2 <- dataset() %>%
ggplot(aes(x, mpg)) +
geom_point(aes(color = mpg, size = 1))+
geom_smooth(method = lm, se = F)+
theme( axis.line = element_line(colour = "darkblue", size = 1, linetype = "solid"))
return(df2)
})
})
}
# UI and Server Sections of App
ui <- fluidPage(theme = shinytheme("darkly"),
navbarPage(
theme = "cerulean",
"Market Analysis Tool",
# Import Tab----
tabPanel("Import",
sidebarPanel(
tags$h3("Input Comparables Data:"),
mod_import_ui("import_1"),
tags$h3("Input Whole Hood Data:"),
mod_import_ui("import_2")
),
mainPanel(
mod_importedDataTable_ui("importedDataTable_1"),
mod_importedDataTable_ui("importedDataTable_2")
), #main panel Import
), #tab panel import
# Comparables Graphs Tab----
tabPanel("Comparables Graphs",
sidebarPanel(
tags$h3("Check out these trends!")
),
mainPanel(
mod_plotFactorOfValue_ui("plotFactorOfValue_1")
), #main panel Comparables Graphs
)
) #navbar page
) #fluid page
server <- function(input, output, session) {
####Import the Data----
datasetComparables <- mod_import_server("import_1")
datasetWholeHood <- mod_import_server("import_2")
#### Output the Data Tables----
mod_importedDataTable_server("importedDataTable_1", dataset = datasetComparables)
mod_importedDataTable_server("importedDataTable_2", dataset = datasetWholeHood)
######## STARTING THE PLOTS HERE----
# I am unable to get the dataframe to be recognized, I am also unable to get the
# xvariable(factorOfValue) hardcoded as a parameter in my call function.
# Can you please help with this? THis is still part of the server section.
mod_plotFactorOfValue_server("plotFactorOfValue_1", dataset = datasetComparables,
factorOfValue = "cyl" ) ## "SqFtTotal"
}
shinyApp(ui = ui, server = server)
问题
我已将我的代码修改为一个文件,而不是组织在多个文件中。
我相信通过调用我的数据帧我无法在另一个模块中再次调用它们
某种原因,我不确定为什么。此外,在调用我的 plotFactorOfValue_server 模块时,我试图在导入硬编码为参数的列名称之前获取已知的
。
我修改了这个模块里面的ggplot
使用 mtcars 数据框(使用权重因子作为 y 变量)
1. My mod_plotFactorOfValue_server function does not recognize my
dataset and does not see my parameter (which is a column name in
the dataset)
2. Are my datasetComparables <- mod_import_server("import_1") and
datasetWholeHood <- mod_import_server("import_2") reactive objects
when called like this? Or will they only exist while being called?
3. Is there just a better way to do this? I don't want to have the user selecting the x
variable (that would mean many selectors for each plot(calling plot module 7 times for
different column names). I want to keep this modular, I have tried this without modules,
and the code is way too long and cumbersome.
代码 - 模块 - UI - 服务器
Modules in order for importing data, exporting data table,
and plotting with ggplot (which is where I am having trouble).
mod_import_ui <- function(id){
ns <- NS(id)
tagList(
fileInput(ns("file1"), label = "Choose CSV File", accept = ".csv")
#, checkboxInput(ns("header"), label = "Header", TRUE)
)
}
mod_import_server <- function(id){
moduleServer( id, function(input, output, session){
ns <- session$ns
dtreact <- reactive({
file <- input$file1
if(is.null(file))
return(NULL)
read.csv(file$datapath,
# header = input$header
)
})
# Return the reactive that yields the data frame
return(dtreact)
})
}
```
Module for displaying imported data as a table, this used the dataframe datasetComparables or datasetWholeHood when called.
```
mod_importedDataTable_ui <- function(id){
ns <- NS(id)
tagList(
DTOutput(ns("contents"))
)
}
#' importedDataTable Server Functions----
#'
#' @noRd
mod_importedDataTable_server <- function(id, dataset){
moduleServer( id, function(input, output, session){
ns <- session$ns
output$contents <- renderDT({
req(dataset())
df1 <- dataset()
return(datatable(df1))
})
})
}
```
A shiny Module that uses ggplot to plot a parameter(factorOfValue) from an imported
dataset.The user should NOT be selecting the factor to be plotted.
```
mod_plotFactorOfValue_ui <- function(id){
ns <- NS(id)
tagList(
plotOutput(ns("plotFactorOfValue"))
)
}
NEED HELP HERE CREATE THE FACTOROFVALUE VARIABLE TO PASS THROUGH AS PARAMETER IN THIS
FUNCTION
mod_plotFactorOfValue_server <- function(id, dataset, factorOfValue){
moduleServer( id, function(input, output, session){
ns <- session$ns
output$plotFactorOfValue <- renderPlot({
req(dataset())
mtdf <- dataset()
x <- mtdf[[factorOfValue]]
df2 <- dataset() %>%
ggplot(aes(x, mpg))+
geom_point(aes(color = mpg, size = 1,))+
geom_smooth(method = lm, se = F)+
theme( axis.line = element_line(colour = "darkblue",
size = 1, linetype = "solid"))
return(plot(df2))
})
})
}
```
UI and Server Sections of App
==============
```
ui <- fluidPage(theme = shinytheme("darkly"),
navbarPage(
theme = "cerulean",
"Market Analysis Tool",
# Import Tab----
tabPanel("Import",
sidebarPanel(
tags$h3("Input Comparables Data:"),
mod_import_ui("import_1"),
tags$h3("Input Whole Hood Data:"),
mod_import_ui("import_2")
),
mainPanel(
mod_importedDataTable_ui("importedDataTable_1"),
mod_importedDataTable_ui("importedDataTable_2")
), #main panel Import
), #tab panel import
# Comparables Graphs Tab----
tabPanel("Comparables Graphs",
sidebarPanel(
tags$h3("Check out these trends!"),
),
mainPanel(
mod_plotFactorOfValue_ui("plotFactorOfValue_1")
), #main panel Comparables Graphs
)
) #navbar page
) #fluid page
server <- function(input, output, session) {
####Import the Data----
datasetComparables <- mod_import_server("import_1")
datasetWholeHood <- mod_import_server("import_2")
#### Output the Data Tables----
mod_importedDataTable_server("importedDataTable_1", dataset = dtreact)
mod_importedDataTable_server("importedDataTable_2", dataset = datasetWholeHood)
######## STARTING THE PLOTS HERE----
```
#I am unable to get the dataframe to be recognized, I am also unable to get the
xvariable(factorOfValue) hardcoded as a parameter in my call function.
# Can you please help with this? THis is still part of the server section.
```
mod_plotFactorOfValue_server("plotFactorOfValue_1", dataset = datasetComparables,
factorOfValue = "SqFtTotal")
}
shinyApp(ui = ui, server = server)
```
您不需要绘制 ggplot 对象。试试这个
library(shinythemes)
library(DT)
mod_import_ui <- function(id){
ns <- NS(id)
tagList(
fileInput(ns("file1"), label = "Choose CSV File", accept = ".csv")
#, checkboxInput(ns("header"), label = "Header", TRUE)
)
}
mod_import_server <- function(id){
moduleServer( id, function(input, output, session){
ns <- session$ns
dtreact <- reactive({
file <- input$file1
if(is.null(file))
return(NULL)
read.csv(file$datapath
# header = input$header
)
})
# Return the reactive that yields the data frame
return(dtreact)
})
}
### Module for displaying imported data as a table, this used the dataframe datasetComparables or datasetWholeHood when called.
mod_importedDataTable_ui <- function(id){
ns <- NS(id)
tagList(
DTOutput(ns("contents"))
)
}
mod_importedDataTable_server <- function(id, dataset){
moduleServer( id, function(input, output, session){
ns <- session$ns
output$contents <- renderDT({
req(dataset())
df1 <- dataset()
return(datatable(df1))
})
})
}
# A shiny Module that uses ggplot to plot a parameter(factorOfValue) from an imported
# dataset.The user should NOT be selecting the factor to be plotted.
mod_plotFactorOfValue_ui <- function(id){
ns <- NS(id)
tagList(
plotOutput(ns("plotFactorOfValue"))
)
}
### NEED HELP HERE CREATE THE FACTOROFVALUE VARIABLE TO PASS THROUGH AS PARAMETER IN THIS FUNCTION
mod_plotFactorOfValue_server <- function(id, dataset, factorOfValue){
moduleServer( id, function(input, output, session){
ns <- session$ns
output$plotFactorOfValue <- renderPlot({
req(dataset())
mtdf <- dataset()
x <- mtdf[[factorOfValue]]
df2 <- dataset() %>%
ggplot(aes(x, mpg)) +
geom_point(aes(color = mpg, size = 1))+
geom_smooth(method = lm, se = F)+
theme( axis.line = element_line(colour = "darkblue", size = 1, linetype = "solid"))
return(df2)
})
})
}
# UI and Server Sections of App
ui <- fluidPage(theme = shinytheme("darkly"),
navbarPage(
theme = "cerulean",
"Market Analysis Tool",
# Import Tab----
tabPanel("Import",
sidebarPanel(
tags$h3("Input Comparables Data:"),
mod_import_ui("import_1"),
tags$h3("Input Whole Hood Data:"),
mod_import_ui("import_2")
),
mainPanel(
mod_importedDataTable_ui("importedDataTable_1"),
mod_importedDataTable_ui("importedDataTable_2")
), #main panel Import
), #tab panel import
# Comparables Graphs Tab----
tabPanel("Comparables Graphs",
sidebarPanel(
tags$h3("Check out these trends!")
),
mainPanel(
mod_plotFactorOfValue_ui("plotFactorOfValue_1")
), #main panel Comparables Graphs
)
) #navbar page
) #fluid page
server <- function(input, output, session) {
####Import the Data----
datasetComparables <- mod_import_server("import_1")
datasetWholeHood <- mod_import_server("import_2")
#### Output the Data Tables----
mod_importedDataTable_server("importedDataTable_1", dataset = datasetComparables)
mod_importedDataTable_server("importedDataTable_2", dataset = datasetWholeHood)
######## STARTING THE PLOTS HERE----
# I am unable to get the dataframe to be recognized, I am also unable to get the
# xvariable(factorOfValue) hardcoded as a parameter in my call function.
# Can you please help with this? THis is still part of the server section.
mod_plotFactorOfValue_server("plotFactorOfValue_1", dataset = datasetComparables,
factorOfValue = "cyl" ) ## "SqFtTotal"
}
shinyApp(ui = ui, server = server)