简体   繁体   English

将Excel文件读入Shiny

[英]reading excel files into Shiny

I'm new to Shiny and am trying to run an app using data from an excel file. 我是Shiny的新手,正在尝试使用excel文件中的数据运行应用程序。 Seems like it should be simple, but can't figure it out. 看起来应该很简单,但无法弄清楚。 there's load of info out there on more complex tasks (uploading files interactively, where you specify columns, file location etc) - but all I want is an app that uses data from a single excel file that has loaded in the background. 那里有大量信息,可以执行更复杂的任务(以交互方式上传文件,在其中指定列,文件位置等)-但我想要的只是一个使用来自后台加载的单个excel文件中数据的应用程序。

Similar questions have been asked before ( Uploading csv file to shinyApps.io and R Shiny read csv file ) but I didn't get a satistactory answer from them. 之前曾问过类似的问题( 将csv文件上传到ShinyApps.ioR Shiny读取csv文件 ),但我没有从他们那里得到令人满意的答案。

I have my excel file saved in the same directory as the app.R file, in a folder named 'data'. 我的excel文件保存在与app.R文件相同的目录下的“数据”文件夹中。 And I read it into the server part of my script like this: 我将其读入脚本的server部分,如下所示:

server <- function(input, output){
  # Read in data
  myDF <- read_excel('data/MyShinyData.xlsx')

When I run the app.R file to test the app, it works fine. 当我运行app.R文件来测试应用程序时,它可以正常工作。 But when I publish it to the Shiny website using shinyapps::deployApp('pathToWorkingDirectory') I get a grayed out version of the app that has no interactivity. 但是,当我使用shinyapps::deployApp('pathToWorkingDirectory')将其发布到Shiny网站时,我看到该应用程序的灰色版本没有交互性。 The app also publishes to the website fine if I simulate the data within the app.R file (the excel file is just this simulated data, written to excel with write.xlsx) - its only when I take out the code for simulating the data and replace it with the read_excel command that it stops working. 如果我模拟app.R文件中的数据(Excel文件就是该模拟数据,并使用write.xlsx写入excel),则该应用程序也可以很好地发布到网站上-仅当我取出用于模拟数据的代码时并将其替换为read_excel命令,它将停止工作。 I've also tried with a .csv file instead of .xlsx, but same problem. 我也尝试使用.csv文件而不是.xlsx,但是存在相同的问题。

I've copied the full code from the app.R file below. 我已经从下面的app.R文件复制了完整的代码。

What am I doing wrong? 我究竟做错了什么? Thanks for your help. 谢谢你的帮助。

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)

Figured it out. 弄清楚了。 I had two problems: 我有两个问题:

(1) I had copied the app into a new folder prior to publishing, so the working directory had changed - needed to reset to the folder containing my app.R file prior to running shinyapps::deployApp . (1)在发布之前,我已将应用复制到新文件夹中,因此工作目录已更改-需要在运行shinyapps::deployApp之前重置为包含我的app.R文件的文件夹。

(2) a couple of packages required by my app load automatically into my R console (I've made changes to my .Rprofile file). (2)应用程序所需的几个软件包会自动加载到R控制台中(我已对.Rprofile文件进行了更改)。 So while I didn't need to load these to run the app locally, I did to publish it online. 因此,虽然我不需要加载这些就可以在本地运行该应用程序,但是我确实将其在线发布了。

Both pretty dumb mistakes, but you live and learn. 两者都是很愚蠢的错误,但是您可以生活和学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM