我是Shiny的新手,正嘗試使用Excel文件中的數據運行應用程序。似乎應該很簡單,但無法弄清楚。還有更復雜的任務(交互式上傳文件,指定列,文件位置等)的負載信息 - 但我想要的是一個應用程序,它使用來自加載在後臺的單個Excel文件中的數據。將excel文件讀入Shiny
之前已經提出過類似問題(Uploading csv file to shinyApps.io和R Shiny read csv file),但我沒有從他們那裏得到滿意的答案。
我把我的excel文件保存在與app.R文件相同的目錄下,名爲'data'的文件夾中。我讀入到我的劇本的server
部分是這樣的:
server <- function(input, output){
# Read in data
myDF <- read_excel('data/MyShinyData.xlsx')
當我運行app.R文件來測試應用程序,它工作正常。但是,當我使用shinyapps::deployApp('pathToWorkingDirectory')
將其發佈到Shiny網站時,我得到的應用程序的灰色版本沒有交互性。如果我模擬app.R文件中的數據(excel文件就是這個模擬數據,寫入excell與write.xlsx),該應用程序還會向網站發佈罰款 - 只有當我拿出用於模擬數據的代碼時並用它停止工作的read_excel命令替換它。我也嘗試使用.csv文件而不是.xlsx,但同樣的問題。
我已經從下面的app.R文件複製了完整的代碼。
我在做什麼錯?謝謝你的幫助。
library('ggplot2')
library('shiny')
library('psych')
library('readxl')
#===============
#This code makes a histogram, a coplot, and a prediction for species richness ('SpNat') given Forest cover ('NBT').
#===============
m1 <- lm(SpNat ~ NBT, data=myDF) #For prediction. best to create all non-reactive [ie non-updating] code outside the app, so it doesn't have to run every time.
#==========
# ui section
#==========
ui <- fluidPage(
### MAKING A TITLE
titlePanel("Dashboard based on excel data"),
### DIVIDING INPUTS TO SIDEBAR VS MAIN PANELS:
sidebarLayout(
sidebarPanel( #everything nested in here will go in sidebar
#dropdown input for coplot:
tags$h3('Select coplot variables'), #heading
selectInput(inputId='choiceX', label='Choose X variable',
choices=c('Species richness'='SpNat', 'Forest cover'='NBT', 'Pest control'='PC')), #***Choices are concatenated text strings.
selectInput(inputId='choiceY', label='Choose Y variable',
choices=c('Species richness'='SpNat', 'Forest cover'='NBT', 'Pest control'='PC')),
selectInput(inputId='choiceZ', label='Choose conditioning variable',
choices=c('Species richness'='SpNat', 'Forest cover'='NBT', 'Pest control'='PC')),
#checkbox input for pairs plots:
tags$h3('Select variables for pairs plots'), #heading
checkboxGroupInput(inputId='vars', label='Choose at least two variables for pairs plot',
selected=c('SpNat', 'NBT', 'PC'), #'determines which vars start off checked. Important for pairs, cos <2 and plot wont work.
choices=c('Species richness'='SpNat', 'Forest cover'='NBT', 'Pest control'='PC')), #***Server receives input as a single concatenated text
#slider input for prediction:
tags$h3('Predicting forest cover'), #heading
sliderInput(inputId='num',label='Pick a forest cover level', value=10, min=1, max=100)),
mainPanel( #everything nested in here will go in main panel
#specify output for app, including headings:
tags$h3('Coplot:'),
plotOutput(outputId='coplot'),
tags$h3('Histogram:'),
plotOutput(outputId='pairs'),
tags$h3('Predicted species richness:'),
verbatimTextOutput('prediction'))))
#==========
# server section
#==========
server <- function(input, output){
# Read in data
myDF <- read_excel('data/MyShinyData.xlsx') #don't need full path
myDF$PC <- as.factor(myDF$PC)
myDF <- select(myDF, SpNat, NBT, PC)
#create output object, and name it so it corresponds to the ui output function ID, plus use the ui input ID to create it:
output$coplot <- renderPlot(
ggplot(myDF, aes_string(input$choiceX, input$choiceY, col=input$choiceZ)) + geom_point()) #note use of aes_string to allow inputID use direct.
output$pairs <- renderPlot({
pairs.panels(subset(myDF, select=input$vars))})
output$prediction <- renderPrint({
newData <- data.frame(NBT=input$num)
cat(predict(m1, newdata = newData))
})
}
#==========
# and stitch together
#==========
shinyApp(ui=ui, server=server)
您需要確保使用相對路徑。 –
@ MattO'Brien謝謝你。我想我已經使用相對路徑了?我的Shiny app有這個路徑'C:/ Users/new user/Documents/TESTShinyAppWithExcelData/app.R',我的數據有這個'C:/ Users/new user/Documents/TESTShinyAppWithExcelData/data/MyShinyData.xlsx',並且我用這個命令read_excel('data/MyShinyData.xlsx')'從閃亮的應用程序讀取數據... ...那麼這是一個正確的相對路徑了嗎?乾杯 – jay