![](/img/trans.png)
[英]SHINY R reading different versions of excel files based on user selections
[英]reading excel files into Shiny
我是Shiny的新手,正在嘗試使用excel文件中的數據運行應用程序。 看起來應該很簡單,但無法弄清楚。 那里有大量信息,可以執行更復雜的任務(以交互方式上傳文件,在其中指定列,文件位置等)-但我想要的只是一個使用來自后台加載的單個excel文件中數據的應用程序。
之前曾問過類似的問題( 將csv文件上傳到ShinyApps.io和R Shiny讀取csv文件 ),但我沒有從他們那里得到令人滿意的答案。
我的excel文件保存在與app.R文件相同的目錄下的“數據”文件夾中。 我將其讀入腳本的server
部分,如下所示:
server <- function(input, output){
# Read in data
myDF <- read_excel('data/MyShinyData.xlsx')
當我運行app.R文件來測試應用程序時,它可以正常工作。 但是,當我使用shinyapps::deployApp('pathToWorkingDirectory')
將其發布到Shiny網站時,我看到該應用程序的灰色版本沒有交互性。 如果我模擬app.R文件中的數據(Excel文件就是該模擬數據,並使用write.xlsx寫入excel),則該應用程序也可以很好地發布到網站上-僅當我取出用於模擬數據的代碼時並將其替換為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)
弄清楚了。 我有兩個問題:
(1)在發布之前,我已將應用復制到新文件夾中,因此工作目錄已更改-需要在運行shinyapps::deployApp
之前重置為包含我的app.R文件的文件夾。
(2)應用程序所需的幾個軟件包會自動加載到R控制台中(我已對.Rprofile文件進行了更改)。 因此,雖然我不需要加載這些就可以在本地運行該應用程序,但是我確實將其在線發布了。
兩者都是很愚蠢的錯誤,但是您可以生活和學習。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.