正在将 excel 个文件读入 Shiny
reading excel files into Shiny
我是 Shiny 的新手,我正在尝试 运行 使用来自 excel 文件的数据的应用程序。看起来应该很简单,但无法弄清楚。那里有很多关于更复杂任务的信息(交互式上传文件,您可以在其中指定列、文件位置等)——但我想要的只是一个应用程序,它使用来自后台加载的单个 excel 文件的数据.
之前也有人问过类似的问题(Uploading csv file to shinyApps.io and 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 文件就是这个模拟数据,用 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) 我在发布前将应用程序复制到一个新文件夹中,因此工作目录已更改 - 需要在 运行 之前重置为包含我的 app.R 文件的文件夹shinyapps::deployApp
.
(2) 我的应用程序所需的几个包自动加载到我的 R 控制台中(我已经更改了我的 .Rprofile 文件)。因此,虽然我不需要将这些加载到本地 运行 应用程序,但我还是在线发布了它。
这两个都是非常愚蠢的错误,但你在生活中学习。
我是 Shiny 的新手,我正在尝试 运行 使用来自 excel 文件的数据的应用程序。看起来应该很简单,但无法弄清楚。那里有很多关于更复杂任务的信息(交互式上传文件,您可以在其中指定列、文件位置等)——但我想要的只是一个应用程序,它使用来自后台加载的单个 excel 文件的数据.
之前也有人问过类似的问题(Uploading csv file to shinyApps.io and 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 文件就是这个模拟数据,用 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) 我在发布前将应用程序复制到一个新文件夹中,因此工作目录已更改 - 需要在 运行 之前重置为包含我的 app.R 文件的文件夹shinyapps::deployApp
.
(2) 我的应用程序所需的几个包自动加载到我的 R 控制台中(我已经更改了我的 .Rprofile 文件)。因此,虽然我不需要将这些加载到本地 运行 应用程序,但我还是在线发布了它。
这两个都是非常愚蠢的错误,但你在生活中学习。