【问题标题】:R Shiny - ui.R seems to not recognize a dataframe read by server.RR Shiny - ui.R 似乎无法识别 server.R 读取的数据帧
【发布时间】:2014-07-13 01:45:26
【问题描述】:

我有一个关于 Shiny 的问题。我将首先提供我确实花时间使用 Google 和 SO 档案,尝试了一些东西,但仍然不知何故错过了一些东西。对于任何张贴失礼,我会深表歉意,并提前感谢任何指导。

我正在尝试我认为是一项非常基本的任务,以学习 Shiny,并从 Shiny 库示例中改编代码。我将 csv 文件读入数据框 (df.shiny)。我想选择与一个设施(df.shiny$Facility 级别)相关的业务绩效数据 (ITBpct) 并将其显示在 SPC 图表中(使用 qcc)。

我的问题似乎与将来自server.R 的数据提供给ui.R 有关。我相信数据已读入数据帧(它在控制台中打印),但不适用于ui.R。我确信我只是忽略了一些东西,但还没有弄清楚。

我使用的是 Shiny 网站上注明的文件夹结构,server.R 和 ui.R 在工作目录子文件夹(“Shiny-App-1”)中,数据在该文件夹的子文件夹中(Shiny- App-1/数据)。

我插入的用于帮助跟踪错误的代码通过在控制台中打印SRV-2UI-1 运行。火狐打开。然后报错。

options(browser = "C:/Program Files (x86)/Mozilla Firefox/firefox.exe")
library(shiny)
runApp("Shiny-App-1")

server.R 代码

library(shiny)
library(qcc)
print("SRV-1")  # for debugging

df.shiny = read.csv("data/ITBDATA.csv")
print(df.shiny) # for debugging
print("SRV-2")  # for debugging


shinyServer(function(input, output, session) {
    # Combine the selected variables into a new data frame
    # assign xrow <- Facility

    print("SRV-3")  # for debugging
    selectedData <- reactive({  subset(df.shiny, Facility %in% input$xrow)  })
    print("SRV-4")  # for debugging

    output$plot1 <- renderPlot({  qcc(selectedData$ITBpct, type = 'xbar.one')  })
})

ui.R 代码

library(shiny)
print("UI-1")  # for debugging

shinyUI(pageWithSidebar(
    headerPanel('SPC Chart by Facility'),
    sidebarPanel( selectInput('xrow', 'Facility', levels(df.shiny$Facility) ) ),
    mainPanel( plotOutput('plot1') )

))

错误信息

ERROR: object 'df.shiny' not found

我可以提供数据。 (不知道如何将示例附加到此说明中。)

会话信息

> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] splines   stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] plyr_1.8.1       forecast_5.4     timeDate_3010.98 zoo_1.7-11       doBy_4.5-10     
 [6] MASS_7.3-31      survival_2.37-7  gplots_2.13.0    car_2.0-20       ggplot2_0.9.3.1 
[11] lattice_0.20-29  qcc_2.3          shiny_0.9.1 

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    问题是您在ui.R 文件中使用了df.shiny$Facility,而df.shiny 没有在那里定义。 ui 看不到server 中的所有变量,它们有其他的通信方式。

    要让它工作,您需要在服务器上构建selectInput,然后在 UI 中呈现它。在您的服务器中,添加

    shinyServer(function(input, output, session) {
        output$facilityControl <- renderUI({
            facilities <- levels(df.shiny$Facility)
            selectInput('xrow', 'Facility', facilities )
        })
    
        selectedData <- reactive({  subset(df.shiny, Facility %in% input$xrow)  })
        output$plot1 <- renderPlot({  qcc(selectedData$ITBpct, type = 'xbar.one')  })
    })
    

    然后把ui改成

    shinyUI(pageWithSidebar(
        headerPanel('SPC Chart by Facility'),
        sidebarPanel( uiOutput("facilityControl" ),
        mainPanel( plotOutput('plot1') )    
    ))
    

    【讨论】:

    • 嗨...非常感谢!这让我进入了下一步。我现在有其他问题,但我会做更多的阅读,也许会再发一篇文章。再次感谢!
    【解决方案2】:

    或者,您可以将需要由 server.Rui.R 访问的所有 R 对象放在 global.R 文件中。

    更多信息:http://shiny.rstudio.com/articles/scoping.html#global-objects

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 2017-03-06
      • 2016-12-27
      • 2014-06-05
      • 2020-12-08
      • 1970-01-01
      相关资源
      最近更新 更多