R Shiny 读取 csv 文件

crl*_*ong 5 r shiny

如果可以,我还有一个关于在使用 Shiny 时读取 csv 文件的问题。

我确实花了很多时间搜索和 rtfm ......如果我错过了什么,我很抱歉。大多数答案似乎有点过于花哨,在选择数据文件时需要用户交互。我只是想让 R Shiny 在没有任何用户交互的情况下读取数据文件(只是一个)。

我有标准文件 ui.R 和 server.R 我将它们放在工作目录中。

我有一个包含数据的 csv 文件,我将其放在名为“data”的子目录中(基于http://shiny.rstudio.com/tutorial/lesson5/的教程)

在 R Studio 中,我手动将工作目录设置为包含 ui.R 和 server.R 文件的目录。我加载闪亮并执行 runApp()。server.R 中的一行脚本尝试使用 read.csv 将数据读入对象“d.in”。

这不起作用,所以我尝试在读取 csv 文件之前强制工作目录,然后在读取数据后和 ShinyServer 代码之前重置它。

代码片段:

wd.datapath = paste0(getwd(),"/data")
wd.init = getwd()
setwd(wd.datapath)

d.in = read.csv("shinyDataITB.csv", header = TRUE)

setwd(wd.init)
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息:“错误:找不到对象 'd.in'”

如果我在运行 runApp 之前手动加载 csv 数据文件,那么其他一切似乎都可以正常工作。我不确定我是如何搞砸的,但欢迎任何帮助。

ui.R 文件

##### ui.R #####

library(shiny)

shinyUI(pageWithSidebar(

  headerPanel("Supply ITB"), 

  sidebarPanel( 

    radioButtons(inputId = "in.facnum",
                 label = "Choose Facility",
                 choices = levels(d.in$facnum)) 
    ),  # end sidebarPanel

  mainPanel(
    h3("SPC chart"), 
    plotOutput("plotDisplay")
    )   # end mainPanel

  ))    # end Sidebar
Run Code Online (Sandbox Code Playgroud)

和 server.R 文件

##### server.R #####

# load packages -------------------------------------------------------------

library(shiny)
library(qcc)

# load the data -------------------------------------------------------------


wd.datapath = paste0(getwd(),"/data")
wd.init = getwd()
setwd(wd.datapath)

#d.in = read.csv(file.choose(), header = TRUE)

d.in = read.csv("shinyDataITB.csv", header = TRUE)

setwd(wd.init)


# add proportions related to fill_lines -------------------------------------

d.in$whprop = d.in$wh / d.in$volume
d.in$dmprop = d.in$dm / d.in$volume
d.in$mmprop = d.in$mm / d.in$volume


# select SPC response variable (using proportions) --------------------------

qccvar = "whprop"


# shiny server body ---------------------------------------------------------

shinyServer(function(input,output) { 


# Individuals (X) chart -----------------------------------------------------

  output$plotDisplay <- renderPlot({

    # select subset for specific facility

    d.strata = subset(d.in, d.in$facnum == input$in.facnum)  # subset

    d.strata = d.strata[order(d.strata$year, d.strata$monthnum, 
                              decreasing = FALSE),]  # order by month

    # create SPC chart

    x.chart = qcc( d.strata[,qccvar], type = "xbar.one", 
               title = paste("Individuals chart\n", input$in.facnum, qccvar) )


  })  # end renderPlot

})    # end shinyServer


### END CODE ###
Run Code Online (Sandbox Code Playgroud)

我已经把数据文件放在一个 dropbox 文件夹中,这里是ShinyDataITB.csv

use*_*568 4

还可以通过以下方式在 server.R 中构建单选按钮:

output$ui <- renderUI({sidebarPanel( 

radioButtons(inputId = "in.facnum",
             label = "Choose Facility",
             choices = levels(d.in$facnum)) 
),  # end sidebarPanel
Run Code Online (Sandbox Code Playgroud)

在 ui.R 中通过:

uiOutput("ui")),
Run Code Online (Sandbox Code Playgroud)