将excel文件读入Shiny [英] reading excel files into Shiny

查看:271
本文介绍了将excel文件读入Shiny的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Shiny的新手,我试图使用excel文件中的数据运行应用程序。似乎这样应该很简单,但不能弄清楚。在更复杂的任务(交互式上传文件,指定列,文件位置等)时,会出现信息的加载 - 但是我想要的是一个应用程序,它使用来自在后台加载的单个excel文件中的数据。



以前已经提出过类似的问题(将csv文件上传到ShinyApps.io R Shiny读取csv文件),但是我没有得到他们的satistactory答案。



我的excel文件保存在与app.R文件相同的目录中,在一个文件夹命名为数据。我将其读入我的脚本的服务器部分,如下所示:

  server<  -  function(input,output){
#读入数据
myDF< - read_excel('data / MyShinyData.xlsx')
/ pre>

当我运行app.R文件来测试应用程序时,工作正常。但是当我使用 shinyapps :: deployApp('pathToWorkingDirectory')将其发布到Shiny网站时,我得到一个没有交互性的应用程序的灰色版本。如果我模拟app.R文件中的数据(该excel文件只是这个模拟数据,用write.xlsx写入excel),该应用程序也会发布到网站上 - 它只有当我拿出模拟数据的代码并将其替换为停止工作的read_excel命令。我也试过一个.csv文件,而不是.xlsx,但是同样的问题。



我已经从下面的app.R文件复制了完整的代码。 / p>

我做错了什么?感谢您的帮助。

 库('ggplot2')
库('闪亮')
库('psych')
库('readxl')

#===============
#这个代码制作一个直方图,给予森林覆盖(NBT)的物种丰富度预测(SpNat)。
#===============

m1< - lm(SpNat〜NBT,data = myDF)#用于预测。最好在应用程序之外创建所有非反应性[即不更新]代码,因此不必每次运行。

#==========
#ui部分
#==========

ui < - fluidPage(

### MAKING A TITLE
titlePanel(基于excel数据的仪表板),

###将分支输入到SIDEBAR VS主要面板:
sidebarLayout(

sidebarPanel(#withy嵌套在这里将进入边栏
#dropdown输入为coplot:
标签$ h3('选择colot变量'),#heading
selectInput(inputId ='choiceX',label ='Choose X variable',
choices = c('Species richness'='SpNat','Forest cover'='NBT' ,'Pest control'='PC'))#***选择是连接的文本字符串
selectInput(inputId ='choiceY',label ='选择Y变量',
choices = c 'Species richness'='SpNat','Forest cover'='NBT','Pest control'='PC')),
selectInput(inputId ='choiceZ',label ='选择条件可变le',
choices = c('Species richness'='SpNat','Forest cover'='NBT','Pest control'='PC')),
#checkbox输入对图
标签$ h3('选择变量为对图'),#heading
checkboxGroupInput(inputId ='vars',label ='至少选择两个变量为对plot',
选择= c('SpNat','NBT','PC'),#'确定哪个vars开始检查。对于重要,cos< 2和情节不工作。
choices = c('Species richness'='SpNat','Forest cover'='NBT','Pest control'='PC')),#***服务器接收输入作为单个并置文本

#slider输入预测:
标签$ h3('预测森林覆盖'),#heading
sliderInput(inputId ='num',label ='选择森林覆盖',value = 10,min = 1,max = 100)),

mainPanel(#这里嵌套的东西将在主面板
#specify输出为应用程序,包括标题:
标签$ h3('Coplot:'),
plotOutput(outputId ='coplot'),
标签$ h3('Histogram:'),
plotOutput(outputId ='pairs '),
标签$ h3('Predicted species richness:'),
verbatimTextOutput('predict')))

#========= =
#服务器部分
#==========

服务器< - 函数(输入,输出){
#读入数据
myDF< - read_excel('data / MySh inyData.xlsx')#不需要完整路径
myDF $ PC< - as.factor(myDF $ PC)
myDF< - select(myDF,SpNat,NBT,PC)

#create输出对象,并将其命名为对应于ui输出功能ID,并使用ui输入ID创建它:
输出$ coplot< - renderPlot(
ggplot(myDF,aes_string(input $ choiceX,input $ choiceY,col = input $ choiceZ))+ geom_point())#note使用aes_string允许inputID使用直接。
输出$ pair< - renderPlot({
pairs.panels(subset(myDF,select = input $ vars))})
输出$ predict< - renderPrint({
newData< - data.frame(NBT = input $ num)
cat(predict(m1,newdata = newData))
})
}

# ==========
#并拼接
#==========

shinyApp(ui = ui,server = server )


解决方案

我有两个问题:



(1)我在发布之前将应用程序复制到一个新文件夹,所以工作目录已经更改 - 需要重新设置为包含我的app.R文件运行 shinyapps :: deployApp



(2)我的应用程序自动加载到我的R控制台(我已经更改了我的.Rprofile文件)。所以当我不需要加载这些来在本地运行应用程序时,我没有在线发布。



两个很愚蠢的错误,但是你生活和学习。 / p>

I'm new to Shiny and am trying to run an app using data from an excel file. 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.

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.

I have my excel file saved in the same directory as the app.R file, in a folder named 'data'. And I read it into the server part of my script like this:

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. 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. 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. I've also tried with a .csv file instead of .xlsx, but same problem.

I've copied the full code from the app.R file below.

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.

(2) a couple of packages required by my app load automatically into my R console (I've made changes to my .Rprofile file). 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.

这篇关于将excel文件读入Shiny的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆