首页 > 解决方案 > 加载的数据在服务器功能中不可用

问题描述

我需要帮助我已经看到类似的问题得到了回答,但我就是无法正确回答,这让我发疯了。

所以这是问题所在,当我使用 ctr + a 运行应用程序时,一切都很好,但是当我单击运行应用程序按钮时,出现以下错误

Error in hasGroups(choices) : object 'my_data' not found

另外,如果我只是在单个文件中运行脚本(不是真正可行的原始文件太大)它可以工作这是脚本 代码

library("tidyverse")
library("readxl")
library("writexl")
library("shiny")

write_xlsx(x = iris, path = "app/IRIS.xlsx")
my_data2 <- read_excel(path = "app/IRIS.xlsx")

source("app/pre_process.R")
my_data <- rename(my_data2)

# comment source and uncomment ui and server and it will work fine
source("app/ui.R")
source("app/server.R")

# ui <- pageWithSidebar(
#   headerPanel('my_data k-means clustering'),
#   sidebarPanel(
#     selectInput('xcol', 'X Variable', names(my_data)),
#     selectInput('ycol', 'Y Variable', names(my_data),
#                 selected=names(my_data)[[2]]),
#     numericInput('clusters', 'Cluster count', 3,
#                  min = 1, max = 9)
#   ),
#   mainPanel(
#     plotOutput('plot1')
#   )
# )

# server <- function(input, output, session) {
#   
#   # Combine the selected variables into a new data frame
#   selectedData <- reactive({
#     my_data %>% select(input$xcol, input$ycol)
#   })
#   
#   clusters <- reactive({
#     kmeans(selectedData(), input$clusters)
#   })
#   
#   output$plot1 <- renderPlot({
#     palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
#               "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))
#     
#     par(mar = c(5.1, 4.1, 0, 1))
#     plot(selectedData(),
#          col = clusters()$cluster,
#          pch = 20, cex = 3)
#     points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
#   })
#   
# }
servidor <- server
shinyApp(ui, server)

预处理

rename <- function(my_data) {
  names(my_data) <- c("tallo_l", "tallo_w", "petalo_l", "petalo_w", "especie")
  return(my_data)
}

这是

Github 存储库

https://github.com/alfonsonoguer/pre-process-app

一般来说,我只是不知道我必须在哪里编写代码才能将其加载到闪亮的环境中。

感谢您的时间和关注,这对您有很大帮助。

标签: rshiny

解决方案


这是 R 查找 ui 和服务器文件的位置(即在哪个环境中)的问题:

如果您local=TRUE在采购时添加参数ui.Rserver.R则应用程序应在使用“运行应用程序”按钮运行时和从脚本以交互方式运行时运行。这相当于将两个文件的内容直接打印到脚本中。

另请参阅此处的解释,特别是“包含的 R 文件的范围”部分。


推荐阅读