简体   繁体   English

R Shiny读取csv文件

[英]R Shiny read csv file

If I may, I have another question about reading a csv file while using Shiny. 如果可以,我还有另一个关于在使用Shiny时读取csv文件的问题。

I did spend quite a bit of time searching and rtfm ... my apologies if I missed something. 我确实花了很多时间搜索和rtfm ...如果我错过了什么我的道歉。 Most answers seemed a bit too fancy, with user interaction when selecting a data file. 大多数答案似乎有点过于花哨,在选择数据文件时会有用户交互。 I simply want R Shiny to read a data file (just the one) without any user interaction. 我只是想让R Shiny在没有任何用户交互的情况下读取数据文件(只是一个)。

I have the standard files ui.R and server.RI place them in a working directory. 我有标准文件ui.R和server.RI将它们放在工作目录中。

I have a csv file with data which I place in a subdirectory called 'data' (based on a tutorial here http://shiny.rstudio.com/tutorial/lesson5/ ) 我有一个带有数据的csv文件,我把它放在一个名为'data'的子目录中(基于这里的教程http://shiny.rstudio.com/tutorial/lesson5/

From within R Studio I manually set the working directory to the one with the ui.R and server.R files. 在R Studio中,我手动将工作目录设置为具有ui.R和server.R文件的目录。 I load shiny and do runApp(). 我加载闪亮并执行runApp()。 A line of script in server.R tries to use read.csv to read the data into an object 'd.in'. server.R中的一行脚本尝试使用read.csv将数据读入对象'd.in'。

This did not work, so I tried coercing the working directory before reading the csv file, and then resetting it after reading the data and before the shinyServer code. 这不起作用,所以我尝试在读取csv文件之前强制执行工作目录,然后在读取数据之后和shinyServer代码之前重置它。

code snippet: 代码段:

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

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

setwd(wd.init)

I get an error message: "ERROR: object 'd.in' not found" 我收到一条错误消息:“错误:对象'd.in'未找到”

If I manually load the csv data file prior to running runApp then everything else seems to work. 如果我在运行runApp之前手动加载csv数据文件,那么其他一切似乎都有效。 I'm not sure how I've botched this but any help will be welcome. 我不确定我是如何拙劣的,但欢迎任何帮助。

The ui.R file 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

And the server.R file 和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 ###

I've put the data file in a dropbox folder here shinyDataITB.csv 我已将数据文件放在splashbox文件夹中的shinyDataITB.csv中

What could also work is building the radio button in server.R via: 还可以通过以下方式在server.R中构建单选按钮:

output$ui <- renderUI({sidebarPanel( 

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

And in ui.R via: 并在ui.R通过:

uiOutput("ui")),

Your problem is in the ui.R . 你的问题出在ui.R You have a reference to d.in$facnum but d.in does not exist in the UI. 您有对d.in$facnum的引用,但d.in中不存在d.in It only exists in server.R . 它只存在于server.R Variables are not shared across the two instances like that. 变量不会在这两个实例之间共享。 The server and UI need to communicate via the input and output parameters. 服务器和UI需要通过输入和输出参数进行通信。

If you want to set the value of choices for your radio button from the data on the server, you can use updateRadioButtons . 如果要从服务器上的数据设置单选按钮的选项值,可以使用updateRadioButtons First, make sure you include the session parameter in the shinyServer function. 首先,确保在shinyServer函数中包含session参数。

shinyServer(function(input,output, session) { 
    ...
    updateRadioButtons(session, "in.facnum", choices=levels(d.in$facnum))
    ...
}

Then, this will set the values in that input. 然后,这将设置该输入中的值。

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

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