有没有办法在没有任何输入对象的情况下将 .Rdata 文件加载到 R shiny 中?
Is there a way to load an .Rdata file into R shiny without any input object?
我看到很多关于将 .Rdata 文件上传到 R shiny 的问题,但它们都包含某种输入对象。有没有办法将它上传到全球环境并在应用程序内部使用它来创建新对象?不得不说这个 .Rdata 文件是另一个闪亮应用程序的结果。
这是我试过的:
# --------------------------------------- Global --------------------------------------- #
# Set working directory -> Ctrl+Shift+H & Open the app folder
setwd("~/Programación en R/Shiny app/Final dashboard app")
# --------------------- Initialize program --------------------- #
# Print in console: global script is beginning to run
print("global.R")
# Allow specific errors to be displayed on screen, instead of displaying a generic error
options(shiny.sanitize.errors = FALSE)
# Load LDA model outcome, topic names & raw data
load("LDA_output.2019-12-28.RData")
#--------------------------------------- User Interface ---------------------------------------#
# Tell user ui script is beginning to run
print("ui.R")
ui <- fluidPage(
theme = shinytheme("cerulean"),
DT::dataTableOutput("lili")
) #fluidPage
#--------------------------------------- Server ---------------------------------------#
server <- function(input, output, session) {
mod <- reactive({get(load("LDA_output.2019-12-28.RData"))})
print(mod())
output$lili <- DT::renderDataTable({
DT::datatable(as.data.frame(mod()[[1]][3])) #list inside that list
})
}
shinyApp(ui, server)
这是由此产生的错误:
Listening on http://127.0.0.1:6282
Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
58: stop
57: .getReactiveEnvironment()$currentContext
56: getCurrentContext
55: .dependents$register
54: mod
52: server [#4]
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
这是我最后使用的服务器功能,
server <- function(input, output, session) {
observe({
print(typeof(mod))})
output$lili <- DT::renderDataTable({
DT::datatable(as.data.frame(mod[[2]]))
})
}
shinyApp(ui, server)
但还是有问题:
Listening on http://127.0.0.1:6282
[1] "list"
Warning: Error in checkName: Must use single string to index into reactivevalues
[No stack trace available]
您的错误是由于:
print(mod())
mod()
是反应性的,需要处于反应性上下文中,例如 observe
。但我不确定是否有任何理由让您的数据(从 .Rdata 加载)具有反应性。您的第一个“加载”应该使对象可供您使用。
我看到很多关于将 .Rdata 文件上传到 R shiny 的问题,但它们都包含某种输入对象。有没有办法将它上传到全球环境并在应用程序内部使用它来创建新对象?不得不说这个 .Rdata 文件是另一个闪亮应用程序的结果。 这是我试过的:
# --------------------------------------- Global --------------------------------------- #
# Set working directory -> Ctrl+Shift+H & Open the app folder
setwd("~/Programación en R/Shiny app/Final dashboard app")
# --------------------- Initialize program --------------------- #
# Print in console: global script is beginning to run
print("global.R")
# Allow specific errors to be displayed on screen, instead of displaying a generic error
options(shiny.sanitize.errors = FALSE)
# Load LDA model outcome, topic names & raw data
load("LDA_output.2019-12-28.RData")
#--------------------------------------- User Interface ---------------------------------------#
# Tell user ui script is beginning to run
print("ui.R")
ui <- fluidPage(
theme = shinytheme("cerulean"),
DT::dataTableOutput("lili")
) #fluidPage
#--------------------------------------- Server ---------------------------------------#
server <- function(input, output, session) {
mod <- reactive({get(load("LDA_output.2019-12-28.RData"))})
print(mod())
output$lili <- DT::renderDataTable({
DT::datatable(as.data.frame(mod()[[1]][3])) #list inside that list
})
}
shinyApp(ui, server)
这是由此产生的错误:
Listening on http://127.0.0.1:6282
Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
58: stop
57: .getReactiveEnvironment()$currentContext
56: getCurrentContext
55: .dependents$register
54: mod
52: server [#4]
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
这是我最后使用的服务器功能,
server <- function(input, output, session) {
observe({
print(typeof(mod))})
output$lili <- DT::renderDataTable({
DT::datatable(as.data.frame(mod[[2]]))
})
}
shinyApp(ui, server)
但还是有问题:
Listening on http://127.0.0.1:6282
[1] "list"
Warning: Error in checkName: Must use single string to index into reactivevalues
[No stack trace available]
您的错误是由于:
print(mod())
mod()
是反应性的,需要处于反应性上下文中,例如 observe
。但我不确定是否有任何理由让您的数据(从 .Rdata 加载)具有反应性。您的第一个“加载”应该使对象可供您使用。