簡體   English   中英

R Shiny讀取csv文件

[英]R Shiny read csv file

如果可以,我還有另一個關於在使用Shiny時讀取csv文件的問題。

我確實花了很多時間搜索和rtfm ...如果我錯過了什么我的道歉。 大多數答案似乎有點過於花哨,在選擇數據文件時會有用戶交互。 我只是想讓R Shiny在沒有任何用戶交互的情況下讀取數據文件(只是一個)。

我有標准文件ui.R和server.RI將它們放在工作目錄中。

我有一個帶有數據的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)

我收到一條錯誤消息:“錯誤:對象'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

和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 ###

我已將數據文件放在splashbox文件夾中的shinyDataITB.csv中

還可以通過以下方式在server.R中構建單選按鈕:

output$ui <- renderUI({sidebarPanel( 

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

並在ui.R通過:

uiOutput("ui")),

你的問題出在ui.R 您有對d.in$facnum的引用,但d.in中不存在d.in 它只存在於server.R 變量不會在這兩個實例之間共享。 服務器和UI需要通過輸入和輸出參數進行通信。

如果要從服務器上的數據設置單選按鈕的選項值,可以使用updateRadioButtons 首先,確保在shinyServer函數中包含session參數。

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

然后,這將設置該輸入中的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM