我有一个关于Shiny的问题.我将通过提供我确实花时间与谷歌和SO档案,尝试了一些事情,但仍然不知何故错过了一些东西.我会为任何发布失礼而道歉,并提前感谢任何指导.
我正在尝试我认为是一项非常基本的任务,以便学习Shiny,从一个Shiny gallery示例中调整代码.我将csv文件读入dataframe(df.shiny).我想选择与一个设施(级别)相关的业务绩效数据(ITBpct)df.shiny$Facility并将其显示在SPC图表中(使用qcc).
我的问题似乎与使数据server.R可用有关ui.R.我相信数据被读入数据帧(它在控制台中打印),但不可用ui.R.我确信我只是忽视了一些东西,但还没有想出来.
我正在使用Shiny站点上提到的文件夹结构,其中server.R和ui.R位于工作目录子文件夹("Shiny-App-1")中,并且子文件夹中的数据到此文件夹(Shiny-App-1) /数据).
我为帮助跟踪错误而插入的代码通过打印SRV-2和UI-1控制台运行.Firefox打开.然后是错误.
options(browser = "C:/Program Files (x86)/Mozilla Firefox/firefox.exe")
library(shiny)
runApp("Shiny-App-1")
Run Code Online (Sandbox Code Playgroud)
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') })
})
Run Code Online (Sandbox Code Playgroud)
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') )
))
Run Code Online (Sandbox Code Playgroud)
错误信息
ERROR: object 'df.shiny' not found
Run Code Online (Sandbox Code Playgroud)
我可以提供数据.(不确定如何将样本附加到此笔记.)
会话信息
> 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
Run Code Online (Sandbox Code Playgroud)
MrF*_*ick 12
问题是您df.shiny$Facility在ui.R文件中使用并且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') })
})
Run Code Online (Sandbox Code Playgroud)
然后将ui更改为
shinyUI(pageWithSidebar(
headerPanel('SPC Chart by Facility'),
sidebarPanel( uiOutput("facilityControl" ),
mainPanel( plotOutput('plot1') )
))
Run Code Online (Sandbox Code Playgroud)
或者你可以把需要由双方访问所有的[R对象server.R,并ui.R在global.R文件中.
更多内容:http://shiny.rstudio.com/articles/scoping.html#global-objects